mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
Rewrite flags.rs::parse_flags (#2237)
This commit is contained in:
parent
1a0f53a807
commit
636827a1d5
2 changed files with 180 additions and 88 deletions
203
cli/flags.rs
203
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]
|
||||
|
|
65
cli/main.rs
65
cli/main.rs
|
@ -43,6 +43,7 @@ use crate::worker::root_specifier_to_url;
|
|||
use crate::worker::Worker;
|
||||
use deno::v8_set_flags;
|
||||
use flags::DenoFlags;
|
||||
use flags::DenoSubcommand;
|
||||
use futures::lazy;
|
||||
use futures::Future;
|
||||
use log::{LevelFilter, Metadata, Record};
|
||||
|
@ -248,23 +249,13 @@ fn run_script(flags: DenoFlags, argv: Vec<String>) {
|
|||
tokio_util::run(main_future);
|
||||
}
|
||||
|
||||
fn fmt_command(mut flags: DenoFlags, mut argv: Vec<String>) {
|
||||
argv.insert(1, "https://deno.land/std/prettier/main.ts".to_string());
|
||||
flags.allow_read = true;
|
||||
flags.allow_write = true;
|
||||
run_script(flags, argv);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[cfg(windows)]
|
||||
ansi_term::enable_ansi_support().ok(); // For Windows 10
|
||||
|
||||
log::set_logger(&LOGGER).unwrap();
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let cli_app = flags::create_cli_app();
|
||||
let matches = cli_app.get_matches_from(args);
|
||||
let flags = flags::parse_flags(matches.clone());
|
||||
let mut argv: Vec<String> = vec!["deno".to_string()];
|
||||
let (flags, subcommand, argv) = flags::flags_from_vec(args);
|
||||
|
||||
if flags.v8_help {
|
||||
// show v8 help and exit
|
||||
|
@ -284,50 +275,12 @@ fn main() {
|
|||
LevelFilter::Warn
|
||||
});
|
||||
|
||||
match matches.subcommand() {
|
||||
("types", Some(_)) => {
|
||||
types_command();
|
||||
}
|
||||
("eval", Some(eval_match)) => {
|
||||
let code: &str = eval_match.value_of("code").unwrap();
|
||||
argv.extend(vec![code.to_string()]);
|
||||
eval_command(flags, argv);
|
||||
}
|
||||
("info", Some(info_match)) => {
|
||||
let file: &str = info_match.value_of("file").unwrap();
|
||||
argv.extend(vec![file.to_string()]);
|
||||
fetch_or_info_command(flags, argv, true);
|
||||
}
|
||||
("fetch", Some(fetch_match)) => {
|
||||
let file: &str = fetch_match.value_of("file").unwrap();
|
||||
argv.extend(vec![file.to_string()]);
|
||||
fetch_or_info_command(flags, argv, false);
|
||||
}
|
||||
("fmt", Some(fmt_match)) => {
|
||||
let files: Vec<String> = fmt_match
|
||||
.values_of("files")
|
||||
.unwrap()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
argv.extend(files);
|
||||
fmt_command(flags, argv);
|
||||
}
|
||||
(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);
|
||||
}
|
||||
run_script(flags, argv);
|
||||
}
|
||||
_ => {
|
||||
run_repl(flags, argv);
|
||||
}
|
||||
match subcommand {
|
||||
DenoSubcommand::Eval => eval_command(flags, argv),
|
||||
DenoSubcommand::Fetch => fetch_or_info_command(flags, argv, false),
|
||||
DenoSubcommand::Info => fetch_or_info_command(flags, argv, true),
|
||||
DenoSubcommand::Repl => run_repl(flags, argv),
|
||||
DenoSubcommand::Run => run_script(flags, argv),
|
||||
DenoSubcommand::Types => types_command(),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue