-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Switch to getopts for flag parsing #1080
Conversation
Hmm it seems fetching changes for third party failed... Okay, I need to create a PR in deno_third_party for that... |
1b6dfa3
to
ce98590
Compare
Great! This looks good. I have a problem tho:
I'm not able to pass a port to |
} | ||
}; | ||
|
||
if matches.opt_present("help") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would think that there would be some way to enumerate over the values, change the opts string from foo-bar
to foo_bar
and automatically set the flags. It would have to be one less place to maintain when adding a flag then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I could look into that, though I don't think getopts
crate itself offers some easy way to do this...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you are already down this path a ways, but docopt
seems like it would be a more direct way of getting a set of flags out. See: https://docs.rs/docopt/1.0.1/docopt/#type-based-decoding
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its ArgvMap
seems nice, would look into it.
Just now I played around and made an ugly hack... (Probably not worth the reduced lines of code...):
pub fn set_flags(
args: Vec<String>,
) -> Result<(DenoFlags, Vec<String>, String), String> {
let args = v8_set_flags(args);
let mut flags = DenoFlags::default();
let mut opts = Options::new();
let matches: Option<Matches>;
{
let mut flags_tuples = vec![
("", "allow-write", "Allow file system write access.", Some(&mut flags.allow_write)),
("", "allow-net", "Allow network access.", Some(&mut flags.allow_net)),
("", "allow-env", "Allow environment access.", Some(&mut flags.allow_env)),
("", "recompile", "Force recompilation of TypeScript code.", Some(&mut flags.recompile)),
("h", "help", "Print this message.", Some(&mut flags.help)),
("D", "log-debug", "Log debug output.", Some(&mut flags.log_debug)),
("v", "version", "Print the version.", Some(&mut flags.version)),
("r", "reload", "Reload cached remote resources.", Some(&mut flags.reload)),
("", "v8-options", "Print V8 command line options.", None), // kept for generating usage string
("", "deps", "Print module dependencies.", Some(&mut flags.deps_flag)),
("", "types", "Print runtime TypeScript declarations.", Some(&mut flags.types_flag))
];
// TODO(kevinkassimo): v8_set_flags intercepts '-help' with single '-'
// Resolve that and then uncomment line below (enabling Go style -long-flag)
// opts.long_only(true);
opts.parsing_style(ParsingStyle::FloatingFrees);
for (short_flag, long_flag, flag_desc, _) in flags_tuples.iter() {
opts.optflag(&short_flag, &long_flag, &flag_desc);
}
let matches_ = match opts.parse(&args) {
Ok(m) => m,
Err(f) => {
return Err(f.to_string());
}
};
for (_, long_flag, _, maybe_flag_ref) in flags_tuples.iter_mut() {
if matches_.opt_present(long_flag) {
if let Some(flag_ref) = maybe_flag_ref {
**flag_ref = true;
}
}
}
matches = Some(matches_);
}
let rest: Vec<_> = matches.unwrap().free.iter().map(|s| s.clone()).collect();
return Ok((flags, rest, get_usage(&opts)));
}
@ry I checked backtrace (temporarily commenting out
It is complaining that Line 6 in a4fb517
would pass "4501" toLine 1123 in a4fb517
which is not a valid address, and the parsing would fail. If you try (We really need to have nice panic messages back...) |
@kevinkassimo My mistake - when I pass "127.0.0.1:4501" as the argument
it's fine. This is due to bad Addr parsing on my part. @kitsonk I like getopt - it's simpler. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - Thank you!
(Maybe I'll give up on my crusade to have single dash options.)
Don't give up 😭 I am with you. |
@ry not necessary. |
@kevinkassimo Dope. Can you comment what's need in #1014... which I will now reopen. |
Using
getopts
crate. Also use help message generated bygetopts
directly.