mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
commit
df38346557
4 changed files with 66 additions and 33 deletions
|
@ -428,9 +428,10 @@ impl<'a, 'b> App<'a, 'b> {
|
|||
/// # ;
|
||||
/// ```
|
||||
pub fn args_from_usage(mut self, usage: &'a str) -> Self {
|
||||
for l in usage.lines() {
|
||||
if l.len() == 0 { continue; }
|
||||
self.p.add_arg(&Arg::from_usage(l.trim()));
|
||||
for line in usage.lines() {
|
||||
let l = line.trim();
|
||||
if l.is_empty() { continue; }
|
||||
self.p.add_arg(&Arg::from_usage(l));
|
||||
}
|
||||
self
|
||||
}
|
||||
|
|
|
@ -115,7 +115,7 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
|
|||
|
||||
// actually adds the arguments
|
||||
pub fn add_arg(&mut self, a: &Arg<'a, 'b>) {
|
||||
assert!(!(self.flags.iter().any(|f| &f.name == &a.name)
|
||||
debug_assert!(!(self.flags.iter().any(|f| &f.name == &a.name)
|
||||
|| self.opts.iter().any(|o| o.name == a.name)
|
||||
|| self.positionals.values().any(|p| p.name == a.name)),
|
||||
format!("Non-unique argument name: {} is already in use", a.name));
|
||||
|
@ -124,12 +124,12 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
|
|||
ag.args.push(a.name);
|
||||
}
|
||||
if let Some(s) = a.short {
|
||||
assert!(!self.short_list.contains(&s),
|
||||
debug_assert!(!self.short_list.contains(&s),
|
||||
format!("Argument short must be unique\n\n\t-{} is already in use", s));
|
||||
self.short_list.push(s);
|
||||
}
|
||||
if let Some(l) = a.long {
|
||||
assert!(!self.long_list.contains(&l),
|
||||
debug_assert!(!self.long_list.contains(&l),
|
||||
format!("Argument long must be unique\n\n\t--{} is already in use", l));
|
||||
self.long_list.push(l);
|
||||
if l == "help" {
|
||||
|
@ -147,21 +147,35 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
|
|||
} else {
|
||||
a.index.unwrap() as usize
|
||||
};
|
||||
assert!(!self.positionals.contains_key(i),
|
||||
debug_assert!(!self.positionals.contains_key(i),
|
||||
format!("Argument \"{}\" has the same index as another positional \
|
||||
argument\n\n\tPerhaps try .multiple(true) to allow one positional argument \
|
||||
to take multiple values", a.name));
|
||||
let pb = PosBuilder::from_arg(&a, i as u64, &mut self.required);
|
||||
self.positionals.insert(i, pb);
|
||||
} else if a.is_set(ArgSettings::TakesValue) {
|
||||
let ob = OptBuilder::from_arg(&a, &mut self.required);
|
||||
let mut ob = OptBuilder::from_arg(&a, &mut self.required);
|
||||
if self.settings.is_set(AppSettings::DeriveDisplayOrder) && a.disp_ord == 999 {
|
||||
ob.disp_ord = if self.settings.is_set(AppSettings::UnifiedHelpMessage) {
|
||||
self.flags.len() + self.opts.len()
|
||||
} else {
|
||||
self.opts.len()
|
||||
};
|
||||
}
|
||||
self.opts.push(ob);
|
||||
} else {
|
||||
let fb = FlagBuilder::from(a);
|
||||
let mut fb = FlagBuilder::from(a);
|
||||
if self.settings.is_set(AppSettings::DeriveDisplayOrder) && a.disp_ord == 999 {
|
||||
fb.disp_ord = if self.settings.is_set(AppSettings::UnifiedHelpMessage) {
|
||||
self.flags.len() + self.opts.len()
|
||||
} else {
|
||||
self.flags.len()
|
||||
};
|
||||
}
|
||||
self.flags.push(fb);
|
||||
}
|
||||
if a.is_set(ArgSettings::Global) {
|
||||
assert!(!a.is_set(ArgSettings::Required),
|
||||
debug_assert!(!a.is_set(ArgSettings::Required),
|
||||
format!("Global arguments cannot be required.\n\n\t'{}' is marked as global and \
|
||||
required", a.name));
|
||||
self.global_args.push(a.into());
|
||||
|
@ -210,6 +224,9 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
|
|||
subcmd = subcmd.setting(AppSettings::GlobalVersion)
|
||||
.version(self.meta.version.unwrap());
|
||||
} else { sdebugln!("No"); }
|
||||
if self.settings.is_set(AppSettings::DeriveDisplayOrder) {
|
||||
subcmd.p.meta.disp_ord = self.subcommands.len();
|
||||
}
|
||||
self.subcommands.push(subcmd);
|
||||
}
|
||||
|
||||
|
|
|
@ -3,27 +3,28 @@ use std::ascii::AsciiExt;
|
|||
|
||||
bitflags! {
|
||||
flags Flags: u32 {
|
||||
const SC_NEGATE_REQS = 0b000000000000000000001,
|
||||
const SC_REQUIRED = 0b000000000000000000010,
|
||||
const A_REQUIRED_ELSE_HELP = 0b000000000000000000100,
|
||||
const GLOBAL_VERSION = 0b000000000000000001000,
|
||||
const VERSIONLESS_SC = 0b000000000000000010000,
|
||||
const UNIFIED_HELP = 0b000000000000000100000,
|
||||
const WAIT_ON_ERROR = 0b000000000000001000000,
|
||||
const SC_REQUIRED_ELSE_HELP= 0b000000000000010000000,
|
||||
const NEEDS_LONG_HELP = 0b000000000000100000000,
|
||||
const NEEDS_LONG_VERSION = 0b000000000001000000000,
|
||||
const NEEDS_SC_HELP = 0b000000000010000000000,
|
||||
const DISABLE_VERSION = 0b000000000100000000000,
|
||||
const HIDDEN = 0b000000001000000000000,
|
||||
const TRAILING_VARARG = 0b000000010000000000000,
|
||||
const NO_BIN_NAME = 0b000000100000000000000,
|
||||
const ALLOW_UNK_SC = 0b000001000000000000000,
|
||||
const UTF8_STRICT = 0b000010000000000000000,
|
||||
const UTF8_NONE = 0b000100000000000000000,
|
||||
const LEADING_HYPHEN = 0b001000000000000000000,
|
||||
const NO_POS_VALUES = 0b010000000000000000000,
|
||||
const NEXT_LINE_HELP = 0b100000000000000000000,
|
||||
const SC_NEGATE_REQS = 0b0000000000000000000001,
|
||||
const SC_REQUIRED = 0b0000000000000000000010,
|
||||
const A_REQUIRED_ELSE_HELP = 0b0000000000000000000100,
|
||||
const GLOBAL_VERSION = 0b0000000000000000001000,
|
||||
const VERSIONLESS_SC = 0b0000000000000000010000,
|
||||
const UNIFIED_HELP = 0b0000000000000000100000,
|
||||
const WAIT_ON_ERROR = 0b0000000000000001000000,
|
||||
const SC_REQUIRED_ELSE_HELP= 0b0000000000000010000000,
|
||||
const NEEDS_LONG_HELP = 0b0000000000000100000000,
|
||||
const NEEDS_LONG_VERSION = 0b0000000000001000000000,
|
||||
const NEEDS_SC_HELP = 0b0000000000010000000000,
|
||||
const DISABLE_VERSION = 0b0000000000100000000000,
|
||||
const HIDDEN = 0b0000000001000000000000,
|
||||
const TRAILING_VARARG = 0b0000000010000000000000,
|
||||
const NO_BIN_NAME = 0b0000000100000000000000,
|
||||
const ALLOW_UNK_SC = 0b0000001000000000000000,
|
||||
const UTF8_STRICT = 0b0000010000000000000000,
|
||||
const UTF8_NONE = 0b0000100000000000000000,
|
||||
const LEADING_HYPHEN = 0b0001000000000000000000,
|
||||
const NO_POS_VALUES = 0b0010000000000000000000,
|
||||
const NEXT_LINE_HELP = 0b0100000000000000000000,
|
||||
const DERIVE_DISP_ORDER = 0b1000000000000000000000,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,7 +64,8 @@ impl AppFlags {
|
|||
AllowInvalidUtf8 => UTF8_NONE,
|
||||
AllowLeadingHyphen => LEADING_HYPHEN,
|
||||
HidePossibleValuesInHelp => NO_POS_VALUES,
|
||||
NextLineHelp => NEXT_LINE_HELP
|
||||
NextLineHelp => NEXT_LINE_HELP,
|
||||
DeriveDisplayOrder => DERIVE_DISP_ORDER
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -426,6 +428,18 @@ pub enum AppSettings {
|
|||
/// .get_matches();
|
||||
/// ```
|
||||
NextLineHelp,
|
||||
/// Displays the arguments and subcommands in the help message in the order that they were
|
||||
/// declared in, vice alphabetically which is the default.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg, SubCommand, AppSettings};
|
||||
/// App::new("myprog")
|
||||
/// .setting(AppSettings::DeriveDisplayOrder)
|
||||
/// .get_matches();
|
||||
/// ```
|
||||
DeriveDisplayOrder,
|
||||
#[doc(hidden)]
|
||||
NeedsLongVersion,
|
||||
#[doc(hidden)]
|
||||
|
@ -457,6 +471,7 @@ impl FromStr for AppSettings {
|
|||
"allowleadinghyphen" => Ok(AppSettings::AllowLeadingHyphen),
|
||||
"hidepossiblevaluesinhelp" => Ok(AppSettings::HidePossibleValuesInHelp),
|
||||
"nextlinehelp" => Ok(AppSettings::NextLineHelp),
|
||||
"derivedisplayorder" => Ok(AppSettings::DeriveDisplayOrder),
|
||||
_ => Err("unknown AppSetting, cannot convert from str".to_owned()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ impl<'a> UsageParser<'a> {
|
|||
}
|
||||
} else { break; }
|
||||
}
|
||||
assert!(!arg.name.is_empty(), format!("No name found for Arg when parsing usage string: {}", self.usage));
|
||||
debug_assert!(!arg.name.is_empty(), format!("No name found for Arg when parsing usage string: {}", self.usage));
|
||||
let n_vals = if let Some(ref v) = arg.val_names { v.len() } else { 0 };
|
||||
if n_vals > 1 {
|
||||
arg.num_vals = Some(n_vals as u64);
|
||||
|
|
Loading…
Reference in a new issue