Skip to content

Commit

Permalink
handle deno -v and deno --version
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Jul 26, 2019
1 parent 3ae8089 commit 04d2b27
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
53 changes: 39 additions & 14 deletions cli/flags.rs
Expand Up @@ -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.
Expand All @@ -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")
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -670,6 +670,10 @@ pub fn flags_from_vec(
let mut argv: Vec<String> = 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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions 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 }
2 changes: 2 additions & 0 deletions tests/version_short_flag.test
@@ -0,0 +1,2 @@
args: -v
output: tests/version.out

0 comments on commit 04d2b27

Please sign in to comment.