-
-
Save ry/158e4d3ca54a57f4926bd0a39fd5a844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 759903d1846f9852a9195df5cb9062df793ee452 Mon Sep 17 00:00:00 2001 | |
From: Ryan Dahl <ry@tinyclouds.org> | |
Date: Thu, 15 Nov 2018 12:12:17 -0500 | |
Subject: [PATCH] Improve flag parsing | |
--- | |
src/flags.rs | 119 +++++++++++++++++++++++++++++++++++------------------------ | |
1 file changed, 70 insertions(+), 49 deletions(-) | |
diff --git a/src/flags.rs b/src/flags.rs | |
index 16ee2f69..c12cf1a9 100644 | |
--- a/src/flags.rs | |
+++ b/src/flags.rs | |
@@ -1,4 +1,5 @@ | |
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | |
+use getopts; | |
use getopts::Options; | |
use libc::c_int; | |
use libdeno; | |
@@ -37,6 +38,62 @@ Environment variables: | |
) | |
} | |
+fn parse_skip_unrecognized( | |
+ opts: &Options, | |
+ flags: &mut DenoFlags, | |
+ args: Vec<String>, | |
+) -> Result<Vec<String>, getopts::Fail> { | |
+ let mut rest = Vec::<String>::new(); | |
+ for i in 0..args.len() { | |
+ let fake_args = vec![args[i].clone()]; | |
+ match opts.parse(fake_args) { | |
+ Err(getopts::Fail::UnrecognizedOption(_)) => { | |
+ rest.push(args[i].clone()); | |
+ } | |
+ Err(e) => { | |
+ return Err(e); | |
+ } | |
+ Ok(matches) => { | |
+ if matches.opt_present("help") { | |
+ flags.help = true; | |
+ } | |
+ if matches.opt_present("log-debug") { | |
+ flags.log_debug = true; | |
+ } | |
+ if matches.opt_present("version") { | |
+ flags.version = true; | |
+ } | |
+ if matches.opt_present("reload") { | |
+ flags.reload = true; | |
+ } | |
+ if matches.opt_present("recompile") { | |
+ flags.recompile = true; | |
+ } | |
+ if matches.opt_present("allow-write") { | |
+ flags.allow_write = true; | |
+ } | |
+ if matches.opt_present("allow-net") { | |
+ flags.allow_net = true; | |
+ } | |
+ if matches.opt_present("allow-env") { | |
+ flags.allow_env = true; | |
+ } | |
+ if matches.opt_present("allow-run") { | |
+ flags.allow_run = true; | |
+ } | |
+ if matches.opt_present("types") { | |
+ flags.types = true; | |
+ } | |
+ | |
+ if matches.free.len() > 0 { | |
+ rest.extend(matches.free); | |
+ } | |
+ } | |
+ } | |
+ } | |
+ Ok(rest) | |
+} | |
+ | |
// Parses flags for deno. This does not do v8_set_flags() - call that separately. | |
#[cfg_attr(feature = "cargo-clippy", allow(stutter))] | |
pub fn set_flags( | |
@@ -62,45 +119,8 @@ pub fn set_flags( | |
let mut flags = DenoFlags::default(); | |
- let matches = match opts.parse(&args) { | |
- Ok(m) => m, | |
- Err(f) => { | |
- return Err(f.to_string()); | |
- } | |
- }; | |
- | |
- if matches.opt_present("help") { | |
- flags.help = true; | |
- } | |
- if matches.opt_present("log-debug") { | |
- flags.log_debug = true; | |
- } | |
- if matches.opt_present("version") { | |
- flags.version = true; | |
- } | |
- if matches.opt_present("reload") { | |
- flags.reload = true; | |
- } | |
- if matches.opt_present("recompile") { | |
- flags.recompile = true; | |
- } | |
- if matches.opt_present("allow-write") { | |
- flags.allow_write = true; | |
- } | |
- if matches.opt_present("allow-net") { | |
- flags.allow_net = true; | |
- } | |
- if matches.opt_present("allow-env") { | |
- flags.allow_env = true; | |
- } | |
- if matches.opt_present("allow-run") { | |
- flags.allow_run = true; | |
- } | |
- if matches.opt_present("types") { | |
- flags.types = true; | |
- } | |
- | |
- let rest: Vec<_> = matches.free.to_vec(); | |
+ let rest = parse_skip_unrecognized(&opts, &mut flags, args) | |
+ .map_err(|e| e.to_string())?; | |
Ok((flags, rest, get_usage(&opts))) | |
} | |
@@ -177,16 +197,17 @@ fn test_set_flags_5() { | |
} | |
#[test] | |
-fn test_set_bad_flags_1() { | |
- let err = set_flags(svec!["deno", "--unknown-flag"]).unwrap_err(); | |
- assert_eq!(err, "Unrecognized option: 'unknown-flag'"); | |
-} | |
- | |
-#[test] | |
-fn test_set_bad_flags_2() { | |
- // This needs to be changed if -z is added as a flag | |
- let err = set_flags(svec!["deno", "-z"]).unwrap_err(); | |
- assert_eq!(err, "Unrecognized option: 'z'"); | |
+fn test_set_flags_6() { | |
+ let (flags, rest, _) = | |
+ set_flags(svec!["deno", "gist.ts", "--title", "X", "--allow-net"]).unwrap(); | |
+ assert_eq!(rest, svec!["deno", "gist.ts", "--title", "X"]); | |
+ assert_eq!( | |
+ flags, | |
+ DenoFlags { | |
+ allow_net: true, | |
+ ..DenoFlags::default() | |
+ } | |
+ ) | |
} | |
// Returns args passed to V8, followed by args passed to JS | |
-- | |
2.15.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment