0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

Better flag parsing

This commit is contained in:
Ian Shehadeh 2018-08-24 15:26:40 -04:00 committed by Ryan Dahl
parent d6c310596e
commit 827f489c5a

View file

@ -4,6 +4,7 @@ use libc::c_int;
use std::ffi::CStr;
use std::ffi::CString;
use std::mem;
use std::vec::Vec;
// Creates vector of strings, Vec<String>
#[cfg(test)]
@ -32,7 +33,7 @@ pub fn print_usage() {
-v or --version Print the version.
-r or --reload Reload cached remote resources.
-D or --log-debug Log debug output.
--help Print this message.
-h or --help Print this message.
--v8-options Print V8 command line options."
);
}
@ -41,19 +42,39 @@ pub fn print_usage() {
pub fn set_flags(args: Vec<String>) -> (DenoFlags, Vec<String>) {
let mut flags = DenoFlags::default();
let mut rest = Vec::new();
for a in &args {
match a.as_str() {
"-h" | "--help" => flags.help = true,
"-D" | "--log-debug" => flags.log_debug = true,
"-v" | "--version" => flags.version = true,
"-r" | "--reload" => flags.reload = true,
"--allow-env" => flags.allow_env = true,
"--allow-write" => flags.allow_write = true,
"--allow-net" => flags.allow_net = true,
_ => rest.push(a.clone()),
let mut arg_iter = args.iter();
while let Some(a) = arg_iter.next() {
if a.len() > 1 && &a[0..2] == "--" {
match a.as_str() {
"--help" => flags.help = true,
"--log-debug" => flags.log_debug = true,
"--version" => flags.version = true,
"--reload" => flags.reload = true,
"--allow-write" => flags.allow_write = true,
"--allow-net" => flags.allow_net = true,
"--allow-env" => flags.allow_env = true,
"--" => break,
_ => unimplemented!(),
}
} else if a.len() > 1 && &a[0..1] == "-" {
let mut iter = a.chars().skip(1); // skip the "-"
while let Some(f) = iter.next() {
match f {
'h' => flags.help = true,
'D' => flags.log_debug = true,
'v' => flags.version = true,
'r' => flags.reload = true,
_ => unimplemented!(),
}
}
} else {
rest.push(a.clone());
}
}
// add any remaining arguments to `rest`
rest.extend(arg_iter.map(|s| s.clone()));
return (flags, rest);
}
@ -99,6 +120,22 @@ fn test_set_flags_3() {
);
}
#[test]
fn test_set_flags_4() {
let (flags, rest) =
set_flags(svec!["deno", "-Dr", "script.ts", "--allow-write"]);
assert_eq!(rest, svec!["deno", "script.ts"]);
assert_eq!(
flags,
DenoFlags {
log_debug: true,
reload: true,
allow_write: true,
..DenoFlags::default()
}
);
}
// Returns args passed to V8, followed by args passed to JS
// TODO Rename to v8_set_flags_preprocess
fn parse_core_args(args: Vec<String>) -> (Vec<String>, Vec<String>) {