diff options
Diffstat (limited to 'cli/flags.rs')
-rw-r--r-- | cli/flags.rs | 203 |
1 files changed, 171 insertions, 32 deletions
diff --git a/cli/flags.rs b/cli/flags.rs index dbd185efb..142ece863 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -203,19 +203,87 @@ pub fn parse_flags(matches: ArgMatches) -> DenoFlags { flags } +/// Used for `deno fmt <files>...` subcommand +const PRETTIER_URL: &str = "https://deno.land/std/prettier/main.ts"; + +/// These are currently handled subcommands. +/// There is no "Help" subcommand because it's handled by `clap::App` itself. +#[derive(Debug, PartialEq)] +pub enum DenoSubcommand { + Eval, + Fetch, + Info, + Repl, + Run, + Types, +} + +pub fn flags_from_vec( + args: Vec<String>, +) -> (DenoFlags, DenoSubcommand, Vec<String>) { + let cli_app = create_cli_app(); + let matches = cli_app.get_matches_from(args); + let mut argv: Vec<String> = vec!["deno".to_string()]; + let mut flags = parse_flags(matches.clone()); + + let subcommand = match matches.subcommand() { + ("eval", Some(eval_match)) => { + let code: &str = eval_match.value_of("code").unwrap(); + argv.extend(vec![code.to_string()]); + DenoSubcommand::Eval + } + ("fetch", Some(fetch_match)) => { + let file: &str = fetch_match.value_of("file").unwrap(); + argv.extend(vec![file.to_string()]); + DenoSubcommand::Fetch + } + ("fmt", Some(fmt_match)) => { + flags.allow_read = true; + flags.allow_write = true; + argv.push(PRETTIER_URL.to_string()); + + let files: Vec<String> = fmt_match + .values_of("files") + .unwrap() + .map(String::from) + .collect(); + argv.extend(files); + + DenoSubcommand::Run + } + ("info", Some(info_match)) => { + let file: &str = info_match.value_of("file").unwrap(); + argv.extend(vec![file.to_string()]); + DenoSubcommand::Info + } + ("types", Some(_)) => DenoSubcommand::Types, + (script, Some(script_match)) => { + argv.extend(vec![script.to_string()]); + // check if there are any extra arguments that should + // be passed to script + if script_match.is_present("") { + let script_args: Vec<String> = script_match + .values_of("") + .unwrap() + .map(String::from) + .collect(); + argv.extend(script_args); + } + DenoSubcommand::Run + } + _ => DenoSubcommand::Repl, + }; + + (flags, subcommand, argv) +} + #[cfg(test)] mod tests { use super::*; - fn flags_from_vec(args: Vec<String>) -> DenoFlags { - let cli_app = create_cli_app(); - let matches = cli_app.get_matches_from(args); - parse_flags(matches) - } - #[test] - fn test_set_flags_1() { - let flags = flags_from_vec(svec!["deno", "version"]); + fn test_flags_from_vec_1() { + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "version"]); assert_eq!( flags, DenoFlags { @@ -223,11 +291,14 @@ mod tests { ..DenoFlags::default() } ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!(argv, svec!["deno", "version"]); } #[test] - fn test_set_flags_2() { - let flags = flags_from_vec(svec!["deno", "-r", "-D", "script.ts"]); + fn test_flags_from_vec_2() { + let (flags, subcommand, argv) = + flags_from_vec(svec!["deno", "-r", "-D", "script.ts"]); assert_eq!( flags, DenoFlags { @@ -236,11 +307,13 @@ mod tests { ..DenoFlags::default() } ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!(argv, svec!["deno", "script.ts"]); } #[test] - fn test_set_flags_3() { - let flags = + fn test_flags_from_vec_3() { + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "-r", "--allow-write", "script.ts"]); assert_eq!( flags, @@ -250,11 +323,13 @@ mod tests { ..DenoFlags::default() } ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!(argv, svec!["deno", "script.ts"]); } #[test] - fn test_set_flags_4() { - let flags = + fn test_flags_from_vec_4() { + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "-Dr", "--allow-write", "script.ts"]); assert_eq!( flags, @@ -265,11 +340,14 @@ mod tests { ..DenoFlags::default() } ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!(argv, svec!["deno", "script.ts"]); } #[test] - fn test_set_flags_5() { - let flags = flags_from_vec(svec!["deno", "--v8-options"]); + fn test_flags_from_vec_5() { + let (flags, subcommand, argv) = + flags_from_vec(svec!["deno", "--v8-options"]); assert_eq!( flags, DenoFlags { @@ -277,8 +355,10 @@ mod tests { ..DenoFlags::default() } ); + assert_eq!(subcommand, DenoSubcommand::Repl); + assert_eq!(argv, svec!["deno"]); - let flags = + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "--v8-flags=--expose-gc,--gc-stats=1"]); assert_eq!( flags, @@ -287,11 +367,13 @@ mod tests { ..DenoFlags::default() } ); + assert_eq!(subcommand, DenoSubcommand::Repl); + assert_eq!(argv, svec!["deno"]); } #[test] - fn test_set_flags_6() { - let flags = + fn test_flags_from_vec_6() { + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "--allow-net", "gist.ts", "--title", "X"]); assert_eq!( flags, @@ -299,12 +381,15 @@ mod tests { allow_net: true, ..DenoFlags::default() } - ) + ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!(argv, svec!["deno", "gist.ts", "--title", "X"]); } #[test] - fn test_set_flags_7() { - let flags = flags_from_vec(svec!["deno", "--allow-all", "gist.ts"]); + fn test_flags_from_vec_7() { + let (flags, subcommand, argv) = + flags_from_vec(svec!["deno", "--allow-all", "gist.ts"]); assert_eq!( flags, DenoFlags { @@ -316,24 +401,29 @@ mod tests { allow_high_precision: true, ..DenoFlags::default() } - ) + ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!(argv, svec!["deno", "gist.ts"]); } #[test] - fn test_set_flags_8() { - let flags = flags_from_vec(svec!["deno", "--allow-read", "gist.ts"]); + fn test_flags_from_vec_8() { + let (flags, subcommand, argv) = + flags_from_vec(svec!["deno", "--allow-read", "gist.ts"]); assert_eq!( flags, DenoFlags { allow_read: true, ..DenoFlags::default() } - ) + ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!(argv, svec!["deno", "gist.ts"]); } #[test] - fn test_set_flags_9() { - let flags = + fn test_flags_from_vec_9() { + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "--allow-high-precision", "script.ts"]); assert_eq!( flags, @@ -341,15 +431,17 @@ mod tests { allow_high_precision: true, ..DenoFlags::default() } - ) + ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!(argv, svec!["deno", "script.ts"]); } #[test] - fn test_set_flags_10() { + fn test_flags_from_vec_10() { // notice that flags passed after script name will not // be parsed to DenoFlags but instead forwarded to // script args as Deno.args - let flags = flags_from_vec(svec![ + let (flags, subcommand, argv) = flags_from_vec(svec![ "deno", "--allow-write", "script.ts", @@ -362,7 +454,54 @@ mod tests { allow_write: true, ..DenoFlags::default() } - ) + ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!(argv, svec!["deno", "script.ts", "-D", "--allow-net"]); + } + + #[test] + fn test_flags_from_vec_11() { + let (flags, subcommand, argv) = + flags_from_vec(svec!["deno", "fmt", "script_1.ts", "script_2.ts"]); + assert_eq!( + flags, + DenoFlags { + allow_write: true, + allow_read: true, + ..DenoFlags::default() + } + ); + assert_eq!(subcommand, DenoSubcommand::Run); + assert_eq!( + argv, + svec!["deno", PRETTIER_URL, "script_1.ts", "script_2.ts"] + ); + } + + #[test] + fn test_flags_from_vec_12() { + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "types"]); + assert_eq!(flags, DenoFlags::default()); + assert_eq!(subcommand, DenoSubcommand::Types); + assert_eq!(argv, svec!["deno"]); + } + + #[test] + fn test_flags_from_vec_13() { + let (flags, subcommand, argv) = + flags_from_vec(svec!["deno", "fetch", "script.ts"]); + assert_eq!(flags, DenoFlags::default()); + assert_eq!(subcommand, DenoSubcommand::Fetch); + assert_eq!(argv, svec!["deno", "script.ts"]); + } + + #[test] + fn test_flags_from_vec_14() { + let (flags, subcommand, argv) = + flags_from_vec(svec!["deno", "info", "script.ts"]); + assert_eq!(flags, DenoFlags::default()); + assert_eq!(subcommand, DenoSubcommand::Info); + assert_eq!(argv, svec!["deno", "script.ts"]); } #[test] |