Skip to content

Commit c2978af

Browse files
committed
setting: adds a new setting to disable the collapsing of positional args into [ARGS] in the usage string (DontCollapseArgsInUsage)
Closes #769
1 parent 945acff commit c2978af

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

src/app/parser.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,23 @@ impl<'a, 'b> Parser<'a, 'b>
448448
count += 1;
449449
debugln!("Parser::get_args_tag:iter: {} Args not required", count);
450450
}
451-
if count > 1 || self.positionals.len() > 1 {
451+
if !self.is_set(AppSettings::DontCollapseArgsInUsage) &&
452+
(count > 1 || self.positionals.len() > 1) {
452453
return None; // [ARGS]
453454
} else if count == 1 {
454-
let p = self.positionals.values().next().expect(INTERNAL_ERROR_MSG);
455+
let p = self.positionals
456+
.values()
457+
.filter(|p| !p.is_set(ArgSettings::Required))
458+
.next()
459+
.expect(INTERNAL_ERROR_MSG);
455460
return Some(format!(" [{}]{}", p.name_no_brackets(), p.multiple_str()));
461+
} else if self.is_set(AppSettings::DontCollapseArgsInUsage) && !self.positionals.is_empty() {
462+
return Some(self.positionals
463+
.values()
464+
.filter(|p| !p.is_set(ArgSettings::Required))
465+
.map(|p| format!(" [{}]{}", p.name_no_brackets(), p.multiple_str()))
466+
.collect::<Vec<_>>()
467+
.join(""));
456468
}
457469
Some("".into())
458470
}

src/app/settings.rs

+47-30
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,37 @@ use std::str::FromStr;
44

55
bitflags! {
66
flags Flags: u32 {
7-
const SC_NEGATE_REQS = 0b000000000000000000000000000001,
8-
const SC_REQUIRED = 0b000000000000000000000000000010,
9-
const A_REQUIRED_ELSE_HELP = 0b000000000000000000000000000100,
10-
const GLOBAL_VERSION = 0b000000000000000000000000001000,
11-
const VERSIONLESS_SC = 0b000000000000000000000000010000,
12-
const UNIFIED_HELP = 0b000000000000000000000000100000,
13-
const WAIT_ON_ERROR = 0b000000000000000000000001000000,
14-
const SC_REQUIRED_ELSE_HELP= 0b000000000000000000000010000000,
15-
const NEEDS_LONG_HELP = 0b000000000000000000000100000000,
16-
const NEEDS_LONG_VERSION = 0b000000000000000000001000000000,
17-
const NEEDS_SC_HELP = 0b000000000000000000010000000000,
18-
const DISABLE_VERSION = 0b000000000000000000100000000000,
19-
const HIDDEN = 0b000000000000000001000000000000,
20-
const TRAILING_VARARG = 0b000000000000000010000000000000,
21-
const NO_BIN_NAME = 0b000000000000000100000000000000,
22-
const ALLOW_UNK_SC = 0b000000000000001000000000000000,
23-
const UTF8_STRICT = 0b000000000000010000000000000000,
24-
const UTF8_NONE = 0b000000000000100000000000000000,
25-
const LEADING_HYPHEN = 0b000000000001000000000000000000,
26-
const NO_POS_VALUES = 0b000000000010000000000000000000,
27-
const NEXT_LINE_HELP = 0b000000000100000000000000000000,
28-
const DERIVE_DISP_ORDER = 0b000000001000000000000000000000,
29-
const COLORED_HELP = 0b000000010000000000000000000000,
30-
const COLOR_ALWAYS = 0b000000100000000000000000000000,
31-
const COLOR_AUTO = 0b000001000000000000000000000000,
32-
const COLOR_NEVER = 0b000010000000000000000000000000,
33-
const DONT_DELIM_TRAIL = 0b000100000000000000000000000000,
34-
const ALLOW_NEG_NUMS = 0b001000000000000000000000000000,
35-
const LOW_INDEX_MUL_POS = 0b010000000000000000000000000000,
36-
const DISABLE_HELP_SC = 0b10000000000000000000000000000,
7+
const SC_NEGATE_REQS = 0b0000000000000000000000000000001,
8+
const SC_REQUIRED = 0b0000000000000000000000000000010,
9+
const A_REQUIRED_ELSE_HELP = 0b0000000000000000000000000000100,
10+
const GLOBAL_VERSION = 0b0000000000000000000000000001000,
11+
const VERSIONLESS_SC = 0b0000000000000000000000000010000,
12+
const UNIFIED_HELP = 0b0000000000000000000000000100000,
13+
const WAIT_ON_ERROR = 0b0000000000000000000000001000000,
14+
const SC_REQUIRED_ELSE_HELP= 0b0000000000000000000000010000000,
15+
const NEEDS_LONG_HELP = 0b0000000000000000000000100000000,
16+
const NEEDS_LONG_VERSION = 0b0000000000000000000001000000000,
17+
const NEEDS_SC_HELP = 0b0000000000000000000010000000000,
18+
const DISABLE_VERSION = 0b0000000000000000000100000000000,
19+
const HIDDEN = 0b0000000000000000001000000000000,
20+
const TRAILING_VARARG = 0b0000000000000000010000000000000,
21+
const NO_BIN_NAME = 0b0000000000000000100000000000000,
22+
const ALLOW_UNK_SC = 0b0000000000000001000000000000000,
23+
const UTF8_STRICT = 0b0000000000000010000000000000000,
24+
const UTF8_NONE = 0b0000000000000100000000000000000,
25+
const LEADING_HYPHEN = 0b0000000000001000000000000000000,
26+
const NO_POS_VALUES = 0b0000000000010000000000000000000,
27+
const NEXT_LINE_HELP = 0b0000000000100000000000000000000,
28+
const DERIVE_DISP_ORDER = 0b0000000001000000000000000000000,
29+
const COLORED_HELP = 0b0000000010000000000000000000000,
30+
const COLOR_ALWAYS = 0b0000000100000000000000000000000,
31+
const COLOR_AUTO = 0b0000001000000000000000000000000,
32+
const COLOR_NEVER = 0b0000010000000000000000000000000,
33+
const DONT_DELIM_TRAIL = 0b0000100000000000000000000000000,
34+
const ALLOW_NEG_NUMS = 0b0001000000000000000000000000000,
35+
const LOW_INDEX_MUL_POS = 0b0010000000000000000000000000000,
36+
const DISABLE_HELP_SC = 0b0100000000000000000000000000000,
37+
const DONT_COLLAPSE_ARGS = 0b1000000000000000000000000000000,
3738
}
3839
}
3940

@@ -65,6 +66,7 @@ impl AppFlags {
6566
ColorAuto => COLOR_AUTO,
6667
ColorNever => COLOR_NEVER,
6768
DontDelimitTrailingValues => DONT_DELIM_TRAIL,
69+
DontCollapseArgsInUsage => DONT_COLLAPSE_ARGS,
6870
DeriveDisplayOrder => DERIVE_DISP_ORDER,
6971
DisableHelpSubcommand => DISABLE_HELP_SC,
7072
DisableVersion => DISABLE_VERSION,
@@ -311,6 +313,18 @@ pub enum AppSettings {
311313
/// ```
312314
ColorNever,
313315

316+
/// Disables the automatic collapsing of positional args into `[ARGS]` inside the usage string
317+
///
318+
/// # Examples
319+
///
320+
/// ```no_run
321+
/// # use clap::{App, Arg, SubCommand, AppSettings};
322+
/// App::new("myprog")
323+
/// .setting(AppSettings::DontCollapseArgsInUsage)
324+
/// .get_matches();
325+
/// ```
326+
DontCollapseArgsInUsage,
327+
314328
/// Disables the automatic delimiting of values when `--` or [`AppSettings::TrailingVarArg`]
315329
/// was used.
316330
///
@@ -702,6 +716,7 @@ impl FromStr for AppSettings {
702716
"colornever" => Ok(AppSettings::ColorNever),
703717
"coloredhelp" => Ok(AppSettings::ColoredHelp),
704718
"derivedisplayorder" => Ok(AppSettings::DeriveDisplayOrder),
719+
"dontcollapseargsinusage" => Ok(AppSettings::DontCollapseArgsInUsage),
705720
"dontdelimittrailingvalues" => Ok(AppSettings::DontDelimitTrailingValues),
706721
"disablehelpsubcommand" => Ok(AppSettings::DisableHelpSubcommand),
707722
"disableversion" => Ok(AppSettings::DisableVersion),
@@ -752,6 +767,8 @@ mod test {
752767
AppSettings::DisableHelpSubcommand);
753768
assert_eq!("disableversion".parse::<AppSettings>().unwrap(),
754769
AppSettings::DisableVersion);
770+
assert_eq!("dontcollapseargsinusage".parse::<AppSettings>().unwrap(),
771+
AppSettings::DontCollapseArgsInUsage);
755772
assert_eq!("dontdelimittrailingvalues".parse::<AppSettings>().unwrap(),
756773
AppSettings::DontDelimitTrailingValues);
757774
assert_eq!("derivedisplayorder".parse::<AppSettings>().unwrap(),

0 commit comments

Comments
 (0)