2016-09-05 19:29:40 +00:00
|
|
|
// Std
|
2016-09-05 21:03:45 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap, VecDeque};
|
|
|
|
use std::ffi::{OsStr, OsString};
|
|
|
|
use std::fmt::Display;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, BufWriter, Write};
|
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::slice::Iter;
|
2016-11-01 20:10:40 +00:00
|
|
|
use std::iter::Peekable;
|
2016-01-11 08:59:56 +00:00
|
|
|
|
2016-09-05 21:03:45 +00:00
|
|
|
// Third Party
|
|
|
|
use vec_map::{self, VecMap};
|
2016-01-11 08:59:56 +00:00
|
|
|
|
2016-09-05 19:29:40 +00:00
|
|
|
// Internal
|
|
|
|
use INTERNAL_ERROR_MSG;
|
|
|
|
use INVALID_UTF8;
|
|
|
|
use SubCommand;
|
2016-01-11 08:59:56 +00:00
|
|
|
use app::App;
|
2016-09-05 19:29:40 +00:00
|
|
|
use app::help::Help;
|
|
|
|
use app::meta::AppMeta;
|
2016-05-06 21:35:53 +00:00
|
|
|
use app::settings::{AppFlags, AppSettings};
|
2016-01-11 08:59:56 +00:00
|
|
|
use args::{AnyArg, ArgMatcher};
|
2016-09-05 19:29:40 +00:00
|
|
|
use args::{Arg, ArgGroup, FlagBuilder, OptBuilder, PosBuilder};
|
|
|
|
use args::MatchedArg;
|
2016-03-09 22:41:02 +00:00
|
|
|
use args::settings::ArgSettings;
|
2016-09-05 19:29:40 +00:00
|
|
|
use completions::ComplGen;
|
2016-05-06 21:35:53 +00:00
|
|
|
use errors::{Error, ErrorKind};
|
2016-01-11 08:59:56 +00:00
|
|
|
use errors::Result as ClapResult;
|
2016-05-30 08:07:44 +00:00
|
|
|
use fmt::{Format, ColorWhen};
|
2016-01-11 08:59:56 +00:00
|
|
|
use osstringext::OsStrExt2;
|
2016-10-23 00:36:15 +00:00
|
|
|
use completions::Shell;
|
2016-09-05 19:29:40 +00:00
|
|
|
use suggestions;
|
|
|
|
|
2016-02-21 16:44:45 +00:00
|
|
|
#[allow(missing_debug_implementations)]
|
2016-01-26 19:02:10 +00:00
|
|
|
#[doc(hidden)]
|
2016-05-06 21:35:53 +00:00
|
|
|
pub struct Parser<'a, 'b>
|
|
|
|
where 'a: 'b
|
|
|
|
{
|
2016-01-11 08:59:56 +00:00
|
|
|
required: Vec<&'b str>,
|
2016-06-29 03:38:22 +00:00
|
|
|
pub short_list: Vec<char>,
|
|
|
|
pub long_list: Vec<&'b str>,
|
2016-01-11 08:59:56 +00:00
|
|
|
blacklist: Vec<&'b str>,
|
|
|
|
// A list of possible flags
|
2016-07-11 02:50:45 +00:00
|
|
|
pub flags: Vec<FlagBuilder<'a, 'b>>,
|
2016-01-11 08:59:56 +00:00
|
|
|
// A list of possible options
|
2016-06-29 03:38:22 +00:00
|
|
|
pub opts: Vec<OptBuilder<'a, 'b>>,
|
2016-01-11 08:59:56 +00:00
|
|
|
// A list of positional arguments
|
2016-06-29 03:38:22 +00:00
|
|
|
pub positionals: VecMap<PosBuilder<'a, 'b>>,
|
2016-01-11 08:59:56 +00:00
|
|
|
// A list of subcommands
|
2016-02-04 16:54:46 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub subcommands: Vec<App<'a, 'b>>,
|
2016-01-22 04:18:52 +00:00
|
|
|
groups: HashMap<&'a str, ArgGroup<'a>>,
|
2016-01-11 08:59:56 +00:00
|
|
|
global_args: Vec<Arg<'a, 'b>>,
|
|
|
|
overrides: Vec<&'b str>,
|
|
|
|
help_short: Option<char>,
|
|
|
|
version_short: Option<char>,
|
|
|
|
settings: AppFlags,
|
2016-06-04 02:44:58 +00:00
|
|
|
pub g_settings: Vec<AppSettings>,
|
2016-01-11 08:59:56 +00:00
|
|
|
pub meta: AppMeta<'b>,
|
2016-06-05 05:09:07 +00:00
|
|
|
trailing_vals: bool,
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> Default for Parser<'a, 'b> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Parser {
|
|
|
|
flags: vec![],
|
|
|
|
opts: vec![],
|
|
|
|
positionals: VecMap::new(),
|
|
|
|
subcommands: vec![],
|
|
|
|
help_short: None,
|
|
|
|
version_short: None,
|
|
|
|
required: vec![],
|
|
|
|
short_list: vec![],
|
|
|
|
long_list: vec![],
|
|
|
|
blacklist: vec![],
|
|
|
|
groups: HashMap::new(),
|
|
|
|
global_args: vec![],
|
|
|
|
overrides: vec![],
|
2016-06-04 02:44:58 +00:00
|
|
|
g_settings: vec![],
|
2016-01-11 08:59:56 +00:00
|
|
|
settings: AppFlags::new(),
|
|
|
|
meta: AppMeta::new(),
|
2016-06-05 05:09:07 +00:00
|
|
|
trailing_vals: false,
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 21:35:53 +00:00
|
|
|
impl<'a, 'b> Parser<'a, 'b>
|
|
|
|
where 'a: 'b
|
|
|
|
{
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn with_name(n: String) -> Self {
|
|
|
|
Parser { meta: AppMeta::with_name(n), ..Default::default() }
|
|
|
|
}
|
|
|
|
|
2016-01-23 11:46:16 +00:00
|
|
|
pub fn help_short(&mut self, s: &str) {
|
2016-01-11 08:59:56 +00:00
|
|
|
self.help_short = s.trim_left_matches(|c| c == '-')
|
2016-09-05 19:29:40 +00:00
|
|
|
.chars()
|
|
|
|
.nth(0);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn version_short(&mut self, s: &str) {
|
|
|
|
self.version_short = s.trim_left_matches(|c| c == '-')
|
2016-09-05 19:29:40 +00:00
|
|
|
.chars()
|
|
|
|
.nth(0);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 08:53:28 +00:00
|
|
|
pub fn gen_completions_to<W: Write>(&mut self, for_shell: Shell, buf: &mut W) {
|
|
|
|
|
2016-06-29 03:38:22 +00:00
|
|
|
self.propogate_help_version();
|
|
|
|
self.build_bin_names();
|
2016-07-05 08:53:28 +00:00
|
|
|
|
|
|
|
ComplGen::new(self).generate(for_shell, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen_completions(&mut self, for_shell: Shell, od: OsString) {
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
let out_dir = PathBuf::from(od);
|
2016-10-23 23:32:52 +00:00
|
|
|
let name = &*self.meta.bin_name.as_ref().unwrap().clone();
|
|
|
|
let file_name = match for_shell {
|
2016-11-01 20:10:40 +00:00
|
|
|
|
2016-10-23 23:32:52 +00:00
|
|
|
Shell::Bash => format!("{}.bash-completion", name),
|
|
|
|
Shell::Fish => format!("{}.fish", name),
|
2016-11-01 20:10:40 +00:00
|
|
|
Shell::Zsh => format!("_{}", name),
|
2016-07-11 02:50:45 +00:00
|
|
|
};
|
2016-07-05 08:53:28 +00:00
|
|
|
|
2016-10-23 23:32:52 +00:00
|
|
|
let mut file = match File::create(out_dir.join(file_name)) {
|
2016-11-01 20:10:40 +00:00
|
|
|
Err(why) => panic!("couldn't create completion file: {}", why.description()),
|
2016-07-05 08:53:28 +00:00
|
|
|
Ok(file) => file,
|
|
|
|
};
|
|
|
|
self.gen_completions_to(for_shell, &mut file)
|
2016-06-29 03:38:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
// actually adds the arguments
|
|
|
|
pub fn add_arg(&mut self, a: &Arg<'a, 'b>) {
|
2016-05-06 21:35:53 +00:00
|
|
|
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));
|
2016-06-24 15:50:37 +00:00
|
|
|
if let Some(ref grps) = a.group {
|
|
|
|
for g in grps {
|
2016-09-05 19:29:40 +00:00
|
|
|
let ag = self.groups.entry(g).or_insert_with(|| ArgGroup::with_name(g));
|
|
|
|
ag.args.push(a.name);
|
2016-06-24 15:50:37 +00:00
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
if let Some(s) = a.short {
|
2016-03-10 21:33:39 +00:00
|
|
|
debug_assert!(!self.short_list.contains(&s),
|
2016-05-06 21:35:53 +00:00
|
|
|
format!("Argument short must be unique\n\n\t-{} is already in use",
|
|
|
|
s));
|
2016-01-26 19:02:10 +00:00
|
|
|
self.short_list.push(s);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
if let Some(l) = a.long {
|
2016-03-10 21:33:39 +00:00
|
|
|
debug_assert!(!self.long_list.contains(&l),
|
2016-05-06 21:35:53 +00:00
|
|
|
format!("Argument long must be unique\n\n\t--{} is already in use",
|
|
|
|
l));
|
2016-01-26 19:02:10 +00:00
|
|
|
self.long_list.push(l);
|
2016-01-11 08:59:56 +00:00
|
|
|
if l == "help" {
|
2016-05-30 10:12:21 +00:00
|
|
|
self.unset(AppSettings::NeedsLongHelp);
|
2016-01-11 08:59:56 +00:00
|
|
|
} else if l == "version" {
|
2016-05-30 10:12:21 +00:00
|
|
|
self.unset(AppSettings::NeedsLongVersion);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-25 20:45:50 +00:00
|
|
|
if a.is_set(ArgSettings::Required) {
|
2016-01-11 08:59:56 +00:00
|
|
|
self.required.push(a.name);
|
|
|
|
}
|
|
|
|
if a.index.is_some() || (a.short.is_none() && a.long.is_none()) {
|
|
|
|
let i = if a.index.is_none() {
|
|
|
|
(self.positionals.len() + 1)
|
|
|
|
} else {
|
|
|
|
a.index.unwrap() as usize
|
|
|
|
};
|
2016-03-10 21:33:39 +00:00
|
|
|
debug_assert!(!self.positionals.contains_key(i),
|
2016-05-06 21:35:53 +00:00
|
|
|
format!("Argument \"{}\" has the same index as another positional \
|
2016-01-11 08:59:56 +00:00
|
|
|
argument\n\n\tPerhaps try .multiple(true) to allow one positional argument \
|
2016-05-06 21:35:53 +00:00
|
|
|
to take multiple values",
|
|
|
|
a.name));
|
2016-06-08 04:10:56 +00:00
|
|
|
let pb = PosBuilder::from_arg(a, i as u64, &mut self.required);
|
2016-01-11 08:59:56 +00:00
|
|
|
self.positionals.insert(i, pb);
|
2016-01-25 20:45:50 +00:00
|
|
|
} else if a.is_set(ArgSettings::TakesValue) {
|
2016-06-08 04:10:56 +00:00
|
|
|
let mut ob = OptBuilder::from_arg(a, &mut self.required);
|
2016-10-25 20:50:29 +00:00
|
|
|
ob.unified_ord = self.flags.len() + self.opts.len();
|
2016-01-11 08:59:56 +00:00
|
|
|
self.opts.push(ob);
|
|
|
|
} else {
|
2016-03-10 21:31:59 +00:00
|
|
|
let mut fb = FlagBuilder::from(a);
|
2016-10-25 20:50:29 +00:00
|
|
|
fb.unified_ord = self.flags.len() + self.opts.len();
|
2016-01-11 08:59:56 +00:00
|
|
|
self.flags.push(fb);
|
|
|
|
}
|
2016-01-25 20:45:50 +00:00
|
|
|
if a.is_set(ArgSettings::Global) {
|
2016-03-10 21:33:39 +00:00
|
|
|
debug_assert!(!a.is_set(ArgSettings::Required),
|
2016-05-06 21:35:53 +00:00
|
|
|
format!("Global arguments cannot be required.\n\n\t'{}' is marked as \
|
|
|
|
global and required",
|
|
|
|
a.name));
|
2016-01-11 08:59:56 +00:00
|
|
|
self.global_args.push(a.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_group(&mut self, group: ArgGroup<'a>) {
|
|
|
|
if group.required {
|
|
|
|
self.required.push(group.name.into());
|
|
|
|
if let Some(ref reqs) = group.requires {
|
2016-02-14 06:10:44 +00:00
|
|
|
self.required.extend_from_slice(reqs);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
if let Some(ref bl) = group.conflicts {
|
2016-02-14 06:10:44 +00:00
|
|
|
self.blacklist.extend_from_slice(bl);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut found = false;
|
|
|
|
if let Some(ref mut grp) = self.groups.get_mut(&group.name) {
|
2016-02-14 06:10:44 +00:00
|
|
|
grp.args.extend_from_slice(&group.args);
|
2016-01-11 08:59:56 +00:00
|
|
|
grp.requires = group.requires.clone();
|
|
|
|
grp.conflicts = group.conflicts.clone();
|
|
|
|
grp.required = group.required;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
self.groups.insert(group.name.into(), group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_subcommand(&mut self, mut subcmd: App<'a, 'b>) {
|
2016-02-04 16:54:46 +00:00
|
|
|
debugln!("fn=Parser::add_subcommand;");
|
2016-10-08 00:45:02 +00:00
|
|
|
debugln!("Term width...{:?}", self.meta.term_w);
|
2016-06-14 01:45:24 +00:00
|
|
|
subcmd.p.meta.term_w = self.meta.term_w;
|
2016-02-04 16:54:46 +00:00
|
|
|
debug!("Is help...");
|
|
|
|
if subcmd.p.meta.name == "help" {
|
|
|
|
sdebugln!("Yes");
|
2016-01-11 08:59:56 +00:00
|
|
|
self.settings.set(AppSettings::NeedsSubcommandHelp);
|
2016-05-06 21:35:53 +00:00
|
|
|
} else {
|
|
|
|
sdebugln!("No");
|
|
|
|
}
|
2016-08-20 21:14:24 +00:00
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
self.subcommands.push(subcmd);
|
|
|
|
}
|
|
|
|
|
2016-09-05 21:01:22 +00:00
|
|
|
pub fn propogate_settings(&mut self) {
|
|
|
|
for sc in &mut self.subcommands {
|
|
|
|
// We have to create a new scope in order to tell rustc the borrow of `sc` is
|
|
|
|
// done and to recursively call this method
|
|
|
|
{
|
|
|
|
let vsc = self.settings.is_set(AppSettings::VersionlessSubcommands);
|
|
|
|
let gv = self.settings.is_set(AppSettings::GlobalVersion);
|
|
|
|
|
|
|
|
debugln!("VersionlessSubcommands set...{:?}", vsc);
|
|
|
|
debugln!("GlobalVersion set...{:?}", gv);
|
|
|
|
|
|
|
|
if vsc {
|
|
|
|
sc.p.settings.set(AppSettings::DisableVersion);
|
|
|
|
}
|
|
|
|
if gv && sc.p.meta.version.is_none() && self.meta.version.is_some() {
|
|
|
|
sc.p.set(AppSettings::GlobalVersion);
|
|
|
|
sc.p.meta.version = Some(self.meta.version.unwrap());
|
|
|
|
}
|
|
|
|
for s in &self.g_settings {
|
|
|
|
sc.p.set(*s);
|
|
|
|
sc.p.g_settings.push(*s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc.p.propogate_settings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:50:29 +00:00
|
|
|
pub fn derive_display_order(&mut self) {
|
|
|
|
if self.settings.is_set(AppSettings::DeriveDisplayOrder) {
|
|
|
|
let unified = self.settings.is_set(AppSettings::UnifiedHelpMessage);
|
|
|
|
for (i, o) in self.opts.iter_mut().enumerate().filter(|&(_, ref o)| o.disp_ord == 999) {
|
|
|
|
o.disp_ord = if unified { o.unified_ord } else { i };
|
|
|
|
}
|
2016-11-01 20:10:40 +00:00
|
|
|
for (i, f) in self.flags
|
|
|
|
.iter_mut()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, ref f)| f.disp_ord == 999) {
|
2016-10-25 20:50:29 +00:00
|
|
|
f.disp_ord = if unified { f.unified_ord } else { i };
|
|
|
|
}
|
2016-11-01 20:10:40 +00:00
|
|
|
for (i, sc) in &mut self.subcommands
|
|
|
|
.iter_mut()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, ref sc)| sc.p.meta.disp_ord == 999) {
|
2016-10-25 20:50:29 +00:00
|
|
|
sc.p.meta.disp_ord = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for sc in &mut self.subcommands {
|
|
|
|
sc.p.derive_display_order();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn required(&self) -> Iter<&str> {
|
|
|
|
self.required.iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_required_from(&self,
|
2016-05-06 21:35:53 +00:00
|
|
|
reqs: &[&'a str],
|
|
|
|
matcher: Option<&ArgMatcher<'a>>)
|
|
|
|
-> VecDeque<String> {
|
2016-01-11 08:59:56 +00:00
|
|
|
let mut c_flags: Vec<&str> = vec![];
|
|
|
|
let mut c_pos: Vec<&str> = vec![];
|
|
|
|
let mut c_opt: Vec<&str> = vec![];
|
|
|
|
let mut grps: Vec<&str> = vec![];
|
|
|
|
for name in reqs {
|
|
|
|
if self.flags.iter().any(|f| &f.name == name) {
|
|
|
|
c_flags.push(name);
|
|
|
|
} else if self.opts.iter().any(|o| &o.name == name) {
|
|
|
|
c_opt.push(name);
|
|
|
|
} else if self.groups.contains_key(name) {
|
|
|
|
grps.push(name);
|
|
|
|
} else {
|
|
|
|
c_pos.push(name);
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 12:03:20 +00:00
|
|
|
macro_rules! fill_vecs {
|
|
|
|
($_self:ident {
|
|
|
|
$t1:ident => $v1:ident => $i1:ident,
|
|
|
|
$t2:ident => $v2:ident => $i2:ident,
|
|
|
|
$t3:ident => $v3:ident => $i3:ident,
|
|
|
|
$gv:ident, $tmp:ident
|
|
|
|
}) => {
|
|
|
|
for a in &$v1 {
|
|
|
|
if let Some(a) = self.$t1.$i1().filter(|arg| &arg.name == a).next() {
|
|
|
|
if let Some(ref rl) = a.requires {
|
|
|
|
for r in rl {
|
|
|
|
if !reqs.contains(r) {
|
|
|
|
if $_self.$t1.$i1().any(|t| &t.name == r) {
|
|
|
|
$tmp.push(*r);
|
|
|
|
} else if $_self.$t2.$i2().any(|t| &t.name == r) {
|
|
|
|
$v2.push(r);
|
|
|
|
} else if $_self.$t3.$i3().any(|t| &t.name == r) {
|
|
|
|
$v3.push(r);
|
|
|
|
} else if $_self.groups.contains_key(r) {
|
|
|
|
$gv.push(r);
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-02 12:03:20 +00:00
|
|
|
$v1.extend(&$tmp);
|
|
|
|
};
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 12:03:20 +00:00
|
|
|
let mut tmp = vec![];
|
|
|
|
fill_vecs!(self {
|
|
|
|
flags => c_flags => iter,
|
|
|
|
opts => c_opt => iter,
|
|
|
|
positionals => c_pos => values,
|
|
|
|
grps, tmp
|
|
|
|
});
|
|
|
|
tmp.clear();
|
|
|
|
fill_vecs!(self {
|
|
|
|
opts => c_opt => iter,
|
|
|
|
flags => c_flags => iter,
|
|
|
|
positionals => c_pos => values,
|
|
|
|
grps, tmp
|
|
|
|
});
|
|
|
|
tmp.clear();
|
|
|
|
fill_vecs!(self {
|
|
|
|
positionals => c_pos => values,
|
|
|
|
opts => c_opt => iter,
|
|
|
|
flags => c_flags => iter,
|
|
|
|
grps, tmp
|
|
|
|
});
|
2016-01-11 08:59:56 +00:00
|
|
|
let mut ret_val = VecDeque::new();
|
2016-05-09 03:20:50 +00:00
|
|
|
c_pos.dedup();
|
|
|
|
c_flags.dedup();
|
|
|
|
c_opt.dedup();
|
2016-05-09 19:54:10 +00:00
|
|
|
grps.dedup();
|
2016-10-04 16:45:35 +00:00
|
|
|
let args_in_groups = grps.iter()
|
2016-10-21 13:41:59 +00:00
|
|
|
.flat_map(|g| self.arg_names_in_group(g))
|
|
|
|
.collect::<Vec<_>>();
|
2016-01-11 08:59:56 +00:00
|
|
|
|
2016-10-04 16:45:35 +00:00
|
|
|
let pmap = c_pos.into_iter()
|
2016-10-21 13:41:59 +00:00
|
|
|
.filter(|&p| matcher.is_none() || !matcher.as_ref().unwrap().contains(p))
|
|
|
|
.filter_map(|p| self.positionals.values().find(|x| x.name == p))
|
|
|
|
.filter(|p| !args_in_groups.contains(&p.name))
|
|
|
|
.map(|p| (p.index, p))
|
|
|
|
.collect::<BTreeMap<u64, &PosBuilder>>();// sort by index
|
2016-05-09 19:54:10 +00:00
|
|
|
debugln!("args_in_groups={:?}", args_in_groups);
|
2016-10-04 16:45:35 +00:00
|
|
|
for &p in pmap.values() {
|
|
|
|
let s = p.to_string();
|
|
|
|
if args_in_groups.is_empty() || !args_in_groups.contains(&&*s) {
|
|
|
|
ret_val.push_back(s);
|
2016-05-09 19:54:10 +00:00
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-02-02 12:03:20 +00:00
|
|
|
macro_rules! write_arg {
|
2016-08-28 03:42:31 +00:00
|
|
|
($i:expr, $m:ident, $v:ident, $r:ident, $aig:ident) => {
|
2016-10-04 13:54:45 +00:00
|
|
|
for f in $v {
|
2016-08-28 03:42:31 +00:00
|
|
|
if $m.is_some() && $m.as_ref().unwrap().contains(f) || $aig.contains(&f) {
|
2016-02-02 12:03:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-02-22 23:36:57 +00:00
|
|
|
$r.push_back($i.filter(|flg| &flg.name == &f).next().unwrap().to_string());
|
2016-02-02 12:03:20 +00:00
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-28 03:42:31 +00:00
|
|
|
write_arg!(self.flags.iter(), matcher, c_flags, ret_val, args_in_groups);
|
|
|
|
write_arg!(self.opts.iter(), matcher, c_opt, ret_val, args_in_groups);
|
2016-05-09 19:32:35 +00:00
|
|
|
let mut g_vec = vec![];
|
2016-10-04 13:54:45 +00:00
|
|
|
for g in grps {
|
2016-01-11 08:59:56 +00:00
|
|
|
let g_string = self.args_in_group(g)
|
2016-09-05 19:29:40 +00:00
|
|
|
.join("|");
|
2016-05-09 19:32:35 +00:00
|
|
|
g_vec.push(format!("<{}>", &g_string[..g_string.len()]));
|
|
|
|
}
|
|
|
|
g_vec.dedup();
|
2016-10-04 13:54:45 +00:00
|
|
|
for g in g_vec {
|
2016-05-09 19:32:35 +00:00
|
|
|
ret_val.push_back(g);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret_val
|
|
|
|
}
|
|
|
|
|
2016-06-24 03:45:52 +00:00
|
|
|
pub fn get_args_tag(&self) -> Option<String> {
|
|
|
|
let mut count = 0;
|
|
|
|
'outer: for p in self.positionals.values().filter(|p| !p.is_set(ArgSettings::Required)) {
|
|
|
|
if let Some(g_vec) = self.groups_for_arg(p.name) {
|
|
|
|
for grp_s in &g_vec {
|
|
|
|
debugln!("iter;grp_s={};", grp_s);
|
|
|
|
if let Some(grp) = self.groups.get(grp_s) {
|
|
|
|
debug!("Is group required...");
|
|
|
|
if grp.required {
|
|
|
|
sdebugln!("Yes (continuing)");
|
|
|
|
continue 'outer;
|
|
|
|
} else {
|
|
|
|
sdebugln!("No (breaking)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debugln!("Arg not required...");
|
2016-09-05 19:29:40 +00:00
|
|
|
count += 1;
|
2016-06-24 03:45:52 +00:00
|
|
|
} else {
|
|
|
|
debugln!("Arg not required...");
|
2016-09-05 19:29:40 +00:00
|
|
|
count += 1;
|
2016-06-24 03:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-26 00:56:22 +00:00
|
|
|
if count > 1 || self.positionals.len() > 1 {
|
2016-06-24 03:45:52 +00:00
|
|
|
return None;
|
|
|
|
} else if count == 1 {
|
|
|
|
let p = self.positionals.values().next().expect(INTERNAL_ERROR_MSG);
|
|
|
|
return Some(format!(" [{}]{}", p.name_no_brackets(), p.multiple_str()));
|
|
|
|
}
|
|
|
|
Some("".into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn needs_flags_tag(&self) -> bool {
|
2016-09-13 01:48:22 +00:00
|
|
|
debugln!("fn=needs_flags_tag;");
|
2016-06-24 03:45:52 +00:00
|
|
|
'outer: for f in &self.flags {
|
|
|
|
debugln!("iter;f={};", f.name);
|
|
|
|
if let Some(l) = f.long {
|
|
|
|
if l == "help" || l == "version" {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(g_vec) = self.groups_for_arg(f.name) {
|
|
|
|
for grp_s in &g_vec {
|
|
|
|
debugln!("iter;grp_s={};", grp_s);
|
|
|
|
if let Some(grp) = self.groups.get(grp_s) {
|
|
|
|
debug!("Is group required...");
|
|
|
|
if grp.required {
|
|
|
|
sdebugln!("Yes (continuing)");
|
|
|
|
continue 'outer;
|
|
|
|
} else {
|
|
|
|
sdebugln!("No (breaking)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debugln!("Flag not required...(returning true)");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
debugln!("Flag not required...(returning true)");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 23:32:42 +00:00
|
|
|
#[inline]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn has_opts(&self) -> bool {
|
2016-03-27 16:56:06 +00:00
|
|
|
!self.opts.is_empty()
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 23:32:42 +00:00
|
|
|
#[inline]
|
2016-06-24 03:45:52 +00:00
|
|
|
pub fn has_flags(&self) -> bool {
|
|
|
|
!self.flags.is_empty()
|
|
|
|
}
|
|
|
|
|
2016-10-23 23:32:42 +00:00
|
|
|
#[inline]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn has_positionals(&self) -> bool {
|
2016-03-27 16:56:06 +00:00
|
|
|
!self.positionals.is_empty()
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 23:32:42 +00:00
|
|
|
#[inline]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn has_subcommands(&self) -> bool {
|
2016-03-27 16:56:06 +00:00
|
|
|
!self.subcommands.is_empty()
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 23:32:42 +00:00
|
|
|
#[inline]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn is_set(&self, s: AppSettings) -> bool {
|
|
|
|
self.settings.is_set(s)
|
|
|
|
}
|
|
|
|
|
2016-10-23 23:32:42 +00:00
|
|
|
#[inline]
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn set(&mut self, s: AppSettings) {
|
|
|
|
self.settings.set(s)
|
|
|
|
}
|
|
|
|
|
2016-10-23 23:32:42 +00:00
|
|
|
#[inline]
|
2016-05-30 10:12:21 +00:00
|
|
|
pub fn unset(&mut self, s: AppSettings) {
|
|
|
|
self.settings.unset(s)
|
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
pub fn verify_positionals(&mut self) {
|
|
|
|
// Because you must wait until all arguments have been supplied, this is the first chance
|
|
|
|
// to make assertions on positional argument indexes
|
|
|
|
//
|
|
|
|
// Firt we verify that the index highest supplied index, is equal to the number of
|
|
|
|
// positional arguments to verify there are no gaps (i.e. supplying an index of 1 and 3
|
|
|
|
// but no 2)
|
2016-08-20 21:33:58 +00:00
|
|
|
if let Some((idx, p)) = self.positionals.iter().rev().next() {
|
2016-05-06 21:59:52 +00:00
|
|
|
debug_assert!(!(idx != self.positionals.len()),
|
2016-05-06 21:35:53 +00:00
|
|
|
format!("Found positional argument \"{}\" who's index is {} but there are \
|
|
|
|
only {} positional arguments defined",
|
|
|
|
p.name,
|
|
|
|
idx,
|
|
|
|
self.positionals.len()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next we verify that only the highest index has a .multiple(true) (if any)
|
2016-11-01 20:10:40 +00:00
|
|
|
if self.positionals()
|
|
|
|
.any(|a| {
|
|
|
|
a.settings.is_set(ArgSettings::Multiple) &&
|
|
|
|
(a.index as usize != self.positionals.len())
|
|
|
|
}) {
|
|
|
|
debug_assert!(self.positionals()
|
|
|
|
.filter(|p| p.settings.is_set(ArgSettings::Multiple)
|
|
|
|
&& p.num_vals.is_none()).map(|_| 1).sum::<u64>() <= 1,
|
|
|
|
"Only one positional argument with .multiple(true) set is allowed per command");
|
|
|
|
|
|
|
|
debug_assert!(self.positionals()
|
|
|
|
.rev()
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.is_set(ArgSettings::Required),
|
|
|
|
"When using a positional argument with .multiple(true) that is *not the last* \
|
|
|
|
positional argument, the last positional argument (i.e the one with the highest \
|
|
|
|
index) *must* have .required(true) set.");
|
|
|
|
|
|
|
|
debug_assert!({
|
|
|
|
let num = self.positionals.len() - 1;
|
|
|
|
self.positionals.get(num).unwrap().is_set(ArgSettings::Multiple)
|
|
|
|
},
|
|
|
|
"Only the last positional argument, or second to last positional argument may be set to .multiple(true)");
|
|
|
|
|
|
|
|
self.set(AppSettings::LowIndexMultiplePositional);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_assert!(self.positionals()
|
|
|
|
.filter(|p| p.settings.is_set(ArgSettings::Multiple)
|
|
|
|
&& p.num_vals.is_none())
|
|
|
|
.map(|_| 1)
|
|
|
|
.sum::<u64>() <= 1,
|
|
|
|
"Only one positional argument with .multiple(true) set is allowed per command");
|
2016-01-11 08:59:56 +00:00
|
|
|
|
|
|
|
// If it's required we also need to ensure all previous positionals are
|
|
|
|
// required too
|
|
|
|
let mut found = false;
|
|
|
|
for p in self.positionals.values().rev() {
|
2016-03-08 15:39:43 +00:00
|
|
|
if found {
|
|
|
|
debug_assert!(p.settings.is_set(ArgSettings::Required),
|
2016-05-06 21:35:53 +00:00
|
|
|
"Found positional argument which is not required with a lower index \
|
|
|
|
than a required positional argument: {:?} index {}",
|
|
|
|
p.name,
|
|
|
|
p.index);
|
2016-03-08 15:39:43 +00:00
|
|
|
} else if p.settings.is_set(ArgSettings::Required) {
|
|
|
|
found = true;
|
|
|
|
continue;
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn propogate_globals(&mut self) {
|
2016-01-23 11:46:16 +00:00
|
|
|
for sc in &mut self.subcommands {
|
2016-01-11 08:59:56 +00:00
|
|
|
// We have to create a new scope in order to tell rustc the borrow of `sc` is
|
|
|
|
// done and to recursively call this method
|
|
|
|
{
|
2016-01-23 11:46:16 +00:00
|
|
|
for a in &self.global_args {
|
2016-02-04 16:54:46 +00:00
|
|
|
sc.p.add_arg(a);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 16:54:46 +00:00
|
|
|
sc.p.propogate_globals();
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:41:45 +00:00
|
|
|
// Checks if the arg matches a subcommand name, or any of it's aliases (if defined)
|
|
|
|
#[inline]
|
|
|
|
fn possible_subcommand(&self, arg_os: &OsStr) -> bool {
|
|
|
|
debugln!("fn=possible_subcommand");
|
|
|
|
self.subcommands
|
|
|
|
.iter()
|
|
|
|
.any(|s| {
|
|
|
|
&s.p.meta.name[..] == &*arg_os ||
|
|
|
|
(s.p.meta.aliases.is_some() &&
|
|
|
|
s.p
|
|
|
|
.meta
|
|
|
|
.aliases
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|&(a, _)| a == &*arg_os))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn get_opt(&self, arg: &str) -> Option<&OptBuilder<'a, 'b>> {
|
|
|
|
debugln!("fn=get_opt");
|
|
|
|
self.opts
|
|
|
|
.iter()
|
|
|
|
.find(|o| {
|
|
|
|
&o.name == &arg ||
|
|
|
|
(o.aliases.is_some() &&
|
|
|
|
o.aliases
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|&(a, _)| a == arg))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_help_subcommand<I, T>(&self, it: &mut I) -> ClapResult<()>
|
|
|
|
where I: Iterator<Item = T>,
|
|
|
|
T: Into<OsString>
|
|
|
|
{
|
|
|
|
debugln!("fn=parse_help_subcommand;");
|
|
|
|
let cmds: Vec<OsString> = it.map(|c| c.into()).collect();
|
|
|
|
let mut help_help = false;
|
|
|
|
let mut bin_name = self.meta
|
|
|
|
.bin_name
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&self.meta.name)
|
|
|
|
.clone();
|
|
|
|
let mut sc = {
|
|
|
|
let mut sc: &Parser = self;
|
|
|
|
for (i, cmd) in cmds.iter().enumerate() {
|
|
|
|
if &*cmd.to_string_lossy() == "help" {
|
|
|
|
// cmd help help
|
|
|
|
help_help = true;
|
|
|
|
}
|
|
|
|
if let Some(c) = sc.subcommands
|
|
|
|
.iter()
|
|
|
|
.find(|s| &*s.p.meta.name == cmd)
|
|
|
|
.map(|sc| &sc.p) {
|
|
|
|
sc = c;
|
|
|
|
if i == cmds.len() - 1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if let Some(c) = sc.subcommands
|
|
|
|
.iter()
|
|
|
|
.find(|s| if let Some(ref als) = s.p
|
|
|
|
.meta
|
|
|
|
.aliases {
|
|
|
|
als.iter()
|
|
|
|
.any(|&(a, _)| &a == &&*cmd.to_string_lossy())
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
})
|
|
|
|
.map(|sc| &sc.p) {
|
|
|
|
sc = c;
|
|
|
|
if i == cmds.len() - 1 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::unrecognized_subcommand(cmd.to_string_lossy().into_owned(),
|
|
|
|
self.meta
|
|
|
|
.bin_name
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&self.meta.name),
|
|
|
|
self.color()));
|
|
|
|
}
|
|
|
|
bin_name = format!("{} {}",
|
|
|
|
bin_name,
|
|
|
|
&*sc.meta.name);
|
|
|
|
}
|
|
|
|
sc.clone()
|
|
|
|
};
|
|
|
|
if help_help {
|
|
|
|
let mut pb = PosBuilder::new("subcommand", 1);
|
|
|
|
pb.help = Some("The subcommand whose help message to display");
|
|
|
|
pb.set(ArgSettings::Multiple);
|
|
|
|
sc.positionals.insert(1, pb);
|
|
|
|
for s in self.g_settings.clone() {
|
|
|
|
sc.set(s);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sc.create_help_and_version();
|
|
|
|
}
|
|
|
|
if sc.meta.bin_name != self.meta.bin_name {
|
|
|
|
sc.meta.bin_name = Some(format!("{} {}", bin_name, sc.meta.name));
|
|
|
|
}
|
2016-10-21 13:52:16 +00:00
|
|
|
sc._help()
|
2016-10-21 13:41:45 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
// The actual parsing function
|
|
|
|
#[cfg_attr(feature = "lints", allow(while_let_on_iterator))]
|
|
|
|
pub fn get_matches_with<I, T>(&mut self,
|
2016-05-06 21:35:53 +00:00
|
|
|
matcher: &mut ArgMatcher<'a>,
|
2016-11-01 20:10:40 +00:00
|
|
|
it: &mut Peekable<I>)
|
2016-05-06 21:35:53 +00:00
|
|
|
-> ClapResult<()>
|
2016-01-11 08:59:56 +00:00
|
|
|
where I: Iterator<Item = T>,
|
2016-11-01 20:10:40 +00:00
|
|
|
T: Into<OsString> + Clone
|
2016-01-11 08:59:56 +00:00
|
|
|
{
|
2016-01-22 17:58:56 +00:00
|
|
|
debugln!("fn=get_matches_with;");
|
2016-01-11 08:59:56 +00:00
|
|
|
// First we create the `--help` and `--version` arguments and add them if
|
|
|
|
// necessary
|
|
|
|
self.create_help_and_version();
|
|
|
|
|
|
|
|
let mut subcmd_name: Option<String> = None;
|
|
|
|
let mut needs_val_of: Option<&str> = None;
|
|
|
|
let mut pos_counter = 1;
|
|
|
|
while let Some(arg) = it.next() {
|
|
|
|
let arg_os = arg.into();
|
2016-01-22 17:58:56 +00:00
|
|
|
debugln!("Begin parsing '{:?}' ({:?})", arg_os, &*arg_os.as_bytes());
|
2016-01-11 08:59:56 +00:00
|
|
|
|
|
|
|
// Is this a new argument, or values from a previous option?
|
2016-11-01 20:10:40 +00:00
|
|
|
let starts_new_arg = is_new_arg(&arg_os);
|
2016-01-11 08:59:56 +00:00
|
|
|
|
2016-10-21 13:39:17 +00:00
|
|
|
// Has the user already passed '--'? Meaning only positional args follow
|
2016-06-05 05:09:07 +00:00
|
|
|
if !self.trailing_vals {
|
2016-05-10 19:21:19 +00:00
|
|
|
// Does the arg match a subcommand name, or any of it's aliases (if defined)
|
2016-10-21 13:39:17 +00:00
|
|
|
let pos_sc = self.possible_subcommand(&arg_os);
|
|
|
|
|
|
|
|
// If the arg doesn't start with a `-` (except numbers, or AllowLeadingHyphen) and
|
|
|
|
// isn't a subcommand
|
|
|
|
if (!starts_new_arg ||
|
|
|
|
(self.is_set(AppSettings::AllowLeadingHyphen) ||
|
|
|
|
self.is_set(AppSettings::AllowNegativeNumbers))) &&
|
|
|
|
!pos_sc {
|
2016-01-11 08:59:56 +00:00
|
|
|
// Check to see if parsing a value from an option
|
2016-10-21 13:39:17 +00:00
|
|
|
if let Some(arg) = needs_val_of {
|
2016-01-11 08:59:56 +00:00
|
|
|
// get the OptBuilder so we can check the settings
|
2016-10-21 13:52:16 +00:00
|
|
|
if let Some(opt) = self.get_opt(arg) {
|
|
|
|
needs_val_of = try!(self.add_val_to_arg(&*opt, &arg_os, matcher));
|
2016-01-11 08:59:56 +00:00
|
|
|
// get the next value from the iterator
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if arg_os.starts_with(b"--") {
|
2016-02-21 16:18:26 +00:00
|
|
|
if arg_os.len_() == 2 {
|
2016-05-06 21:35:53 +00:00
|
|
|
// The user has passed '--' which means only positional args follow no
|
|
|
|
// matter what they start with
|
2016-06-05 05:09:07 +00:00
|
|
|
self.trailing_vals = true;
|
2016-01-11 08:59:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
needs_val_of = try!(self.parse_long_arg(matcher, &arg_os));
|
2016-07-01 17:47:39 +00:00
|
|
|
if !(needs_val_of.is_none() && self.is_set(AppSettings::AllowLeadingHyphen)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-21 16:18:26 +00:00
|
|
|
} else if arg_os.starts_with(b"-") && arg_os.len_() != 1 {
|
2016-10-21 13:41:45 +00:00
|
|
|
// Try to parse short args like normal, if AllowLeadingHyphen or
|
|
|
|
// AllowNegativeNumbers is set, parse_short_arg will *not* throw
|
|
|
|
// an error, and instead return Ok(None)
|
2016-01-11 08:59:56 +00:00
|
|
|
needs_val_of = try!(self.parse_short_arg(matcher, &arg_os));
|
2016-10-21 13:41:45 +00:00
|
|
|
// If it's None, we then check if one of those two AppSettings was set
|
|
|
|
debugln!("AllowLeadingHyphen set...{:?}", self.is_set(AppSettings::AllowLeadingHyphen));
|
|
|
|
debugln!("AllowNegativeNumbers set...{:?}", self.is_set(AppSettings::AllowNegativeNumbers));
|
|
|
|
debugln!("Valid negative number...{:?}", (arg_os.to_string_lossy().parse::<i64>().is_ok() ||
|
|
|
|
arg_os.to_string_lossy().parse::<f64>().is_ok()));
|
|
|
|
if needs_val_of.is_none() {
|
|
|
|
if self.is_set(AppSettings::AllowNegativeNumbers) {
|
2016-11-01 20:10:40 +00:00
|
|
|
if !(arg_os.to_string_lossy().parse::<i64>().is_ok() ||
|
|
|
|
arg_os.to_string_lossy().parse::<f64>().is_ok()) {
|
2016-10-21 13:41:45 +00:00
|
|
|
return Err(Error::unknown_argument(&*arg_os.to_string_lossy(),
|
|
|
|
"",
|
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
|
|
|
}
|
|
|
|
} else if !self.is_set(AppSettings::AllowLeadingHyphen) {
|
|
|
|
continue;
|
2016-11-01 20:10:40 +00:00
|
|
|
}
|
2016-10-21 13:41:45 +00:00
|
|
|
} else {
|
2016-01-27 16:28:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-01-22 17:58:56 +00:00
|
|
|
|
2016-01-31 12:35:42 +00:00
|
|
|
if pos_sc {
|
2016-10-21 13:41:45 +00:00
|
|
|
if &*arg_os == "help" && self.is_set(AppSettings::NeedsSubcommandHelp) {
|
|
|
|
try!(self.parse_help_subcommand(it));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-01-22 17:58:56 +00:00
|
|
|
subcmd_name = Some(arg_os.to_str().expect(INVALID_UTF8).to_owned());
|
2016-01-11 08:59:56 +00:00
|
|
|
break;
|
2016-10-21 13:41:45 +00:00
|
|
|
} else if let Some(cdate) =
|
|
|
|
suggestions::did_you_mean(&*arg_os.to_string_lossy(),
|
|
|
|
self.subcommands
|
|
|
|
.iter()
|
|
|
|
.map(|s| &s.p.meta.name)) {
|
2016-05-06 21:35:53 +00:00
|
|
|
return Err(Error::invalid_subcommand(arg_os.to_string_lossy().into_owned(),
|
|
|
|
cdate,
|
|
|
|
self.meta
|
|
|
|
.bin_name
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&self.meta.name),
|
2016-05-30 08:07:44 +00:00
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:10:40 +00:00
|
|
|
debugln!("Positional counter...{}", pos_counter);
|
|
|
|
debug!("Checking for low index multiples...");
|
|
|
|
if self.is_set(AppSettings::LowIndexMultiplePositional) && pos_counter == (self.positionals.len() - 1) {
|
|
|
|
sdebugln!("Found");
|
|
|
|
if let Some(na) = it.peek() {
|
|
|
|
let n = (*na).clone().into();
|
|
|
|
if is_new_arg(&n) || self.possible_subcommand(&n) || suggestions::did_you_mean(&n.to_string_lossy(),
|
|
|
|
self.subcommands
|
|
|
|
.iter()
|
|
|
|
.map(|s| &s.p.meta.name)).is_some() {
|
|
|
|
debugln!("Bumping the positional counter...");
|
|
|
|
pos_counter += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debugln!("Bumping the positional counter...");
|
|
|
|
pos_counter += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sdebugln!("None");
|
|
|
|
}
|
2016-03-04 18:42:22 +00:00
|
|
|
if let Some(p) = self.positionals.get(pos_counter) {
|
2016-06-05 05:09:07 +00:00
|
|
|
parse_positional!(self, p, arg_os, pos_counter, matcher);
|
2016-07-26 00:56:22 +00:00
|
|
|
} else if self.settings.is_set(AppSettings::AllowExternalSubcommands) {
|
2016-07-16 21:18:46 +00:00
|
|
|
// Get external subcommand name
|
|
|
|
let sc_name = match arg_os.to_str() {
|
|
|
|
Some(s) => s.to_string(),
|
|
|
|
None => {
|
|
|
|
if !self.settings.is_set(AppSettings::StrictUtf8) {
|
2016-09-05 19:29:40 +00:00
|
|
|
return Err(Error::invalid_utf8(&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-07-16 21:18:46 +00:00
|
|
|
}
|
|
|
|
arg_os.to_string_lossy().into_owned()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Collect the external subcommand args
|
2016-07-26 00:56:22 +00:00
|
|
|
let mut sc_m = ArgMatcher::new();
|
|
|
|
while let Some(v) = it.next() {
|
|
|
|
let a = v.into();
|
|
|
|
if let None = a.to_str() {
|
|
|
|
if !self.settings.is_set(AppSettings::StrictUtf8) {
|
2016-09-05 19:29:40 +00:00
|
|
|
return Err(Error::invalid_utf8(&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-16 21:18:46 +00:00
|
|
|
sc_m.add_val_to("", &a);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-07-26 00:56:22 +00:00
|
|
|
|
|
|
|
matcher.subcommand(SubCommand {
|
2016-07-16 21:18:46 +00:00
|
|
|
name: sc_name,
|
2016-07-26 00:56:22 +00:00
|
|
|
matches: sc_m.into(),
|
|
|
|
});
|
2016-10-21 13:39:17 +00:00
|
|
|
} else if !(self.is_set(AppSettings::AllowLeadingHyphen) ||
|
|
|
|
self.is_set(AppSettings::AllowNegativeNumbers)) {
|
2016-07-26 00:56:22 +00:00
|
|
|
return Err(Error::unknown_argument(&*arg_os.to_string_lossy(),
|
|
|
|
"",
|
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut reqs_validated = false;
|
|
|
|
if let Some(a) = needs_val_of {
|
2016-09-13 01:48:22 +00:00
|
|
|
debugln!("needs_val_of={}", a);
|
|
|
|
debug!("Is this an opt...");
|
2016-09-30 21:54:14 +00:00
|
|
|
if let Some(o) = self.opts
|
|
|
|
.iter()
|
|
|
|
.find(|o| {
|
|
|
|
&o.name == &a ||
|
|
|
|
(o.aliases.is_some() &&
|
|
|
|
o.aliases
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|&(n, _)| n == &*a))
|
|
|
|
}) {
|
2016-09-13 01:48:22 +00:00
|
|
|
sdebugln!("Yes");
|
2016-01-11 08:59:56 +00:00
|
|
|
try!(self.validate_required(matcher));
|
|
|
|
reqs_validated = true;
|
2016-08-20 21:33:58 +00:00
|
|
|
let should_err = if let Some(v) = matcher.0.args.get(&*o.name) {
|
2016-01-25 18:13:11 +00:00
|
|
|
v.vals.is_empty() && !(o.min_vals.is_some() && o.min_vals.unwrap() == 0)
|
2016-01-11 08:59:56 +00:00
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
if should_err {
|
2016-09-05 19:29:40 +00:00
|
|
|
return Err(Error::empty_value(o,
|
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-09-13 01:48:22 +00:00
|
|
|
sdebugln!("No");
|
|
|
|
debugln!("Returning Error::empty_value");
|
2016-01-11 08:59:56 +00:00
|
|
|
return Err(Error::empty_value(self.positionals
|
2016-05-06 21:35:53 +00:00
|
|
|
.values()
|
2016-09-05 19:14:47 +00:00
|
|
|
.find(|p| &p.name == &a)
|
2016-05-06 21:35:53 +00:00
|
|
|
.expect(INTERNAL_ERROR_MSG),
|
2016-05-30 08:07:44 +00:00
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 17:52:00 +00:00
|
|
|
try!(self.add_defaults(matcher));
|
2016-01-11 08:59:56 +00:00
|
|
|
try!(self.validate_blacklist(matcher));
|
|
|
|
try!(self.validate_num_args(matcher));
|
|
|
|
matcher.usage(self.create_usage(&[]));
|
|
|
|
|
|
|
|
if !(self.settings.is_set(AppSettings::SubcommandsNegateReqs) && subcmd_name.is_some()) &&
|
2016-05-06 21:35:53 +00:00
|
|
|
!reqs_validated {
|
2016-01-11 08:59:56 +00:00
|
|
|
try!(self.validate_required(matcher));
|
|
|
|
}
|
2016-05-10 19:21:19 +00:00
|
|
|
if let Some(pos_sc_name) = subcmd_name {
|
|
|
|
// is this is a real subcommand, or an alias
|
|
|
|
let sc_name = if self.subcommands.iter().any(|sc| sc.p.meta.name == pos_sc_name) {
|
|
|
|
pos_sc_name
|
|
|
|
} else {
|
|
|
|
self.subcommands
|
|
|
|
.iter()
|
|
|
|
.filter(|sc| sc.p.meta.aliases.is_some())
|
2016-09-05 19:29:40 +00:00
|
|
|
.filter_map(|sc| if sc.p
|
|
|
|
.meta
|
|
|
|
.aliases
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|&(a, _)| &a == &&*pos_sc_name) {
|
|
|
|
Some(sc.p.meta.name.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
2016-05-10 19:21:19 +00:00
|
|
|
.next()
|
|
|
|
.expect(INTERNAL_ERROR_MSG)
|
|
|
|
};
|
2016-01-22 17:58:56 +00:00
|
|
|
try!(self.parse_subcommand(sc_name, matcher, it));
|
2016-01-11 08:59:56 +00:00
|
|
|
} else if self.is_set(AppSettings::SubcommandRequired) {
|
|
|
|
let bn = self.meta.bin_name.as_ref().unwrap_or(&self.meta.name);
|
2016-09-05 19:29:40 +00:00
|
|
|
return Err(Error::missing_subcommand(bn,
|
|
|
|
&self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
} else if self.is_set(AppSettings::SubcommandRequiredElseHelp) {
|
|
|
|
let mut out = vec![];
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(self.write_help_err(&mut out));
|
2016-01-11 08:59:56 +00:00
|
|
|
return Err(Error {
|
|
|
|
message: String::from_utf8_lossy(&*out).into_owned(),
|
|
|
|
kind: ErrorKind::MissingArgumentOrSubcommand,
|
|
|
|
info: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if matcher.is_empty() && matcher.subcommand_name().is_none() &&
|
|
|
|
self.is_set(AppSettings::ArgRequiredElseHelp) {
|
|
|
|
let mut out = vec![];
|
2016-05-30 08:07:44 +00:00
|
|
|
try!(self.write_help_err(&mut out));
|
2016-01-11 08:59:56 +00:00
|
|
|
return Err(Error {
|
|
|
|
message: String::from_utf8_lossy(&*out).into_owned(),
|
|
|
|
kind: ErrorKind::MissingArgumentOrSubcommand,
|
|
|
|
info: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-01-22 17:58:56 +00:00
|
|
|
|
2016-06-29 03:38:22 +00:00
|
|
|
fn propogate_help_version(&mut self) {
|
2016-09-13 01:48:22 +00:00
|
|
|
debugln!("fn=propogate_help_version;");
|
2016-06-29 03:38:22 +00:00
|
|
|
self.create_help_and_version();
|
2016-07-26 00:56:22 +00:00
|
|
|
for sc in &mut self.subcommands {
|
2016-06-29 03:38:22 +00:00
|
|
|
sc.p.propogate_help_version();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_bin_names(&mut self) {
|
2016-09-13 01:48:22 +00:00
|
|
|
debugln!("fn=build_bin_names;");
|
2016-07-26 00:56:22 +00:00
|
|
|
for sc in &mut self.subcommands {
|
2016-06-29 03:38:22 +00:00
|
|
|
debug!("bin_name set...");
|
|
|
|
if sc.p.meta.bin_name.is_none() {
|
|
|
|
sdebugln!("No");
|
|
|
|
let bin_name = format!("{}{}{}",
|
|
|
|
self.meta
|
|
|
|
.bin_name
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&self.meta.name.clone()),
|
|
|
|
if self.meta.bin_name.is_some() {
|
|
|
|
" "
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
},
|
|
|
|
&*sc.p.meta.name);
|
|
|
|
debugln!("Setting bin_name of {} to {}", self.meta.name, bin_name);
|
|
|
|
sc.p.meta.bin_name = Some(bin_name);
|
|
|
|
} else {
|
|
|
|
sdebugln!("yes ({:?})", sc.p.meta.bin_name);
|
|
|
|
}
|
|
|
|
debugln!("Calling build_bin_names from...{}", sc.p.meta.name);
|
|
|
|
sc.p.build_bin_names();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 21:35:53 +00:00
|
|
|
fn parse_subcommand<I, T>(&mut self,
|
|
|
|
sc_name: String,
|
|
|
|
matcher: &mut ArgMatcher<'a>,
|
2016-11-01 20:10:40 +00:00
|
|
|
it: &mut Peekable<I>)
|
2016-05-06 21:35:53 +00:00
|
|
|
-> ClapResult<()>
|
2016-01-22 17:58:56 +00:00
|
|
|
where I: Iterator<Item = T>,
|
2016-11-01 20:10:40 +00:00
|
|
|
T: Into<OsString> + Clone
|
2016-01-22 17:58:56 +00:00
|
|
|
{
|
|
|
|
use std::fmt::Write;
|
|
|
|
debugln!("fn=parse_subcommand;");
|
|
|
|
let mut mid_string = String::new();
|
|
|
|
if !self.settings.is_set(AppSettings::SubcommandsNegateReqs) {
|
|
|
|
let mut hs: Vec<&str> = self.required.iter().map(|n| &**n).collect();
|
|
|
|
for k in matcher.arg_names() {
|
|
|
|
hs.push(k);
|
|
|
|
}
|
|
|
|
let reqs = self.get_required_from(&hs, Some(matcher));
|
|
|
|
|
2016-01-23 11:46:16 +00:00
|
|
|
for s in &reqs {
|
2016-01-22 17:58:56 +00:00
|
|
|
write!(&mut mid_string, " {}", s).expect(INTERNAL_ERROR_MSG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mid_string.push_str(" ");
|
|
|
|
if let Some(ref mut sc) = self.subcommands
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter_mut()
|
|
|
|
.find(|s| &s.p.meta.name == &sc_name) {
|
2016-01-22 17:58:56 +00:00
|
|
|
let mut sc_matcher = ArgMatcher::new();
|
|
|
|
// bin_name should be parent's bin_name + [<reqs>] + the sc's name separated by
|
|
|
|
// a space
|
2016-02-04 16:54:46 +00:00
|
|
|
sc.p.meta.usage = Some(format!("{}{}{}",
|
2016-05-06 21:35:53 +00:00
|
|
|
self.meta.bin_name.as_ref().unwrap_or(&String::new()),
|
|
|
|
if self.meta.bin_name.is_some() {
|
|
|
|
&*mid_string
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
},
|
|
|
|
&*sc.p.meta.name));
|
2016-02-04 16:54:46 +00:00
|
|
|
sc.p.meta.bin_name = Some(format!("{}{}{}",
|
2016-05-06 21:35:53 +00:00
|
|
|
self.meta
|
|
|
|
.bin_name
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&String::new()),
|
|
|
|
if self.meta.bin_name.is_some() {
|
|
|
|
" "
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
},
|
|
|
|
&*sc.p.meta.name));
|
2016-02-04 16:54:46 +00:00
|
|
|
try!(sc.p.get_matches_with(&mut sc_matcher, it));
|
2016-01-22 17:58:56 +00:00
|
|
|
matcher.subcommand(SubCommand {
|
2016-02-04 16:54:46 +00:00
|
|
|
name: sc.p.meta.name.clone(),
|
2016-01-22 17:58:56 +00:00
|
|
|
matches: sc_matcher.into(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
|
|
|
|
fn groups_for_arg(&self, name: &str) -> Option<Vec<&'a str>> {
|
2016-09-13 01:48:22 +00:00
|
|
|
debugln!("fn=groups_for_arg; name={}", name);
|
2016-01-22 04:18:52 +00:00
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
if self.groups.is_empty() {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("No groups defined");
|
2016-01-11 08:59:56 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let mut res = vec![];
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("Searching through groups...");
|
|
|
|
for (gn, grp) in &self.groups {
|
|
|
|
for a in &grp.args {
|
|
|
|
if a == &name {
|
|
|
|
sdebugln!("\tFound '{}'", gn);
|
|
|
|
res.push(*gn);
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
if res.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn args_in_group(&self, group: &str) -> Vec<String> {
|
|
|
|
let mut g_vec = vec![];
|
|
|
|
let mut args = vec![];
|
|
|
|
|
|
|
|
for n in &self.groups.get(group).unwrap().args {
|
2016-09-05 19:14:47 +00:00
|
|
|
if let Some(f) = self.flags.iter().find(|f| &f.name == n) {
|
2016-01-11 08:59:56 +00:00
|
|
|
args.push(f.to_string());
|
2016-09-05 19:14:47 +00:00
|
|
|
} else if let Some(f) = self.opts.iter().find(|o| &o.name == n) {
|
2016-01-11 08:59:56 +00:00
|
|
|
args.push(f.to_string());
|
|
|
|
} else if self.groups.contains_key(&**n) {
|
|
|
|
g_vec.push(*n);
|
2016-02-02 10:05:54 +00:00
|
|
|
} else if let Some(p) = self.positionals
|
2016-09-05 19:29:40 +00:00
|
|
|
.values()
|
|
|
|
.find(|p| &p.name == n) {
|
2016-05-09 01:33:27 +00:00
|
|
|
args.push(p.name.to_owned());
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 10:05:54 +00:00
|
|
|
for av in g_vec.iter().map(|g| self.args_in_group(g)) {
|
|
|
|
args.extend(av);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
args.dedup();
|
|
|
|
args.iter().map(ToOwned::to_owned).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn arg_names_in_group(&self, group: &str) -> Vec<&'a str> {
|
|
|
|
let mut g_vec = vec![];
|
|
|
|
let mut args = vec![];
|
|
|
|
|
|
|
|
for n in &self.groups.get(group).unwrap().args {
|
2016-02-14 06:10:44 +00:00
|
|
|
if self.groups.contains_key(&*n) {
|
2016-01-11 08:59:56 +00:00
|
|
|
g_vec.push(*n);
|
2016-02-14 06:10:44 +00:00
|
|
|
} else {
|
2016-01-11 08:59:56 +00:00
|
|
|
args.push(*n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 10:05:54 +00:00
|
|
|
for av in g_vec.iter().map(|g| self.arg_names_in_group(g)) {
|
|
|
|
args.extend(av);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
args.dedup();
|
|
|
|
args.iter().map(|s| *s).collect()
|
|
|
|
}
|
|
|
|
|
2016-06-24 04:26:07 +00:00
|
|
|
pub fn create_help_and_version(&mut self) {
|
2016-01-22 17:58:56 +00:00
|
|
|
debugln!("fn=create_help_and_version;");
|
2016-01-11 08:59:56 +00:00
|
|
|
// name is "hclap_help" because flags are sorted by name
|
2016-06-24 04:26:07 +00:00
|
|
|
if self.is_set(AppSettings::NeedsLongHelp) {
|
2016-03-09 22:35:40 +00:00
|
|
|
debugln!("Building --help");
|
2016-01-11 08:59:56 +00:00
|
|
|
if self.help_short.is_none() && !self.short_list.contains(&'h') {
|
|
|
|
self.help_short = Some('h');
|
|
|
|
}
|
|
|
|
let arg = FlagBuilder {
|
2016-03-09 22:35:40 +00:00
|
|
|
name: "hclap_help",
|
2016-01-11 08:59:56 +00:00
|
|
|
short: self.help_short,
|
2016-03-09 22:35:40 +00:00
|
|
|
long: Some("help"),
|
|
|
|
help: Some("Prints help information"),
|
|
|
|
..Default::default()
|
2016-01-11 08:59:56 +00:00
|
|
|
};
|
2016-07-03 14:52:06 +00:00
|
|
|
if let Some(h) = self.help_short {
|
|
|
|
self.short_list.push(h);
|
|
|
|
}
|
2016-03-09 22:35:40 +00:00
|
|
|
self.long_list.push("help");
|
2016-01-11 08:59:56 +00:00
|
|
|
self.flags.push(arg);
|
|
|
|
}
|
|
|
|
if !self.settings.is_set(AppSettings::DisableVersion) &&
|
2016-09-05 19:29:40 +00:00
|
|
|
self.is_set(AppSettings::NeedsLongVersion) {
|
2016-03-09 22:35:40 +00:00
|
|
|
debugln!("Building --version");
|
2016-01-11 08:59:56 +00:00
|
|
|
if self.version_short.is_none() && !self.short_list.contains(&'V') {
|
|
|
|
self.version_short = Some('V');
|
|
|
|
}
|
|
|
|
// name is "vclap_version" because flags are sorted by name
|
|
|
|
let arg = FlagBuilder {
|
2016-03-09 22:35:40 +00:00
|
|
|
name: "vclap_version",
|
2016-01-11 08:59:56 +00:00
|
|
|
short: self.version_short,
|
2016-03-09 22:35:40 +00:00
|
|
|
long: Some("version"),
|
|
|
|
help: Some("Prints version information"),
|
|
|
|
..Default::default()
|
2016-01-11 08:59:56 +00:00
|
|
|
};
|
2016-07-03 14:52:06 +00:00
|
|
|
if let Some(v) = self.version_short {
|
|
|
|
self.short_list.push(v);
|
|
|
|
}
|
2016-03-09 22:35:40 +00:00
|
|
|
self.long_list.push("version");
|
2016-01-11 08:59:56 +00:00
|
|
|
self.flags.push(arg);
|
|
|
|
}
|
2016-06-24 04:26:07 +00:00
|
|
|
if !self.subcommands.is_empty() && self.is_set(AppSettings::NeedsSubcommandHelp) {
|
2016-03-09 22:35:40 +00:00
|
|
|
debugln!("Building help");
|
2016-05-06 21:35:53 +00:00
|
|
|
self.subcommands
|
|
|
|
.push(App::new("help")
|
2016-09-05 19:29:40 +00:00
|
|
|
.about("Prints this message or the help of the given subcommand(s)"));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieves the names of all args the user has supplied thus far, except required ones
|
|
|
|
// because those will be listed in self.required
|
|
|
|
pub fn create_current_usage(&self, matcher: &'b ArgMatcher<'a>) -> String {
|
2016-05-06 21:35:53 +00:00
|
|
|
self.create_usage(&*matcher.arg_names()
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
|
|
|
.filter(|n| {
|
|
|
|
if let Some(o) = self.opts
|
|
|
|
.iter()
|
|
|
|
.find(|&o| &&o.name == n) {
|
|
|
|
!o.settings.is_set(ArgSettings::Required)
|
|
|
|
} else if let Some(p) = self.positionals
|
|
|
|
.values()
|
|
|
|
.find(|&p| &&p.name == n) {
|
|
|
|
!p.settings.is_set(ArgSettings::Required)
|
|
|
|
} else {
|
|
|
|
true // flags can't be required, so they're always true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|&n| n)
|
|
|
|
.collect::<Vec<_>>())
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_for_help_and_version_str(&self, arg: &OsStr) -> ClapResult<()> {
|
2016-05-06 21:35:53 +00:00
|
|
|
debug!("Checking if --{} is help or version...",
|
|
|
|
arg.to_str().unwrap());
|
2016-01-11 08:59:56 +00:00
|
|
|
if arg == "help" && self.settings.is_set(AppSettings::NeedsLongHelp) {
|
2016-05-02 23:02:24 +00:00
|
|
|
sdebugln!("Help");
|
2016-01-11 08:59:56 +00:00
|
|
|
try!(self._help());
|
|
|
|
}
|
|
|
|
if arg == "version" && self.settings.is_set(AppSettings::NeedsLongVersion) {
|
2016-05-02 23:02:24 +00:00
|
|
|
sdebugln!("Version");
|
2016-01-11 08:59:56 +00:00
|
|
|
try!(self._version());
|
|
|
|
}
|
2016-01-22 04:18:52 +00:00
|
|
|
sdebugln!("Neither");
|
2016-01-11 08:59:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_for_help_and_version_char(&self, arg: char) -> ClapResult<()> {
|
2016-01-22 04:18:52 +00:00
|
|
|
debug!("Checking if -{} is help or version...", arg);
|
2016-05-02 23:02:24 +00:00
|
|
|
if let Some(h) = self.help_short {
|
2016-05-30 10:12:21 +00:00
|
|
|
if arg == h && self.settings.is_set(AppSettings::NeedsLongHelp) {
|
2016-09-13 01:48:22 +00:00
|
|
|
sdebugln!("Help");
|
2016-05-06 21:35:53 +00:00
|
|
|
try!(self._help());
|
|
|
|
}
|
2016-05-02 23:02:24 +00:00
|
|
|
}
|
|
|
|
if let Some(v) = self.version_short {
|
2016-05-30 10:12:21 +00:00
|
|
|
if arg == v && self.settings.is_set(AppSettings::NeedsLongVersion) {
|
2016-09-13 01:48:22 +00:00
|
|
|
sdebugln!("Version");
|
2016-05-06 21:35:53 +00:00
|
|
|
try!(self._version());
|
|
|
|
}
|
2016-05-02 23:02:24 +00:00
|
|
|
}
|
2016-01-22 04:18:52 +00:00
|
|
|
sdebugln!("Neither");
|
2016-01-11 08:59:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _help(&self) -> ClapResult<()> {
|
2016-08-20 21:14:24 +00:00
|
|
|
let mut buf = vec![];
|
|
|
|
try!(Help::write_parser_help(&mut buf, self));
|
2016-08-22 00:45:10 +00:00
|
|
|
// let out = io::stdout();
|
|
|
|
// let mut out_buf = BufWriter::new(out.lock());
|
|
|
|
// try!(out_buf.write(&*buf));
|
2016-01-11 08:59:56 +00:00
|
|
|
Err(Error {
|
2016-08-20 21:14:24 +00:00
|
|
|
message: unsafe { String::from_utf8_unchecked(buf) },
|
2016-01-11 08:59:56 +00:00
|
|
|
kind: ErrorKind::HelpDisplayed,
|
|
|
|
info: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _version(&self) -> ClapResult<()> {
|
|
|
|
let out = io::stdout();
|
|
|
|
let mut buf_w = BufWriter::new(out.lock());
|
|
|
|
try!(self.print_version(&mut buf_w));
|
|
|
|
Err(Error {
|
|
|
|
message: String::new(),
|
|
|
|
kind: ErrorKind::VersionDisplayed,
|
|
|
|
info: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_long_arg(&mut self,
|
|
|
|
matcher: &mut ArgMatcher<'a>,
|
|
|
|
full_arg: &OsStr)
|
2016-05-06 21:35:53 +00:00
|
|
|
-> ClapResult<Option<&'b str>> {
|
|
|
|
// maybe here lifetime should be 'a
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("fn=parse_long_arg;");
|
2016-01-11 08:59:56 +00:00
|
|
|
let mut val = None;
|
2016-01-22 04:18:52 +00:00
|
|
|
debug!("Does it contain '='...");
|
2016-01-11 08:59:56 +00:00
|
|
|
let arg = if full_arg.contains_byte(b'=') {
|
|
|
|
let (p0, p1) = full_arg.trim_left_matches(b'-').split_at_byte(b'=');
|
2016-01-22 04:18:52 +00:00
|
|
|
sdebugln!("Yes '{:?}'", p1);
|
2016-01-11 08:59:56 +00:00
|
|
|
val = Some(p1);
|
|
|
|
p0
|
|
|
|
} else {
|
2016-01-22 04:18:52 +00:00
|
|
|
sdebugln!("No");
|
2016-01-11 08:59:56 +00:00
|
|
|
full_arg.trim_left_matches(b'-')
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(opt) = self.opts
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
2016-10-21 13:41:59 +00:00
|
|
|
.find(|v| {
|
|
|
|
(v.long.is_some() && &*v.long.unwrap() == arg) ||
|
2016-09-30 21:54:14 +00:00
|
|
|
(v.aliases.is_some() &&
|
|
|
|
v.aliases
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|&(n, _)| n == &*arg))
|
2016-10-21 13:41:59 +00:00
|
|
|
}) {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("Found valid opt '{}'", opt.to_string());
|
2016-01-11 08:59:56 +00:00
|
|
|
let ret = try!(self.parse_opt(val, opt, matcher));
|
|
|
|
arg_post_processing!(self, opt, matcher);
|
|
|
|
|
|
|
|
return Ok(ret);
|
|
|
|
} else if let Some(flag) = self.flags
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
2016-10-21 13:41:59 +00:00
|
|
|
.find(|v| {
|
|
|
|
(v.long.is_some() && &*v.long.unwrap() == arg) ||
|
2016-10-03 15:01:04 +00:00
|
|
|
(v.aliases.is_some() &&
|
|
|
|
v.aliases
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|&(n, _)| n == &*arg))
|
2016-10-21 13:41:59 +00:00
|
|
|
}) {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("Found valid flag '{}'", flag.to_string());
|
2016-01-11 08:59:56 +00:00
|
|
|
// Only flags could be help or version, and we need to check the raw long
|
|
|
|
// so this is the first point to check
|
|
|
|
try!(self.check_for_help_and_version_str(&arg));
|
|
|
|
|
|
|
|
try!(self.parse_flag(flag, matcher));
|
|
|
|
|
|
|
|
// Handle conflicts, requirements, etc.
|
|
|
|
arg_post_processing!(self, flag, matcher);
|
|
|
|
|
2016-07-01 17:47:39 +00:00
|
|
|
return Ok(None);
|
|
|
|
} else if self.is_set(AppSettings::AllowLeadingHyphen) {
|
2016-01-11 08:59:56 +00:00
|
|
|
return Ok(None);
|
2016-10-21 13:39:17 +00:00
|
|
|
} else if self.is_set(AppSettings::AllowNegativeNumbers) &&
|
|
|
|
(arg.to_string_lossy().parse::<i64>().is_ok() ||
|
|
|
|
arg.to_string_lossy().parse::<f64>().is_ok()) {
|
|
|
|
return Ok(None);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("Didn't match anything");
|
2016-01-11 08:59:56 +00:00
|
|
|
self.did_you_mean_error(arg.to_str().expect(INVALID_UTF8), matcher).map(|_| None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_short_arg(&mut self,
|
|
|
|
matcher: &mut ArgMatcher<'a>,
|
|
|
|
full_arg: &OsStr)
|
|
|
|
-> ClapResult<Option<&'a str>> {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("fn=parse_short_arg;");
|
2016-01-11 08:59:56 +00:00
|
|
|
// let mut utf8 = true;
|
|
|
|
let arg_os = full_arg.trim_left_matches(b'-');
|
|
|
|
let arg = arg_os.to_string_lossy();
|
|
|
|
|
|
|
|
for c in arg.chars() {
|
|
|
|
// Check for matching short options, and return the name if there is no trailing
|
|
|
|
// concatenated value: -oval
|
|
|
|
// Option: -o
|
|
|
|
// Value: val
|
|
|
|
if let Some(opt) = self.opts
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
|
|
|
.find(|&v| v.short.is_some() && v.short.unwrap() == c) {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("Found valid short opt -{} in '{}'", c, arg);
|
2016-01-11 08:59:56 +00:00
|
|
|
// Check for trailing concatenated value
|
2016-01-22 04:18:52 +00:00
|
|
|
let p: Vec<_> = arg.splitn(2, c).collect();
|
2016-05-06 21:35:53 +00:00
|
|
|
debugln!("arg: {:?}, arg_os: {:?}, full_arg: {:?}",
|
|
|
|
arg,
|
|
|
|
arg_os,
|
|
|
|
full_arg);
|
2016-01-22 17:58:56 +00:00
|
|
|
debugln!("p[0]: {:?}, p[1]: {:?}", p[0].as_bytes(), p[1].as_bytes());
|
|
|
|
let i = p[0].as_bytes().len() + 1;
|
|
|
|
let val = if p[1].as_bytes().len() > 0 {
|
2016-05-06 21:35:53 +00:00
|
|
|
debugln!("setting val: {:?} (bytes), {:?} (ascii)",
|
|
|
|
arg_os.split_at(i).1.as_bytes(),
|
|
|
|
arg_os.split_at(i).1);
|
2016-01-22 17:58:56 +00:00
|
|
|
Some(arg_os.split_at(i).1)
|
2016-01-22 04:18:52 +00:00
|
|
|
} else {
|
|
|
|
None
|
2016-01-11 08:59:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Default to "we're expecting a value later"
|
2016-01-22 04:18:52 +00:00
|
|
|
let ret = try!(self.parse_opt(val, opt, matcher));
|
2016-01-11 08:59:56 +00:00
|
|
|
|
|
|
|
arg_post_processing!(self, opt, matcher);
|
|
|
|
|
|
|
|
return Ok(ret);
|
2016-05-06 21:35:53 +00:00
|
|
|
} else if let Some(flag) = self.flags
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
|
|
|
.find(|&v| v.short.is_some() && v.short.unwrap() == c) {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("Found valid short flag -{}", c);
|
2016-01-11 08:59:56 +00:00
|
|
|
// Only flags can be help or version
|
|
|
|
try!(self.check_for_help_and_version_char(c));
|
|
|
|
try!(self.parse_flag(flag, matcher));
|
|
|
|
// Handle conflicts, requirements, overrides, etc.
|
|
|
|
// Must be called here due to mutablilty
|
|
|
|
arg_post_processing!(self, flag, matcher);
|
2016-10-21 13:39:17 +00:00
|
|
|
} else if !(self.is_set(AppSettings::AllowLeadingHyphen) ||
|
|
|
|
self.is_set(AppSettings::AllowNegativeNumbers)) {
|
2016-01-11 08:59:56 +00:00
|
|
|
let mut arg = String::new();
|
|
|
|
arg.push('-');
|
|
|
|
arg.push(c);
|
2016-05-06 21:35:53 +00:00
|
|
|
return Err(Error::unknown_argument(&*arg,
|
|
|
|
"",
|
2016-05-30 08:07:44 +00:00
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_opt(&self,
|
|
|
|
val: Option<&OsStr>,
|
|
|
|
opt: &OptBuilder<'a, 'b>,
|
|
|
|
matcher: &mut ArgMatcher<'a>)
|
|
|
|
-> ClapResult<Option<&'a str>> {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("fn=parse_opt;");
|
|
|
|
validate_multiples!(self, opt, matcher);
|
2016-06-29 17:16:31 +00:00
|
|
|
let mut has_eq = false;
|
2016-01-22 04:18:52 +00:00
|
|
|
|
|
|
|
debug!("Checking for val...");
|
2016-01-27 16:28:52 +00:00
|
|
|
if let Some(fv) = val {
|
2016-06-29 17:16:31 +00:00
|
|
|
has_eq = fv.starts_with(&[b'=']);
|
2016-01-27 16:28:52 +00:00
|
|
|
let v = fv.trim_left_matches(b'=');
|
2016-02-21 16:18:26 +00:00
|
|
|
if !opt.is_set(ArgSettings::EmptyValues) && v.len_() == 0 {
|
2016-01-22 04:18:52 +00:00
|
|
|
sdebugln!("Found Empty - Error");
|
2016-09-05 19:29:40 +00:00
|
|
|
return Err(Error::empty_value(opt,
|
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-03-09 22:35:40 +00:00
|
|
|
sdebugln!("Found - {:?}, len: {}", v, v.len_());
|
2016-06-29 17:16:31 +00:00
|
|
|
debugln!("{:?} contains '='...{:?}", fv, fv.starts_with(&[b'=']));
|
2016-01-22 04:18:52 +00:00
|
|
|
try!(self.add_val_to_arg(opt, v, matcher));
|
2016-05-06 21:35:53 +00:00
|
|
|
} else {
|
|
|
|
sdebugln!("None");
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
|
2016-01-25 18:13:11 +00:00
|
|
|
matcher.inc_occurrence_of(opt.name);
|
|
|
|
// Increment or create the group "args"
|
|
|
|
self.groups_for_arg(opt.name).and_then(|vec| Some(matcher.inc_occurrences_of(&*vec)));
|
|
|
|
|
2016-09-05 19:29:40 +00:00
|
|
|
if val.is_none() ||
|
2016-10-21 13:41:59 +00:00
|
|
|
!has_eq &&
|
|
|
|
(opt.is_set(ArgSettings::Multiple) && !opt.is_set(ArgSettings::RequireDelimiter) &&
|
|
|
|
matcher.needs_more_vals(opt)) {
|
2016-01-11 08:59:56 +00:00
|
|
|
return Ok(Some(opt.name));
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_val_to_arg<A>(&self,
|
2016-05-06 21:35:53 +00:00
|
|
|
arg: &A,
|
|
|
|
val: &OsStr,
|
|
|
|
matcher: &mut ArgMatcher<'a>)
|
|
|
|
-> ClapResult<Option<&'a str>>
|
|
|
|
where A: AnyArg<'a, 'b> + Display
|
|
|
|
{
|
2016-01-26 07:03:30 +00:00
|
|
|
debugln!("fn=add_val_to_arg;");
|
|
|
|
let mut ret = None;
|
2016-06-05 05:09:07 +00:00
|
|
|
if !(self.trailing_vals && self.is_set(AppSettings::DontDelimitTrailingValues)) {
|
|
|
|
if let Some(delim) = arg.val_delim() {
|
|
|
|
if val.is_empty_() {
|
|
|
|
ret = try!(self.add_single_val_to_arg(arg, val, matcher));
|
|
|
|
} else {
|
|
|
|
for v in val.split(delim as u32 as u8) {
|
|
|
|
ret = try!(self.add_single_val_to_arg(arg, v, matcher));
|
|
|
|
}
|
fix(Options): values using delimiters no longer parse additional values after a trailing space
Imagine two args, an option `-o` which accepts mutliple values, and a positional arg `<file>`.
Prior to this change the following (incorrect) parses would have happened:
```
$ prog -o 1,2 some.txt
o = 1, 2, file
file =
```
Only when delimters are used, spaces stop parsing values for the option. The follow are now correct
```
$ prog -o 1,2 some.txt
o = 1, 2
file = some.txt
$ prog some.txt -o 1 2
o = 1, 2
file = some.txt
```
This still has the bi-product of:
```
$ prog -o 1 2 some.txt
o = 1, 2, some.txt
file =
```
This is simply a CLI design and documentation issue (i.e. don't allow options with unlimited
multiple values with positionaln args, or clearly document that positional args go first). This
will also be helped by the upcoming `Arg::require_delimiter`
Relates to #546
2016-06-29 17:21:30 +00:00
|
|
|
// If there was a delimiter used, we're not looking for more values
|
2016-09-05 19:29:40 +00:00
|
|
|
if val.contains_byte(delim as u32 as u8) ||
|
|
|
|
arg.is_set(ArgSettings::RequireDelimiter) {
|
fix(Options): values using delimiters no longer parse additional values after a trailing space
Imagine two args, an option `-o` which accepts mutliple values, and a positional arg `<file>`.
Prior to this change the following (incorrect) parses would have happened:
```
$ prog -o 1,2 some.txt
o = 1, 2, file
file =
```
Only when delimters are used, spaces stop parsing values for the option. The follow are now correct
```
$ prog -o 1,2 some.txt
o = 1, 2
file = some.txt
$ prog some.txt -o 1 2
o = 1, 2
file = some.txt
```
This still has the bi-product of:
```
$ prog -o 1 2 some.txt
o = 1, 2, some.txt
file =
```
This is simply a CLI design and documentation issue (i.e. don't allow options with unlimited
multiple values with positionaln args, or clearly document that positional args go first). This
will also be helped by the upcoming `Arg::require_delimiter`
Relates to #546
2016-06-29 17:21:30 +00:00
|
|
|
ret = None;
|
2016-06-29 03:38:22 +00:00
|
|
|
}
|
2016-03-30 02:25:13 +00:00
|
|
|
}
|
2016-06-05 05:09:07 +00:00
|
|
|
} else {
|
|
|
|
ret = try!(self.add_single_val_to_arg(arg, val, matcher));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-01-26 15:51:47 +00:00
|
|
|
} else {
|
|
|
|
ret = try!(self.add_single_val_to_arg(arg, val, matcher));
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
|
|
|
Ok(ret)
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 21:35:53 +00:00
|
|
|
fn add_single_val_to_arg<A>(&self,
|
|
|
|
arg: &A,
|
|
|
|
v: &OsStr,
|
|
|
|
matcher: &mut ArgMatcher<'a>)
|
|
|
|
-> ClapResult<Option<&'a str>>
|
2016-03-16 14:17:00 +00:00
|
|
|
where A: AnyArg<'a, 'b> + Display
|
2016-01-26 15:51:47 +00:00
|
|
|
{
|
|
|
|
debugln!("adding val: {:?}", v);
|
|
|
|
matcher.add_val_to(arg.name(), v);
|
|
|
|
|
|
|
|
// Increment or create the group "args"
|
|
|
|
if let Some(grps) = self.groups_for_arg(arg.name()) {
|
|
|
|
for grp in grps {
|
|
|
|
matcher.add_val_to(&*grp, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The validation must come AFTER inserting into 'matcher' or the usage string
|
|
|
|
// can't be built
|
|
|
|
self.validate_value(arg, v, matcher)
|
|
|
|
}
|
|
|
|
|
2016-05-06 21:35:53 +00:00
|
|
|
fn validate_value<A>(&self,
|
|
|
|
arg: &A,
|
|
|
|
val: &OsStr,
|
|
|
|
matcher: &ArgMatcher<'a>)
|
|
|
|
-> ClapResult<Option<&'a str>>
|
|
|
|
where A: AnyArg<'a, 'b> + Display
|
|
|
|
{
|
2016-01-22 17:58:56 +00:00
|
|
|
debugln!("fn=validate_value; val={:?}", val);
|
|
|
|
if self.is_set(AppSettings::StrictUtf8) && val.to_str().is_none() {
|
2016-05-30 08:07:44 +00:00
|
|
|
return Err(Error::invalid_utf8(&*self.create_current_usage(matcher), self.color()));
|
2016-01-22 17:58:56 +00:00
|
|
|
}
|
2016-08-20 21:33:58 +00:00
|
|
|
if let Some(p_vals) = arg.possible_vals() {
|
2016-01-22 17:58:56 +00:00
|
|
|
let val_str = val.to_string_lossy();
|
|
|
|
if !p_vals.contains(&&*val_str) {
|
2016-05-06 21:35:53 +00:00
|
|
|
return Err(Error::invalid_value(val_str,
|
|
|
|
p_vals,
|
|
|
|
arg,
|
2016-05-30 08:07:44 +00:00
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-06 21:35:53 +00:00
|
|
|
if !arg.is_set(ArgSettings::EmptyValues) && val.is_empty_() &&
|
|
|
|
matcher.contains(&*arg.name()) {
|
2016-05-30 08:07:44 +00:00
|
|
|
return Err(Error::empty_value(arg, &*self.create_current_usage(matcher), self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-08-20 21:33:58 +00:00
|
|
|
if let Some(vtor) = arg.validator() {
|
2016-01-22 17:58:56 +00:00
|
|
|
if let Err(e) = vtor(val.to_string_lossy().into_owned()) {
|
2016-05-30 08:07:44 +00:00
|
|
|
return Err(Error::value_validation(e, self.color()));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-05 00:38:17 +00:00
|
|
|
if matcher.needs_more_vals(arg) {
|
2016-01-11 08:59:56 +00:00
|
|
|
return Ok(Some(arg.name()));
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2016-05-06 21:35:53 +00:00
|
|
|
fn parse_flag(&self,
|
|
|
|
flag: &FlagBuilder<'a, 'b>,
|
|
|
|
matcher: &mut ArgMatcher<'a>)
|
|
|
|
-> ClapResult<()> {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("fn=parse_flag;");
|
|
|
|
validate_multiples!(self, flag, matcher);
|
|
|
|
|
2016-01-25 18:13:11 +00:00
|
|
|
matcher.inc_occurrence_of(flag.name);
|
2016-01-11 08:59:56 +00:00
|
|
|
// Increment or create the group "args"
|
2016-01-22 04:18:52 +00:00
|
|
|
self.groups_for_arg(flag.name).and_then(|vec| Some(matcher.inc_occurrences_of(&*vec)));
|
2016-01-11 08:59:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate_blacklist(&self, matcher: &mut ArgMatcher) -> ClapResult<()> {
|
2016-10-31 04:35:23 +00:00
|
|
|
debugln!("fn=validate_blacklist;blacklist={:?}", self.blacklist);
|
2016-01-11 08:59:56 +00:00
|
|
|
macro_rules! build_err {
|
2016-01-22 04:18:52 +00:00
|
|
|
($me:ident, $name:expr, $matcher:ident) => ({
|
2016-10-31 04:35:23 +00:00
|
|
|
debugln!("macro=build_err;name={}", $name);
|
|
|
|
let mut c_with = find_from!($me, $name, blacklist, &$matcher);
|
|
|
|
c_with = if c_with.is_none() {
|
|
|
|
if let Some(aa) = $me.find_any_arg($name) {
|
|
|
|
if let Some(bl) = aa.blacklist() {
|
|
|
|
if let Some(arg_name) = bl.iter().find(|arg| $matcher.contains(arg)) {
|
|
|
|
if let Some(aa) = $me.find_any_arg(arg_name) {
|
|
|
|
Some(aa.to_string())
|
|
|
|
} else {
|
|
|
|
c_with
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c_with
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c_with
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c_with
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c_with
|
|
|
|
};
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("'{:?}' conflicts with '{}'", c_with, $name);
|
2016-05-09 03:20:50 +00:00
|
|
|
$matcher.remove($name);
|
2016-01-11 08:59:56 +00:00
|
|
|
let usg = $me.create_current_usage($matcher);
|
2016-10-31 04:35:23 +00:00
|
|
|
if let Some(f) = $me.find_flag($name) {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("It was a flag...");
|
2016-05-30 08:07:44 +00:00
|
|
|
Error::argument_conflict(f, c_with, &*usg, self.color())
|
2016-10-31 04:35:23 +00:00
|
|
|
} else if let Some(o) = $me.find_option($name) {
|
|
|
|
debugln!("It was an option...");
|
2016-05-30 08:07:44 +00:00
|
|
|
Error::argument_conflict(o, c_with, &*usg, self.color())
|
2016-01-11 08:59:56 +00:00
|
|
|
} else {
|
2016-10-31 04:35:23 +00:00
|
|
|
match $me.find_positional($name) {
|
2016-01-22 04:18:52 +00:00
|
|
|
Some(p) => {
|
|
|
|
debugln!("It was a positional...");
|
2016-05-30 08:07:44 +00:00
|
|
|
Error::argument_conflict(p, c_with, &*usg, self.color())
|
2016-01-22 04:18:52 +00:00
|
|
|
},
|
2016-01-11 08:59:56 +00:00
|
|
|
None => panic!(INTERNAL_ERROR_MSG)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-01-22 04:18:52 +00:00
|
|
|
for name in &self.blacklist {
|
|
|
|
debugln!("Checking blacklisted name: {}", name);
|
|
|
|
if self.groups.contains_key(name) {
|
|
|
|
debugln!("groups contains it...");
|
2016-01-11 08:59:56 +00:00
|
|
|
for n in self.arg_names_in_group(name) {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("Checking arg '{}' in group...", n);
|
2016-06-08 04:10:56 +00:00
|
|
|
if matcher.contains(n) {
|
2016-01-22 04:18:52 +00:00
|
|
|
debugln!("matcher contains it...");
|
2016-10-31 04:35:23 +00:00
|
|
|
return Err(build_err!(self, &n, matcher));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-22 04:18:52 +00:00
|
|
|
} else if matcher.contains(name) {
|
|
|
|
debugln!("matcher contains it...");
|
2016-10-31 04:35:23 +00:00
|
|
|
return Err(build_err!(self, name, matcher));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validate_num_args(&self, matcher: &mut ArgMatcher) -> ClapResult<()> {
|
2016-01-26 07:03:30 +00:00
|
|
|
debugln!("fn=validate_num_args;");
|
2016-01-11 08:59:56 +00:00
|
|
|
for (name, ma) in matcher.iter() {
|
|
|
|
if self.groups.contains_key(&**name) {
|
|
|
|
continue;
|
2016-07-26 00:56:22 +00:00
|
|
|
} else if let Some(opt) = self.opts
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
|
|
|
.find(|o| &o.name == name) {
|
2016-07-26 00:56:22 +00:00
|
|
|
try!(self._validate_num_vals(opt, ma, matcher));
|
|
|
|
} else if let Some(pos) = self.positionals
|
2016-09-05 19:29:40 +00:00
|
|
|
.values()
|
|
|
|
.find(|p| &p.name == name) {
|
2016-07-26 00:56:22 +00:00
|
|
|
try!(self._validate_num_vals(pos, ma, matcher));
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-26 07:03:30 +00:00
|
|
|
fn _validate_num_vals<A>(&self, a: &A, ma: &MatchedArg, matcher: &ArgMatcher) -> ClapResult<()>
|
2016-03-16 14:17:00 +00:00
|
|
|
where A: AnyArg<'a, 'b> + Display
|
2016-01-26 07:03:30 +00:00
|
|
|
{
|
|
|
|
debugln!("fn=_validate_num_vals;");
|
|
|
|
if let Some(num) = a.num_vals() {
|
|
|
|
debugln!("num_vals set: {}", num);
|
|
|
|
let should_err = if a.is_set(ArgSettings::Multiple) {
|
2016-02-02 12:29:04 +00:00
|
|
|
((ma.vals.len() as u64) % num) != 0
|
2016-01-26 07:03:30 +00:00
|
|
|
} else {
|
2016-02-02 12:29:04 +00:00
|
|
|
num != (ma.vals.len() as u64)
|
2016-01-26 07:03:30 +00:00
|
|
|
};
|
|
|
|
if should_err {
|
|
|
|
debugln!("Sending error WrongNumberOfValues");
|
2016-05-06 21:35:53 +00:00
|
|
|
return Err(Error::wrong_number_of_values(a,
|
|
|
|
num,
|
|
|
|
if a.is_set(ArgSettings::Multiple) {
|
|
|
|
(ma.vals.len() % num as usize)
|
|
|
|
} else {
|
|
|
|
ma.vals.len()
|
|
|
|
},
|
|
|
|
if ma.vals.len() == 1 ||
|
|
|
|
(a.is_set(ArgSettings::Multiple) &&
|
|
|
|
(ma.vals.len() % num as usize) ==
|
|
|
|
1) {
|
|
|
|
"as"
|
|
|
|
} else {
|
|
|
|
"ere"
|
|
|
|
},
|
2016-05-30 08:07:44 +00:00
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(num) = a.max_vals() {
|
|
|
|
debugln!("max_vals set: {}", num);
|
2016-02-02 12:29:04 +00:00
|
|
|
if (ma.vals.len() as u64) > num {
|
2016-01-26 07:03:30 +00:00
|
|
|
debugln!("Sending error TooManyValues");
|
2016-05-06 21:35:53 +00:00
|
|
|
return Err(Error::too_many_values(ma.vals
|
2016-09-05 19:29:40 +00:00
|
|
|
.get(ma.vals
|
|
|
|
.keys()
|
|
|
|
.last()
|
|
|
|
.expect(INTERNAL_ERROR_MSG))
|
|
|
|
.expect(INTERNAL_ERROR_MSG)
|
|
|
|
.to_str()
|
|
|
|
.expect(INVALID_UTF8),
|
2016-05-06 21:35:53 +00:00
|
|
|
a,
|
2016-05-30 08:07:44 +00:00
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(num) = a.min_vals() {
|
|
|
|
debugln!("min_vals set: {}", num);
|
2016-02-02 12:29:04 +00:00
|
|
|
if (ma.vals.len() as u64) < num {
|
2016-01-26 07:03:30 +00:00
|
|
|
debugln!("Sending error TooFewValues");
|
2016-05-06 21:35:53 +00:00
|
|
|
return Err(Error::too_few_values(a,
|
|
|
|
num,
|
|
|
|
ma.vals.len(),
|
2016-05-30 08:07:44 +00:00
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()));
|
2016-01-26 07:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
fn validate_required(&self, matcher: &ArgMatcher) -> ClapResult<()> {
|
2016-01-23 11:46:16 +00:00
|
|
|
'outer: for name in &self.required {
|
2016-01-11 08:59:56 +00:00
|
|
|
if matcher.contains(name) {
|
|
|
|
continue 'outer;
|
|
|
|
}
|
|
|
|
if let Some(grp) = self.groups.get(name) {
|
|
|
|
for arg in &grp.args {
|
2016-05-06 21:35:53 +00:00
|
|
|
if matcher.contains(arg) {
|
|
|
|
continue 'outer;
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.groups.values().any(|g| g.args.contains(name)) {
|
|
|
|
continue 'outer;
|
|
|
|
}
|
2016-09-05 19:14:47 +00:00
|
|
|
if let Some(a) = self.flags.iter().find(|f| &f.name == name) {
|
2016-05-06 21:35:53 +00:00
|
|
|
if self.is_missing_required_ok(a, matcher) {
|
|
|
|
continue 'outer;
|
|
|
|
}
|
2016-09-05 19:14:47 +00:00
|
|
|
} else if let Some(a) = self.opts.iter().find(|o| &o.name == name) {
|
2016-05-06 21:35:53 +00:00
|
|
|
if self.is_missing_required_ok(a, matcher) {
|
|
|
|
continue 'outer;
|
|
|
|
}
|
2016-09-05 19:14:47 +00:00
|
|
|
} else if let Some(a) = self.positionals.values().find(|p| &p.name == name) {
|
2016-05-06 21:35:53 +00:00
|
|
|
if self.is_missing_required_ok(a, matcher) {
|
|
|
|
continue 'outer;
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-09-05 19:29:40 +00:00
|
|
|
let err =
|
|
|
|
if self.settings.is_set(AppSettings::ArgRequiredElseHelp) && matcher.is_empty() {
|
|
|
|
self._help().unwrap_err()
|
|
|
|
} else {
|
|
|
|
let mut reqs = self.required.iter().map(|&r| &*r).collect::<Vec<_>>();
|
|
|
|
reqs.retain(|n| !matcher.contains(n));
|
|
|
|
reqs.dedup();
|
|
|
|
Error::missing_required_argument(
|
2016-05-02 23:02:24 +00:00
|
|
|
&*self.get_required_from(&*reqs, Some(matcher))
|
2016-01-11 08:59:56 +00:00
|
|
|
.iter()
|
|
|
|
.fold(String::new(),
|
2016-03-14 01:32:44 +00:00
|
|
|
|acc, s| acc + &format!("\n {}", Format::Error(s))[..]),
|
2016-05-30 08:07:44 +00:00
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color())
|
2016-09-05 19:29:40 +00:00
|
|
|
};
|
2016-01-11 08:59:56 +00:00
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-05-06 21:35:53 +00:00
|
|
|
fn is_missing_required_ok<A>(&self, a: &A, matcher: &ArgMatcher) -> bool
|
|
|
|
where A: AnyArg<'a, 'b>
|
|
|
|
{
|
2016-01-11 08:59:56 +00:00
|
|
|
if let Some(bl) = a.blacklist() {
|
|
|
|
for n in bl.iter() {
|
2016-05-06 21:35:53 +00:00
|
|
|
if matcher.contains(n) ||
|
|
|
|
self.groups
|
2016-09-05 19:29:40 +00:00
|
|
|
.get(n)
|
|
|
|
.map_or(false, |g| g.args.iter().any(|an| matcher.contains(an))) {
|
2016-01-11 08:59:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 17:32:36 +00:00
|
|
|
} else if let Some(ru) = a.required_unless() {
|
2016-07-23 21:21:41 +00:00
|
|
|
let mut found_any = false;
|
2016-05-02 17:32:36 +00:00
|
|
|
for n in ru.iter() {
|
2016-05-06 21:35:53 +00:00
|
|
|
if matcher.contains(n) ||
|
|
|
|
self.groups
|
2016-09-05 19:29:40 +00:00
|
|
|
.get(n)
|
|
|
|
.map_or(false, |g| g.args.iter().any(|an| matcher.contains(an))) {
|
|
|
|
if !a.is_set(ArgSettings::RequiredUnlessAll) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
found_any = true;
|
|
|
|
} else if a.is_set(ArgSettings::RequiredUnlessAll) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-02 17:32:36 +00:00
|
|
|
}
|
2016-07-23 21:21:41 +00:00
|
|
|
return found_any;
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn did_you_mean_error(&self, arg: &str, matcher: &mut ArgMatcher<'a>) -> ClapResult<()> {
|
|
|
|
// Didn't match a flag or option...maybe it was a typo and close to one
|
2016-05-06 21:35:53 +00:00
|
|
|
let suffix =
|
|
|
|
suggestions::did_you_mean_suffix(arg,
|
|
|
|
self.long_list.iter(),
|
|
|
|
suggestions::DidYouMeanMessageStyle::LongFlag);
|
2016-01-11 08:59:56 +00:00
|
|
|
|
|
|
|
// Add the arg to the matches to build a proper usage string
|
|
|
|
if let Some(name) = suffix.1 {
|
|
|
|
if let Some(opt) = self.opts
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
|
|
|
.find(|o| o.long.is_some() && o.long.unwrap() == name) {
|
2016-05-06 21:35:53 +00:00
|
|
|
self.groups_for_arg(&*opt.name)
|
|
|
|
.and_then(|grps| Some(matcher.inc_occurrences_of(&*grps)));
|
2016-02-14 06:10:44 +00:00
|
|
|
matcher.insert(&*opt.name);
|
2016-01-11 08:59:56 +00:00
|
|
|
} else if let Some(flg) = self.flags
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
|
|
|
.find(|f| f.long.is_some() && f.long.unwrap() == name) {
|
2016-05-06 21:35:53 +00:00
|
|
|
self.groups_for_arg(&*flg.name)
|
|
|
|
.and_then(|grps| Some(matcher.inc_occurrences_of(&*grps)));
|
2016-02-14 06:10:44 +00:00
|
|
|
matcher.insert(&*flg.name);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 05:28:52 +00:00
|
|
|
let used_arg = format!("--{}", arg);
|
2016-09-05 19:29:40 +00:00
|
|
|
Err(Error::unknown_argument(&*used_arg,
|
|
|
|
&*suffix.0,
|
|
|
|
&*self.create_current_usage(matcher),
|
|
|
|
self.color()))
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a usage string if one was not provided by the user manually. This happens just
|
|
|
|
// after all arguments were parsed, but before any subcommands have been parsed
|
|
|
|
// (so as to give subcommands their own usage recursively)
|
2016-04-01 00:33:28 +00:00
|
|
|
pub fn create_usage(&self, used: &[&str]) -> String {
|
2016-01-22 17:58:56 +00:00
|
|
|
debugln!("fn=create_usage;");
|
2016-01-11 08:59:56 +00:00
|
|
|
let mut usage = String::with_capacity(75);
|
2016-03-14 01:32:44 +00:00
|
|
|
usage.push_str("USAGE:\n ");
|
2016-06-08 04:10:56 +00:00
|
|
|
usage.push_str(&self.create_usage_no_title(used));
|
2016-04-03 04:17:22 +00:00
|
|
|
usage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a usage string (*without title*) if one was not provided by the user
|
|
|
|
// manually. This happens just
|
|
|
|
// after all arguments were parsed, but before any subcommands have been parsed
|
|
|
|
// (so as to give subcommands their own usage recursively)
|
|
|
|
pub fn create_usage_no_title(&self, used: &[&str]) -> String {
|
2016-05-02 23:02:24 +00:00
|
|
|
debugln!("fn=create_usage_no_title;");
|
2016-04-03 04:17:22 +00:00
|
|
|
let mut usage = String::with_capacity(75);
|
2016-01-11 08:59:56 +00:00
|
|
|
if let Some(u) = self.meta.usage_str {
|
|
|
|
usage.push_str(&*u);
|
2016-03-08 15:39:43 +00:00
|
|
|
} else if used.is_empty() {
|
2016-05-06 21:35:53 +00:00
|
|
|
usage.push_str(&*self.meta
|
2016-09-05 19:29:40 +00:00
|
|
|
.usage
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(self.meta
|
|
|
|
.bin_name
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&self.meta.name)));
|
2016-05-02 23:02:24 +00:00
|
|
|
let mut reqs: Vec<&str> = self.required().map(|r| &**r).collect();
|
|
|
|
reqs.dedup();
|
2016-01-11 08:59:56 +00:00
|
|
|
let req_string = self.get_required_from(&reqs, None)
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
|
|
|
.fold(String::new(), |a, s| a + &format!(" {}", s)[..]);
|
2016-01-11 08:59:56 +00:00
|
|
|
|
2016-06-24 03:45:52 +00:00
|
|
|
let flags = self.needs_flags_tag();
|
|
|
|
if flags && !self.is_set(AppSettings::UnifiedHelpMessage) {
|
2016-01-11 08:59:56 +00:00
|
|
|
usage.push_str(" [FLAGS]");
|
2016-06-24 03:45:52 +00:00
|
|
|
} else if flags {
|
2016-01-11 08:59:56 +00:00
|
|
|
usage.push_str(" [OPTIONS]");
|
|
|
|
}
|
2016-05-06 21:35:53 +00:00
|
|
|
if !self.is_set(AppSettings::UnifiedHelpMessage) && self.has_opts() &&
|
|
|
|
self.opts.iter().any(|a| !a.settings.is_set(ArgSettings::Required)) {
|
2016-01-11 08:59:56 +00:00
|
|
|
usage.push_str(" [OPTIONS]");
|
|
|
|
}
|
|
|
|
|
|
|
|
usage.push_str(&req_string[..]);
|
|
|
|
|
|
|
|
// places a '--' in the usage string if there are args and options
|
|
|
|
// supporting multiple values
|
2016-05-06 21:35:53 +00:00
|
|
|
if self.has_positionals() &&
|
|
|
|
self.opts.iter().any(|a| a.settings.is_set(ArgSettings::Multiple)) &&
|
|
|
|
self.positionals.values().any(|a| !a.settings.is_set(ArgSettings::Required)) &&
|
|
|
|
!self.has_subcommands() {
|
2016-01-11 08:59:56 +00:00
|
|
|
usage.push_str(" [--]")
|
|
|
|
}
|
2016-05-06 21:35:53 +00:00
|
|
|
if self.has_positionals() &&
|
|
|
|
self.positionals.values().any(|a| !a.settings.is_set(ArgSettings::Required)) {
|
2016-06-24 03:45:52 +00:00
|
|
|
if let Some(args_tag) = self.get_args_tag() {
|
|
|
|
usage.push_str(&*args_tag);
|
2016-06-04 03:14:02 +00:00
|
|
|
} else {
|
|
|
|
usage.push_str(" [ARGS]");
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-27 16:56:06 +00:00
|
|
|
if self.has_subcommands() && !self.is_set(AppSettings::SubcommandRequired) {
|
2016-01-11 08:59:56 +00:00
|
|
|
usage.push_str(" [SUBCOMMAND]");
|
2016-03-27 16:56:06 +00:00
|
|
|
} else if self.is_set(AppSettings::SubcommandRequired) && self.has_subcommands() {
|
2016-01-11 08:59:56 +00:00
|
|
|
usage.push_str(" <SUBCOMMAND>");
|
|
|
|
}
|
2016-03-08 15:39:43 +00:00
|
|
|
} else {
|
|
|
|
self.smart_usage(&mut usage, used);
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
usage.shrink_to_fit();
|
|
|
|
usage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a context aware usage string, or "smart usage" from currently used
|
|
|
|
// args, and requirements
|
|
|
|
fn smart_usage(&self, usage: &mut String, used: &[&str]) {
|
2016-05-02 23:02:24 +00:00
|
|
|
debugln!("fn=smart_usage;");
|
2016-01-11 08:59:56 +00:00
|
|
|
let mut hs: Vec<&str> = self.required().map(|s| &**s).collect();
|
2016-02-14 06:10:44 +00:00
|
|
|
hs.extend_from_slice(used);
|
2016-05-02 23:02:24 +00:00
|
|
|
|
2016-01-11 08:59:56 +00:00
|
|
|
let r_string = self.get_required_from(&hs, None)
|
2016-09-05 19:29:40 +00:00
|
|
|
.iter()
|
|
|
|
.fold(String::new(), |acc, s| acc + &format!(" {}", s)[..]);
|
2016-05-06 21:35:53 +00:00
|
|
|
|
|
|
|
usage.push_str(&self.meta
|
2016-09-05 19:29:40 +00:00
|
|
|
.usage
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(self.meta
|
|
|
|
.bin_name
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&self.meta
|
|
|
|
.name))[..]);
|
2016-01-11 08:59:56 +00:00
|
|
|
usage.push_str(&*r_string);
|
|
|
|
if self.is_set(AppSettings::SubcommandRequired) {
|
|
|
|
usage.push_str(" <SUBCOMMAND>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prints the version to the user and exits if quit=true
|
|
|
|
fn print_version<W: Write>(&self, w: &mut W) -> ClapResult<()> {
|
|
|
|
try!(self.write_version(w));
|
|
|
|
w.flush().map_err(Error::from)
|
|
|
|
}
|
|
|
|
|
2016-05-30 08:48:13 +00:00
|
|
|
pub fn write_version<W: Write>(&self, w: &mut W) -> io::Result<()> {
|
2016-01-11 08:59:56 +00:00
|
|
|
if let Some(bn) = self.meta.bin_name.as_ref() {
|
2016-02-19 04:56:44 +00:00
|
|
|
if bn.contains(' ') {
|
2016-01-11 08:59:56 +00:00
|
|
|
// Incase we're dealing with subcommands i.e. git mv is translated to git-mv
|
2016-05-30 08:48:13 +00:00
|
|
|
write!(w,
|
2016-05-06 21:35:53 +00:00
|
|
|
"{} {}",
|
|
|
|
bn.replace(" ", "-"),
|
|
|
|
self.meta.version.unwrap_or("".into()))
|
2016-01-11 08:59:56 +00:00
|
|
|
} else {
|
2016-05-30 08:48:13 +00:00
|
|
|
write!(w,
|
2016-05-06 21:35:53 +00:00
|
|
|
"{} {}",
|
|
|
|
&self.meta.name[..],
|
|
|
|
self.meta.version.unwrap_or("".into()))
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-30 08:48:13 +00:00
|
|
|
write!(w,
|
2016-05-06 21:35:53 +00:00
|
|
|
"{} {}",
|
|
|
|
&self.meta.name[..],
|
|
|
|
self.meta.version.unwrap_or("".into()))
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_help(&self) -> ClapResult<()> {
|
|
|
|
let out = io::stdout();
|
|
|
|
let mut buf_w = BufWriter::new(out.lock());
|
|
|
|
self.write_help(&mut buf_w)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_help<W: Write>(&self, w: &mut W) -> ClapResult<()> {
|
2016-06-08 04:10:56 +00:00
|
|
|
Help::write_parser_help(w, self)
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-02-08 17:52:00 +00:00
|
|
|
|
2016-05-30 08:07:44 +00:00
|
|
|
pub fn write_help_err<W: Write>(&self, w: &mut W) -> ClapResult<()> {
|
2016-06-08 04:10:56 +00:00
|
|
|
Help::write_parser_help_to_stderr(w, self)
|
2016-05-30 08:07:44 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 17:52:00 +00:00
|
|
|
fn add_defaults(&mut self, matcher: &mut ArgMatcher<'a>) -> ClapResult<()> {
|
|
|
|
macro_rules! add_val {
|
|
|
|
($_self:ident, $a:ident, $m:ident) => {
|
|
|
|
if $m.get($a.name).is_none() {
|
2016-05-06 21:35:53 +00:00
|
|
|
try!($_self.add_val_to_arg($a, OsStr::new($a.default_val
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()), $m));
|
2016-02-08 17:52:00 +00:00
|
|
|
arg_post_processing!($_self, $a, $m);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
for o in self.opts.iter().filter(|o| o.default_val.is_some()) {
|
|
|
|
add_val!(self, o, matcher);
|
|
|
|
}
|
|
|
|
for p in self.positionals.values().filter(|p| p.default_val.is_some()) {
|
|
|
|
add_val!(self, p, matcher);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-04-01 00:22:59 +00:00
|
|
|
|
2016-10-31 04:35:23 +00:00
|
|
|
pub fn flags(&self) -> Iter<FlagBuilder<'a, 'b>> {
|
2016-04-01 00:22:59 +00:00
|
|
|
self.flags.iter()
|
|
|
|
}
|
|
|
|
|
2016-10-31 04:35:23 +00:00
|
|
|
pub fn opts(&self) -> Iter<OptBuilder<'a, 'b>> {
|
2016-04-01 00:22:59 +00:00
|
|
|
self.opts.iter()
|
|
|
|
}
|
|
|
|
|
2016-10-31 04:35:23 +00:00
|
|
|
pub fn positionals(&self) -> vec_map::Values<PosBuilder<'a, 'b>> {
|
2016-04-01 00:22:59 +00:00
|
|
|
self.positionals.values()
|
|
|
|
}
|
2016-05-30 08:07:44 +00:00
|
|
|
|
2016-10-23 23:32:42 +00:00
|
|
|
pub fn subcommands(&self) -> Iter<App> {
|
|
|
|
self.subcommands.iter()
|
|
|
|
}
|
|
|
|
|
2016-05-30 08:07:44 +00:00
|
|
|
// Should we color the output? None=determined by output location, true=yes, false=no
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn color(&self) -> ColorWhen {
|
2016-09-13 01:48:22 +00:00
|
|
|
debugln!("fn=color;");
|
2016-05-30 08:07:44 +00:00
|
|
|
debug!("Color setting...");
|
|
|
|
if self.is_set(AppSettings::ColorNever) {
|
|
|
|
sdebugln!("Never");
|
|
|
|
ColorWhen::Never
|
|
|
|
} else if self.is_set(AppSettings::ColorAlways) {
|
|
|
|
sdebugln!("Always");
|
|
|
|
ColorWhen::Always
|
|
|
|
} else {
|
|
|
|
sdebugln!("Auto");
|
|
|
|
ColorWhen::Auto
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 23:32:52 +00:00
|
|
|
|
2016-10-31 04:35:23 +00:00
|
|
|
pub fn find_any_arg(&self, arg: &str) -> Option<&AnyArg> {
|
|
|
|
if let Some(f) = self.find_flag(arg) {
|
|
|
|
return Some(f);
|
|
|
|
}
|
|
|
|
if let Some(o) = self.find_option(arg) {
|
|
|
|
return Some(o);
|
|
|
|
}
|
|
|
|
if let Some(p) = self.find_positional(arg) {
|
|
|
|
return Some(p);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2016-11-01 20:10:40 +00:00
|
|
|
|
2016-10-31 04:35:23 +00:00
|
|
|
fn find_flag(&self, name: &str) -> Option<&FlagBuilder<'a, 'b>> {
|
2016-10-23 23:32:52 +00:00
|
|
|
for f in self.flags() {
|
2016-11-01 20:10:40 +00:00
|
|
|
if f.name == name ||
|
|
|
|
f.aliases.as_ref().unwrap_or(&vec![("",false)]).iter().any(|&(n, _)| n == name) {
|
2016-10-23 23:32:52 +00:00
|
|
|
return Some(f);
|
|
|
|
}
|
|
|
|
}
|
2016-10-31 04:35:23 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_option(&self, name: &str) -> Option<&OptBuilder<'a, 'b>> {
|
2016-10-23 23:32:52 +00:00
|
|
|
for o in self.opts() {
|
2016-11-01 20:10:40 +00:00
|
|
|
if o.name == name ||
|
|
|
|
o.aliases.as_ref().unwrap_or(&vec![("",false)]).iter().any(|&(n, _)| n == name) {
|
2016-10-23 23:32:52 +00:00
|
|
|
return Some(o);
|
|
|
|
}
|
|
|
|
}
|
2016-10-31 04:35:23 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_positional(&self, name: &str) -> Option<&PosBuilder<'a, 'b>> {
|
2016-10-23 23:32:52 +00:00
|
|
|
for p in self.positionals() {
|
2016-10-31 04:35:23 +00:00
|
|
|
if p.name == name {
|
2016-10-23 23:32:52 +00:00
|
|
|
return Some(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-10-24 09:56:32 +00:00
|
|
|
#[cfg_attr(feature = "lints", allow(explicit_iter_loop))]
|
2016-10-23 23:32:52 +00:00
|
|
|
pub fn find_subcommand(&'b self, sc: &str) -> Option<&'b App<'a, 'b>> {
|
|
|
|
debugln!("fn=find_subcommand;");
|
|
|
|
debugln!("Looking for sc...{}", sc);
|
|
|
|
debugln!("Currently in Parser...{}", self.meta.bin_name.as_ref().unwrap());
|
|
|
|
for s in self.subcommands.iter() {
|
2016-11-01 20:10:40 +00:00
|
|
|
if s.p.meta.bin_name.as_ref().unwrap_or(&String::new()) == sc ||
|
|
|
|
(s.p.meta.aliases.is_some() &&
|
|
|
|
s.p
|
|
|
|
.meta
|
|
|
|
.aliases
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.any(|&(s, _)| s == sc.split(' ').rev().next().expect(INTERNAL_ERROR_MSG))) {
|
2016-10-23 23:32:52 +00:00
|
|
|
return Some(s);
|
|
|
|
}
|
|
|
|
if let Some(app) = s.p.find_subcommand(sc) {
|
|
|
|
return Some(app);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2016-01-11 08:59:56 +00:00
|
|
|
}
|
2016-03-15 02:09:17 +00:00
|
|
|
|
2016-05-06 21:35:53 +00:00
|
|
|
impl<'a, 'b> Clone for Parser<'a, 'b>
|
|
|
|
where 'a: 'b
|
|
|
|
{
|
2016-03-15 02:09:17 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Parser {
|
|
|
|
required: self.required.clone(),
|
|
|
|
short_list: self.short_list.clone(),
|
|
|
|
long_list: self.long_list.clone(),
|
|
|
|
blacklist: self.blacklist.clone(),
|
|
|
|
flags: self.flags.clone(),
|
|
|
|
opts: self.opts.clone(),
|
|
|
|
positionals: self.positionals.clone(),
|
|
|
|
subcommands: self.subcommands.clone(),
|
|
|
|
groups: self.groups.clone(),
|
|
|
|
global_args: self.global_args.clone(),
|
|
|
|
overrides: self.overrides.clone(),
|
2016-03-23 01:15:11 +00:00
|
|
|
help_short: self.help_short,
|
|
|
|
version_short: self.version_short,
|
2016-03-15 02:09:17 +00:00
|
|
|
settings: self.settings.clone(),
|
2016-06-04 02:44:58 +00:00
|
|
|
g_settings: self.g_settings.clone(),
|
2016-03-15 02:09:17 +00:00
|
|
|
meta: self.meta.clone(),
|
2016-06-05 05:09:07 +00:00
|
|
|
trailing_vals: self.trailing_vals,
|
2016-03-15 02:09:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-01 20:10:40 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn is_new_arg(arg_os: &OsStr) -> bool {
|
|
|
|
// Is this a new argument, or values from a previous option?
|
|
|
|
debug!("Starts new arg...");
|
|
|
|
if arg_os.starts_with(b"-") {
|
|
|
|
sdebugln!("Maybe");
|
|
|
|
// a singe '-' by itself is a value and typically means "stdin" on unix systems
|
|
|
|
!(arg_os.len_() == 1)
|
|
|
|
} else {
|
|
|
|
sdebugln!("No");
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|