diff --git a/cli/flags.rs b/cli/flags.rs index 0a6b353f92a732..a889555bb8fce3 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -116,8 +116,8 @@ Examples: https://github.com/WICG/import-maps#the-import-map", pub fn create_cli_app<'a, 'b>() -> App<'a, 'b> { add_run_args(App::new("deno")) .bin_name("deno") - .global_settings(&[AppSettings::ColorNever, AppSettings::UnifiedHelpMessage]) - .settings(&[AppSettings::DisableVersion, AppSettings::AllowExternalSubcommands]) + .global_settings(&[AppSettings::ColorNever, AppSettings::UnifiedHelpMessage, AppSettings::DisableVersion]) + .settings(&[AppSettings::AllowExternalSubcommands]) .after_help(ENV_VARIABLES_HELP) .long_about("A secure runtime for JavaScript and TypeScript built with V8, Rust, and Tokio. @@ -140,6 +140,17 @@ To evaluate code from the command line: To get help on the another subcommands (run in this case): deno help run") + .arg( + Arg::with_name("version") + .short("v") + .long("version") + .help("Print the version") + .long_help("Print current version of Deno. + +Includes versions of Deno, V8 JavaScript Engine, and the TypeScript +compiler.", + ) + ) .arg( Arg::with_name("log-level") .short("L") @@ -190,7 +201,6 @@ To get help on the another subcommands (run in this case): .global(true), ).subcommand( SubCommand::with_name("version") - .setting(AppSettings::DisableVersion) .about("Print the version") .long_about("Print current version of Deno. @@ -199,7 +209,6 @@ compiler.", ), ).subcommand( SubCommand::with_name("bundle") - .setting(AppSettings::DisableVersion) .about("Bundle module and dependencies into single file") .long_about( "Output a single JavaScript file with all dependencies @@ -212,7 +221,6 @@ Example: .arg(Arg::with_name("out_file").takes_value(true).required(false)), ).subcommand( SubCommand::with_name("fetch") - .setting(AppSettings::DisableVersion) .about("Fetch the dependencies") .long_about( "Fetch and compile remote dependencies recursively. @@ -229,7 +237,6 @@ would be made unless --reload is specified. ).arg(Arg::with_name("file").takes_value(true).required(true)), ).subcommand( SubCommand::with_name("types") - .setting(AppSettings::DisableVersion) .about("Print runtime TypeScript declarations") .long_about("Print runtime TypeScript declarations. @@ -239,7 +246,6 @@ The declaration file could be saved and used for typing information.", ), ).subcommand( SubCommand::with_name("info") - .setting(AppSettings::DisableVersion) .about("Show source file related info") .long_about("Show source file related info. @@ -255,7 +261,6 @@ The following information is shown: ).arg(Arg::with_name("file").takes_value(true).required(true)), ).subcommand( SubCommand::with_name("eval") - .setting(AppSettings::DisableVersion) .about("Eval script") .long_about( "Evaluate provided script. @@ -266,7 +271,6 @@ This command has implicit access to all permissions (equivalent to deno run --al ).arg(Arg::with_name("code").takes_value(true).required(true)), ).subcommand( SubCommand::with_name("fmt") - .setting(AppSettings::DisableVersion) .about("Format files") .long_about( "Auto-format JavaScript/TypeScript source code using Prettier @@ -290,7 +294,6 @@ Automatically downloads Prettier dependencies on first run. .settings(&[ AppSettings::AllowExternalSubcommands, AppSettings::DisableHelpSubcommand, - AppSettings::DisableVersion, AppSettings::SubcommandRequired, ]).about("Run a program given a filename or url to the source code") .long_about( @@ -317,7 +320,6 @@ ability to spawn subprocesses. ), ).subcommand( SubCommand::with_name("xeval") - .setting(AppSettings::DisableVersion) .about("Eval a script on text segments from stdin") .long_about( "Eval a script on lines from stdin @@ -357,7 +359,6 @@ Demonstrates breaking the input up by space delimiter instead of by lines: ).subcommand( SubCommand::with_name("install") .settings(&[ - AppSettings::DisableVersion, AppSettings::DisableHelpSubcommand, AppSettings::AllowExternalSubcommands, AppSettings::SubcommandRequired, @@ -395,7 +396,6 @@ To change installation directory use -d/--dir flag SubCommand::with_name("completions") .settings(&[ AppSettings::DisableHelpSubcommand, - AppSettings::DisableVersion, ]).about("Generate shell completions") .long_about( "Output shell completion script to standard output. @@ -670,6 +670,10 @@ pub fn flags_from_vec( let mut argv: Vec = vec!["deno".to_string()]; let mut flags = parse_flags(&matches.clone(), None); + if flags.version { + return (flags, DenoSubcommand::Version, argv); + } + let subcommand = match matches.subcommand() { ("bundle", Some(bundle_match)) => { flags.allow_write = true; @@ -805,7 +809,6 @@ pub fn flags_from_vec( argv.extend(vec![code.to_string()]); DenoSubcommand::Xeval } - ("version", Some(_)) => DenoSubcommand::Version, (script, Some(script_match)) => { argv.extend(vec![script.to_string()]); // check if there are any extra arguments that should @@ -853,6 +856,28 @@ mod tests { ); assert_eq!(subcommand, DenoSubcommand::Version); assert_eq!(argv, svec!["deno"]); + + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "--version"]); + assert_eq!( + flags, + DenoFlags { + version: true, + ..DenoFlags::default() + } + ); + assert_eq!(subcommand, DenoSubcommand::Version); + assert_eq!(argv, svec!["deno"]); + + let (flags, subcommand, argv) = flags_from_vec(svec!["deno", "-v"]); + assert_eq!( + flags, + DenoFlags { + version: true, + ..DenoFlags::default() + } + ); + assert_eq!(subcommand, DenoSubcommand::Version); + assert_eq!(argv, svec!["deno"]); } #[test] diff --git a/tests/version_long_flag.test b/tests/version_long_flag.test new file mode 100644 index 00000000000000..b6821b4f6cf4ea --- /dev/null +++ b/tests/version_long_flag.test @@ -0,0 +1,7 @@ +args: --version +output: tests/version.out + + + +DenoFlags { log_level: None, version: true, reload: false, config_path: None, import_map_path: None, allow_read: true, read_whitelist: [], allow_write: true, write_whitelist: [], allow_net: true, net_whitelist: [], allow_env: true, allow_run: true, allow_hrtime: true, no_prompts: false, no_fetch: false, seed: None, v8_flags: None, xeval_replvar: None, xeval_delim: None } +DenoFlags { log_level: None, version: true, reload: false, config_path: None, import_map_path: None, allow_read: false, read_whitelist: [], allow_write: false, write_whitelist: [], allow_net: false, net_whitelist: [], allow_env: false, allow_run: false, allow_hrtime: false, no_prompts: false, no_fetch: false, seed: None, v8_flags: None, xeval_replvar: None, xeval_delim: None } diff --git a/tests/version_short_flag.test b/tests/version_short_flag.test new file mode 100644 index 00000000000000..2078ce874e28f1 --- /dev/null +++ b/tests/version_short_flag.test @@ -0,0 +1,2 @@ +args: -v +output: tests/version.out