From df3b6d9d264747bff0e2a81ab6eddb063d11a442 Mon Sep 17 00:00:00 2001 From: Ariel Cohen Date: Thu, 18 Aug 2022 12:25:52 +0300 Subject: [PATCH] Add --execute option (#6302) --- crates/nu-cli/src/repl.rs | 14 +++++++++++++- src/main.rs | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 73b55d894..de245e040 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -16,7 +16,7 @@ use nu_protocol::{ ast::PathMember, engine::{EngineState, Stack, StateWorkingSet}, format_duration, BlockId, HistoryFileFormat, PipelineData, PositionalArg, ShellError, Span, - Type, Value, VarId, + Spanned, Type, Value, VarId, }; use reedline::{DefaultHinter, Emacs, SqliteBackedHistory, Vi}; use std::io::{self, Write}; @@ -39,6 +39,7 @@ pub fn evaluate_repl( stack: &mut Stack, nushell_path: &str, is_perf_true: bool, + prerun_command: Option>, ) -> Result<()> { use reedline::{FileBackedHistory, Reedline, Signal}; @@ -140,6 +141,17 @@ pub fn evaluate_repl( } } + if let Some(s) = prerun_command { + eval_source( + engine_state, + stack, + s.item.as_bytes(), + &format!("entry #{}", entry_num), + PipelineData::new(Span::new(0, 0)), + ); + engine_state.merge_env(stack, get_guaranteed_cwd(engine_state, stack))?; + } + loop { if is_perf_true { info!( diff --git a/src/main.rs b/src/main.rs index 8c6c0246d..92017a413 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,7 +101,7 @@ fn main() -> Result<()> { } else if arg.starts_with('-') { // Cool, it's a flag let flag_value = match arg.as_ref() { - "--commands" | "-c" | "--table-mode" | "-m" => { + "--commands" | "-c" | "--table-mode" | "-m" | "-e" | "--execute" => { args.next().map(|a| escape_quote_string(&a)) } "--config" | "--env-config" => args.next().map(|a| escape_quote_string(&a)), @@ -341,6 +341,7 @@ fn main() -> Result<()> { &mut stack, config_files::NUSHELL_FOLDER, is_perf_true(), + binary_args.execute, ); if is_perf_true() { info!("repl eval {}:{}:{}", file!(), line!(), column!()); @@ -438,6 +439,7 @@ fn parse_commandline_args( let env_file: Option = call.get_flag_expr("env-config"); let log_level: Option = call.get_flag_expr("log-level"); let log_target: Option = call.get_flag_expr("log-target"); + let execute: Option = call.get_flag_expr("execute"); let threads: Option = call.get_flag(engine_state, &mut stack, "threads")?; let table_mode: Option = call.get_flag(engine_state, &mut stack, "table-mode")?; @@ -468,6 +470,7 @@ fn parse_commandline_args( let env_file = extract_contents(env_file)?; let log_level = extract_contents(log_level)?; let log_target = extract_contents(log_target)?; + let execute = extract_contents(execute)?; let help = call.has_flag("help"); @@ -501,6 +504,7 @@ fn parse_commandline_args( env_file, log_level, log_target, + execute, perf, threads, table_mode, @@ -527,6 +531,7 @@ struct NushellCliArgs { env_file: Option>, log_level: Option>, log_target: Option>, + execute: Option>, perf: bool, threads: Option, table_mode: Option, @@ -588,6 +593,12 @@ impl Command for Nu { "set the target for the log to output. stdout, stderr(default), mixed or file", None, ) + .named( + "execute", + SyntaxShape::String, + "run the given commands and then enter an interactive shell", + Some('e'), + ) .named( "threads", SyntaxShape::Int,