Allow both dashes and underscores for Z flags

This makes it a little easier to match whatever
the natural formatting is for where you're setting
unstable options -- CLI or config file.
This commit is contained in:
Alex Berghage 2020-06-21 14:18:44 -06:00
parent 4aede8c784
commit 9cb1ef88b6
3 changed files with 25 additions and 2 deletions

View File

@ -429,7 +429,9 @@ impl CliUnstable {
Ok(true)
};
match k {
// Permit dashes or underscores in parsing these
let normalized_key = k.replace("_", "-");
match normalized_key.as_str() {
"print-im-a-teapot" => self.print_im_a_teapot = parse_bool(k, v)?,
"unstable-options" => self.unstable_options = parse_empty(k, v)?,
"no-index-update" => self.no_index_update = parse_empty(k, v)?,

View File

@ -2,7 +2,8 @@
Experimental Cargo features are only available on the nightly channel. You
typically use one of the `-Z` flags to enable them. Run `cargo -Z help` to
see a list of flags available.
see a list of flags available. All Z flags accept either dashes or underscores
as separators.
`-Z unstable-options` is a generic flag for enabling other unstable
command-line flags. Options requiring this will be called out below.

View File

@ -267,6 +267,26 @@ fn rerooted_remains() {
assert_eq!(config.get::<String>("c").unwrap(), "cli2");
}
#[cargo_test]
fn unstable_dash_underscore_interchangable() {
// Confirm unstable flag parsing treats underscores and dashes
// interchangably when coming from the CLI.
let config = ConfigBuilder::new()
.unstable_flag("print_im_a_teapot")
.build();
assert_eq!(config.cli_unstable().print_im_a_teapot, true);
let config = ConfigBuilder::new()
.unstable_flag("print-im-a-teapot")
.build();
assert_eq!(config.cli_unstable().print_im_a_teapot, true);
let config = ConfigBuilder::new()
.unstable_flag("print_im-a_teapot")
.build();
assert_eq!(config.cli_unstable().print_im_a_teapot, true);
}
#[cargo_test]
fn bad_parse() {
// Fail to TOML parse.