Add --execute option (#6302)

This commit is contained in:
Ariel Cohen 2022-08-18 12:25:52 +03:00 committed by GitHub
parent 4bbdb73668
commit df3b6d9d26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 2 deletions

View file

@ -16,7 +16,7 @@ use nu_protocol::{
ast::PathMember, ast::PathMember,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
format_duration, BlockId, HistoryFileFormat, PipelineData, PositionalArg, ShellError, Span, format_duration, BlockId, HistoryFileFormat, PipelineData, PositionalArg, ShellError, Span,
Type, Value, VarId, Spanned, Type, Value, VarId,
}; };
use reedline::{DefaultHinter, Emacs, SqliteBackedHistory, Vi}; use reedline::{DefaultHinter, Emacs, SqliteBackedHistory, Vi};
use std::io::{self, Write}; use std::io::{self, Write};
@ -39,6 +39,7 @@ pub fn evaluate_repl(
stack: &mut Stack, stack: &mut Stack,
nushell_path: &str, nushell_path: &str,
is_perf_true: bool, is_perf_true: bool,
prerun_command: Option<Spanned<String>>,
) -> Result<()> { ) -> Result<()> {
use reedline::{FileBackedHistory, Reedline, Signal}; 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 { loop {
if is_perf_true { if is_perf_true {
info!( info!(

View file

@ -101,7 +101,7 @@ fn main() -> Result<()> {
} else if arg.starts_with('-') { } else if arg.starts_with('-') {
// Cool, it's a flag // Cool, it's a flag
let flag_value = match arg.as_ref() { 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)) args.next().map(|a| escape_quote_string(&a))
} }
"--config" | "--env-config" => 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, &mut stack,
config_files::NUSHELL_FOLDER, config_files::NUSHELL_FOLDER,
is_perf_true(), is_perf_true(),
binary_args.execute,
); );
if is_perf_true() { if is_perf_true() {
info!("repl eval {}:{}:{}", file!(), line!(), column!()); info!("repl eval {}:{}:{}", file!(), line!(), column!());
@ -438,6 +439,7 @@ fn parse_commandline_args(
let env_file: Option<Expression> = call.get_flag_expr("env-config"); let env_file: Option<Expression> = call.get_flag_expr("env-config");
let log_level: Option<Expression> = call.get_flag_expr("log-level"); let log_level: Option<Expression> = call.get_flag_expr("log-level");
let log_target: Option<Expression> = call.get_flag_expr("log-target"); let log_target: Option<Expression> = call.get_flag_expr("log-target");
let execute: Option<Expression> = call.get_flag_expr("execute");
let threads: Option<Value> = call.get_flag(engine_state, &mut stack, "threads")?; let threads: Option<Value> = call.get_flag(engine_state, &mut stack, "threads")?;
let table_mode: Option<Value> = let table_mode: Option<Value> =
call.get_flag(engine_state, &mut stack, "table-mode")?; 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 env_file = extract_contents(env_file)?;
let log_level = extract_contents(log_level)?; let log_level = extract_contents(log_level)?;
let log_target = extract_contents(log_target)?; let log_target = extract_contents(log_target)?;
let execute = extract_contents(execute)?;
let help = call.has_flag("help"); let help = call.has_flag("help");
@ -501,6 +504,7 @@ fn parse_commandline_args(
env_file, env_file,
log_level, log_level,
log_target, log_target,
execute,
perf, perf,
threads, threads,
table_mode, table_mode,
@ -527,6 +531,7 @@ struct NushellCliArgs {
env_file: Option<Spanned<String>>, env_file: Option<Spanned<String>>,
log_level: Option<Spanned<String>>, log_level: Option<Spanned<String>>,
log_target: Option<Spanned<String>>, log_target: Option<Spanned<String>>,
execute: Option<Spanned<String>>,
perf: bool, perf: bool,
threads: Option<Value>, threads: Option<Value>,
table_mode: Option<Value>, table_mode: Option<Value>,
@ -588,6 +593,12 @@ impl Command for Nu {
"set the target for the log to output. stdout, stderr(default), mixed or file", "set the target for the log to output. stdout, stderr(default), mixed or file",
None, None,
) )
.named(
"execute",
SyntaxShape::String,
"run the given commands and then enter an interactive shell",
Some('e'),
)
.named( .named(
"threads", "threads",
SyntaxShape::Int, SyntaxShape::Int,