From 36eaa9b3b379e11fe611bdd74f88eb6095ccc8cd Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 9 Dec 2022 13:29:08 +0300 Subject: [PATCH] chore(tvix/cli): re-add `--raw` argument Change-Id: I82f469f7f186c754c40bd941587b30aa3fc48045 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7548 Reviewed-by: grfn Tested-by: BuildkiteCI --- tvix/cli/src/main.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/tvix/cli/src/main.rs b/tvix/cli/src/main.rs index 75142d620..5604e1a83 100644 --- a/tvix/cli/src/main.rs +++ b/tvix/cli/src/main.rs @@ -11,15 +11,16 @@ struct Args { #[clap(long, short = 'E')] expr: Option, - // TODO: port these options here directly - // #[clap(flatten)] - // eval_options: tvix_eval::Options, + + /// Print "raw" (unquoted) output. + #[clap(long)] + raw: bool, } /// Interprets the given code snippet, printing out warnings, errors /// and the result itself. The return value indicates whether /// evaluation succeeded. -fn interpret(code: &str, path: Option) -> bool { +fn interpret(code: &str, path: Option, args: &Args) -> bool { let eval = tvix_eval::Evaluation::new(code, path); let source_map = eval.source_map(); let result = eval.evaluate(); @@ -33,7 +34,7 @@ fn interpret(code: &str, path: Option) -> bool { } if let Some(value) = result.value.as_ref() { - println_result(value, /* TODO raw = */ false); + println_result(value, args.raw); } // inform the caller about any errors @@ -43,24 +44,24 @@ fn interpret(code: &str, path: Option) -> bool { fn main() { let args = Args::parse(); - if let Some(file) = args.script { - run_file(file /* TODO, args.eval_options*/) - } else if let Some(expr) = args.expr { - if !interpret(&expr, None) { + if let Some(file) = &args.script { + run_file(file.clone(), &args) + } else if let Some(expr) = &args.expr { + if !interpret(expr, None, &args) { std::process::exit(1); } } else { - run_prompt(/* TODO args.eval_options */) + run_prompt(&args) } } -fn run_file(mut path: PathBuf /* TODO: , eval_options: tvix_eval::Options */) { +fn run_file(mut path: PathBuf, args: &Args) { if path.is_dir() { path.push("default.nix"); } let contents = fs::read_to_string(&path).expect("failed to read the input file"); - if !interpret(&contents, Some(path)) { + if !interpret(&contents, Some(path), args) { std::process::exit(1); } } @@ -81,7 +82,7 @@ fn state_dir() -> Option { path } -fn run_prompt(/* TODO eval_options: tvix_eval::Options */) { +fn run_prompt(args: &Args) { let mut rl = Editor::<()>::new().expect("should be able to launch rustyline"); let history_path = match state_dir() { @@ -106,7 +107,7 @@ fn run_prompt(/* TODO eval_options: tvix_eval::Options */) { } rl.add_history_entry(&line); - interpret(&line, None); + interpret(&line, None, args); } Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break,