Skip to content

Commit

Permalink
Merge pull request #2362 from ldm0/demangle
Browse files Browse the repository at this point in the history
Demangle interlinking flags
  • Loading branch information
pksunkara authored Mar 9, 2021
2 parents 6361249 + 0b1d137 commit d53f192
Show file tree
Hide file tree
Showing 35 changed files with 350 additions and 112 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ fn main() {
.arg(Arg::new("v")
.short('v')
.multiple(true)
.takes_value(true)
.about("Sets the level of verbosity"))
.subcommand(App::new("test")
.about("controls testing features")
Expand Down
6 changes: 6 additions & 0 deletions benches/03_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn build_from_builder(c: &mut Criterion) {
.about("tests options")
.short('o')
.long("option")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences),
)
Expand Down Expand Up @@ -99,6 +100,7 @@ pub fn build_from_builder(c: &mut Criterion) {
)
.arg(
Arg::new("positional3")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences)
.about("tests positionals with specific values")
Expand All @@ -114,6 +116,7 @@ pub fn build_from_builder(c: &mut Criterion) {
.arg(
Arg::new("multvalsmo")
.long("multvalsmo")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences)
.about("Tests multiple values, not mult occs")
Expand All @@ -122,6 +125,7 @@ pub fn build_from_builder(c: &mut Criterion) {
.arg(
Arg::new("minvals")
.long("minvals2")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences)
.about("Tests 2 min vals")
Expand All @@ -130,6 +134,7 @@ pub fn build_from_builder(c: &mut Criterion) {
.arg(
Arg::new("maxvals")
.long("maxvals3")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences)
.about("Tests 3 max vals")
Expand All @@ -144,6 +149,7 @@ pub fn build_from_builder(c: &mut Criterion) {
Arg::new("scoption")
.short('o')
.long("option")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences)
.about("tests options"),
Expand Down
2 changes: 2 additions & 0 deletions benches/04_new_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn app_example7<'c>() -> App<'c> {
.arg(
Arg::new("input")
.about("the input file to use")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences)
.setting(ArgSettings::Required)
Expand All @@ -129,6 +130,7 @@ fn app_example8<'c>() -> App<'c> {
.arg(
Arg::new("input")
.about("the input file to use")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences)
.setting(ArgSettings::Required)
Expand Down
2 changes: 2 additions & 0 deletions benches/05_ripgrep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ where
]))
.arg(
arg("path")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences),
)
Expand All @@ -350,6 +351,7 @@ where
.arg(
flag("color")
.value_name("WHEN")
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::HidePossibleValues)
.possible_values(&["never", "auto", "always", "ansi"]),
)
Expand Down
1 change: 1 addition & 0 deletions benches/06_rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ fn build_cli() -> App<'static> {
.arg(
Arg::new("command")
.setting(ArgSettings::Required)
.setting(ArgSettings::TakesValue)
.setting(ArgSettings::MultipleValues)
.setting(ArgSettings::MultipleOccurrences),
),
Expand Down
1 change: 1 addition & 0 deletions clap_generate/examples/value_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fn build_cli() -> App<'static> {
)
.arg(
Arg::new("command_with_args")
.takes_value(true)
.multiple_values(true)
.value_hint(ValueHint::CommandWithArguments),
)
Expand Down
1 change: 1 addition & 0 deletions clap_generate/tests/value_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn build_app_with_value_hints() -> App<'static> {
)
.arg(
Arg::new("command_with_args")
.takes_value(true)
.multiple_values(true)
.value_hint(ValueHint::CommandWithArguments),
)
Expand Down
1 change: 1 addition & 0 deletions examples/05_flag_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn main() {
.about("turns up the awesome") // Displayed when showing help info
.short('a') // Trigger this arg with "-a"
.long("awesome") // Trigger this arg with "--awesome"
.takes_value(true)
.multiple(true) // This flag should allow multiple
// occurrences such as "-aaa" or "-a -a"
.requires("config") // Says, "If the user uses -a, they MUST
Expand Down
1 change: 1 addition & 0 deletions examples/17_yaml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ args:
help: demo flag argument
short: F
multiple: true
takes_value: true
global: true
# Conflicts, mutual overrides, and requirements can all be defined as a
# list, where the key is the name of the other argument
Expand Down
2 changes: 1 addition & 1 deletion examples/22_stop_parsing_with_--.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
let matches = App::new("myprog")
.arg(Arg::new("eff").short('f'))
.arg(Arg::new("pea").short('p').takes_value(true))
.arg(Arg::new("slop").multiple(true).last(true))
.arg(Arg::new("slop").takes_value(true).multiple(true).last(true))
.get_matches();

println!("-f used: {:?}", matches.is_present("eff"));
Expand Down
2 changes: 2 additions & 0 deletions examples/23_flag_subcommands_pacman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn main() {
.long("search")
.about("search locally installed packages for matching strings")
.conflicts_with("info")
.takes_value(true)
.multiple_values(true),
)
.arg(
Expand All @@ -48,6 +49,7 @@ fn main() {
.short('i')
.conflicts_with("search")
.about("view package information")
.takes_value(true)
.multiple_values(true),
),
)
Expand Down
4 changes: 2 additions & 2 deletions src/build/app/debug_asserts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{App, AppSettings, ArgSettings, ValueHint};
use crate::{build::arg::debug_asserts::assert_arg, App, AppSettings, ArgSettings, ValueHint};
use std::cmp::Ordering;

#[derive(Eq)]
Expand Down Expand Up @@ -63,7 +63,7 @@ pub(crate) fn assert_app(app: &App) {
}

for arg in app.args.args() {
arg._debug_asserts();
assert_arg(arg);

if let Some(s) = arg.short.as_ref() {
short_flags.push(Flag::Arg(format!("-{}", s), &*arg.name));
Expand Down
4 changes: 2 additions & 2 deletions src/build/app/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ pub enum AppSettings {
/// .setting(AppSettings::AllowMissingPositional)
/// .arg(Arg::new("foo"))
/// .arg(Arg::new("bar"))
/// .arg(Arg::new("baz").multiple(true))
/// .arg(Arg::new("baz").takes_value(true).multiple(true))
/// .get_matches_from(vec![
/// "prog", "foo", "bar", "baz1", "baz2", "baz3"
/// ]);
Expand All @@ -348,7 +348,7 @@ pub enum AppSettings {
/// .setting(AppSettings::AllowMissingPositional)
/// .arg(Arg::new("foo"))
/// .arg(Arg::new("bar"))
/// .arg(Arg::new("baz").multiple(true))
/// .arg(Arg::new("baz").takes_value(true).multiple(true))
/// .get_matches_from(vec![
/// "prog", "--", "baz1", "baz2", "baz3"
/// ]);
Expand Down
81 changes: 81 additions & 0 deletions src/build/arg/debug_asserts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::{Arg, ArgSettings, ValueHint};

pub(crate) fn assert_arg(arg: &Arg) {
debug!("Arg::_debug_asserts:{}", arg.name);

// Self conflict
// TODO: this check should be recursive
assert!(
!arg.blacklist.iter().any(|x| *x == arg.id),
"Argument '{}' cannot conflict with itself",
arg.name,
);

if arg.value_hint != ValueHint::Unknown {
assert!(
arg.is_set(ArgSettings::TakesValue),
"Argument '{}' has value hint but takes no value",
arg.name
);

if arg.value_hint == ValueHint::CommandWithArguments {
assert!(
arg.is_set(ArgSettings::MultipleValues),
"Argument '{}' uses hint CommandWithArguments and must accept multiple values",
arg.name
)
}
}

if arg.index.is_some() {
assert!(
arg.short.is_none() && arg.long.is_none(),
"Argument '{}' is a positional argument and can't have short or long name versions",
arg.name
);
}

if arg.is_set(ArgSettings::Required) {
assert!(
arg.default_vals.is_empty(),
"Argument '{}' is required and can't have a default value",
arg.name
);
}

assert_app_flags(arg);
}

fn assert_app_flags(arg: &Arg) {
use ArgSettings::*;

macro_rules! checker {
($a:ident requires $($b:ident)|+) => {
if arg.is_set($a) {
let mut s = String::new();

$(
if !arg.is_set($b) {
s.push_str(&format!("\nArgSettings::{} is required when ArgSettings::{} is set.\n", std::stringify!($b), std::stringify!($a)));
}
)+

if !s.is_empty() {
panic!("{}", s)
}
}
}
}

checker!(AllowEmptyValues requires TakesValue);
checker!(RequireDelimiter requires TakesValue | UseValueDelimiter);
checker!(HidePossibleValues requires TakesValue);
checker!(AllowHyphenValues requires TakesValue);
checker!(RequireEquals requires TakesValue);
checker!(Last requires TakesValue);
checker!(HideDefaultValue requires TakesValue);
checker!(MultipleValues requires TakesValue);
checker!(HideEnv requires TakesValue);
checker!(HideEnvValues requires TakesValue);
checker!(IgnoreCase requires TakesValue);
}
Loading

0 comments on commit d53f192

Please sign in to comment.