This commit is contained in:
Alena Yuryeva 2018-07-26 20:15:47 +05:00 committed by Kevin K
parent 7e5a6935b9
commit f230cfedc3
No known key found for this signature in database
GPG key ID: 2E39D46AABC94DDD
8 changed files with 52 additions and 74 deletions

View file

@ -1,5 +1,6 @@
// Std // Std
use std::io::Write; use std::io::Write;
use std::ffi::OsStr;
// Internal // Internal
use build::{App, Arg}; use build::{App, Arg};
@ -131,7 +132,7 @@ complete -F _{name} -o bashdefault -o default {name}
p = &find_subcmd!(p, sc).unwrap(); p = &find_subcmd!(p, sc).unwrap();
} }
let mut opts = String::new(); let mut opts = String::new();
for o in opts!(p) { for (_, o) in opts!(p) {
if let Some(l) = o.long { if let Some(l) = o.long {
opts = format!( opts = format!(
"{} "{}
@ -180,7 +181,8 @@ complete -F _{name} -o bashdefault -o default {name}
"{shorts} {longs} {pos} {subcmds}", "{shorts} {longs} {pos} {subcmds}",
shorts = shorts!(p).fold(String::new(), |acc, s| format!("{} -{}", acc, s)), shorts = shorts!(p).fold(String::new(), |acc, s| format!("{} -{}", acc, s)),
// Handles aliases too // Handles aliases too
longs = longs!(p).fold(String::new(), |acc, l| format!("{} --{}", acc, l)), // error-handling?
longs = longs!(p).fold(String::new(), |acc, l| format!("{} --{}", acc, l.to_str().unwrap()),
pos = positionals!(p).fold(String::new(), |acc, p| format!("{} {}", acc, p)), pos = positionals!(p).fold(String::new(), |acc, p| format!("{} {}", acc, p)),
// Handles aliases too // Handles aliases too
subcmds = sc_names!(p).fold(String::new(), |acc, s| format!("{} {}", acc, s)) subcmds = sc_names!(p).fold(String::new(), |acc, s| format!("{} {}", acc, s))

View file

@ -80,7 +80,7 @@ where
let mut completions = String::new(); let mut completions = String::new();
let preamble = String::from("\n cand "); let preamble = String::from("\n cand ");
for option in opts!(p) { for (_, option) in opts!(p) {
if let Some(data) = option.short { if let Some(data) = option.short {
let tooltip = get_tooltip(option.help, data); let tooltip = get_tooltip(option.help, data);
completions.push_str(&preamble); completions.push_str(&preamble);
@ -93,7 +93,7 @@ where
} }
} }
for flag in flags!(p) { for (_, flag) in flags!(p) {
if let Some(data) = flag.short { if let Some(data) = flag.short {
let tooltip = get_tooltip(flag.help, data); let tooltip = get_tooltip(flag.help, data);
completions.push_str(&preamble); completions.push_str(&preamble);

View file

@ -44,7 +44,7 @@ fn gen_fish_inner(root_command: &str, comp_gen: &FishGen, subcommand: &str, buff
basic_template.push_str(format!("\"__fish_seen_subcommand_from {}\"", subcommand).as_str()); basic_template.push_str(format!("\"__fish_seen_subcommand_from {}\"", subcommand).as_str());
} }
for option in opts!(comp_gen.0) { for (_, option) in opts!(comp_gen.0) {
let mut template = basic_template.clone(); let mut template = basic_template.clone();
if let Some(data) = option.short { if let Some(data) = option.short {
template.push_str(format!(" -s {}", data).as_str()); template.push_str(format!(" -s {}", data).as_str());
@ -62,7 +62,7 @@ fn gen_fish_inner(root_command: &str, comp_gen: &FishGen, subcommand: &str, buff
buffer.push_str("\n"); buffer.push_str("\n");
} }
for flag in flags!(comp_gen.0) { for (_, flag) in flags!(comp_gen.0) {
let mut template = basic_template.clone(); let mut template = basic_template.clone();
if let Some(data) = flag.short { if let Some(data) = flag.short {
template.push_str(format!(" -s {}", data).as_str()); template.push_str(format!(" -s {}", data).as_str());

View file

@ -79,7 +79,7 @@ fn generate_inner<'a, 'b, 'p>(
let mut completions = String::new(); let mut completions = String::new();
let preamble = String::from("\n [CompletionResult]::new("); let preamble = String::from("\n [CompletionResult]::new(");
for option in opts!(p) { for (_, option) in opts!(p) {
if let Some(data) = option.short { if let Some(data) = option.short {
let tooltip = get_tooltip(option.help, data); let tooltip = get_tooltip(option.help, data);
completions.push_str(&preamble); completions.push_str(&preamble);
@ -102,7 +102,7 @@ fn generate_inner<'a, 'b, 'p>(
} }
} }
for flag in flags!(p) { for (_, flag) in flags!(p) {
if let Some(data) = flag.short { if let Some(data) = flag.short {
let tooltip = get_tooltip(flag.help, data); let tooltip = get_tooltip(flag.help, data);
completions.push_str(&preamble); completions.push_str(&preamble);

View file

@ -334,7 +334,7 @@ fn escape_value(string: &str) -> String {
fn write_opts_of(p: &App) -> String { fn write_opts_of(p: &App) -> String {
debugln!("write_opts_of;"); debugln!("write_opts_of;");
let mut ret = vec![]; let mut ret = vec![];
for o in opts!(p) { for (_, o) in opts!(p) {
debugln!("write_opts_of:iter: o={}", o.name); debugln!("write_opts_of:iter: o={}", o.name);
let help = o.help.map_or(String::new(), escape_help); let help = o.help.map_or(String::new(), escape_help);
let mut conflicts = get_zsh_arg_conflicts!(p, o, INTERNAL_ERROR_MSG); let mut conflicts = get_zsh_arg_conflicts!(p, o, INTERNAL_ERROR_MSG);
@ -399,7 +399,7 @@ fn write_opts_of(p: &App) -> String {
fn write_flags_of(p: &App) -> String { fn write_flags_of(p: &App) -> String {
debugln!("write_flags_of;"); debugln!("write_flags_of;");
let mut ret = vec![]; let mut ret = vec![];
for f in flags!(p) { for (_, f) in flags!(p) {
debugln!("write_flags_of:iter: f={}", f.name); debugln!("write_flags_of:iter: f={}", f.name);
let help = f.help.map_or(String::new(), escape_help); let help = f.help.map_or(String::new(), escape_help);
let mut conflicts = get_zsh_arg_conflicts!(p, f, INTERNAL_ERROR_MSG); let mut conflicts = get_zsh_arg_conflicts!(p, f, INTERNAL_ERROR_MSG);

View file

@ -517,11 +517,7 @@
//! [license]: https://raw.githubusercontent.com/kbknapp/clap-rs/master/LICENSE-MIT //! [license]: https://raw.githubusercontent.com/kbknapp/clap-rs/master/LICENSE-MIT
#![crate_type = "lib"] #![crate_type = "lib"]
<<<<<<< HEAD
#![doc(html_root_url = "https://docs.rs/clap/3.0.0-alpha.1")] #![doc(html_root_url = "https://docs.rs/clap/3.0.0-alpha.1")]
=======
#![doc(html_root_url = "https://docs.rs/clap/3.0.0-alpha1")]
>>>>>>> WIP. Big reformat
#![deny( #![deny(
missing_docs, missing_docs,
missing_debug_implementations, missing_debug_implementations,
@ -585,18 +581,14 @@ use std::result::Result as StdResult;
#[macro_use] #[macro_use]
mod macros; mod macros;
<<<<<<< HEAD
<<<<<<< HEAD
mod build; mod build;
=======
mod mkeymap; mod mkeymap;
>>>>>>> WIP changing macros into MKeyMap calls
mod completions; mod completions;
=======
mod build; mod build;
mod completions; mod completions;
mod mkeymap;
>>>>>>> WIP. Big reformat
mod output; mod output;
mod parse; mod parse;
mod util; mod util;

View file

@ -9,7 +9,7 @@ use std::hash::{Hash, Hasher};
use std::slice; use std::slice;
// ! rustdoc // ! rustdoc
#[derive(Default, PartialEq, Debug)] #[derive(Default, PartialEq, Debug, Clone)]
pub struct MKeyMap<'a, 'b> pub struct MKeyMap<'a, 'b>
where where
'a: 'b, 'a: 'b,

View file

@ -246,11 +246,7 @@ where
let mut found = false; let mut found = false;
let mut foundx2 = false; let mut foundx2 = false;
for p in self for p in positionals!(self.app)
.positionals
.values()
.rev()
.map(|p_name| self.app.find(p_name).expect(INTERNAL_ERROR_MSG))
{ {
if foundx2 && !p.is_set(ArgSettings::Required) { if foundx2 && !p.is_set(ArgSettings::Required) {
assert!( assert!(
@ -282,11 +278,7 @@ where
// Check that if a required positional argument is found, all positions with a lower // Check that if a required positional argument is found, all positions with a lower
// index are also required // index are also required
let mut found = false; let mut found = false;
for p in self for p in positionals!(self.app)
.positionals
.values()
.rev()
.map(|p_name| self.app.find(p_name).expect(INTERNAL_ERROR_MSG))
{ {
if found { if found {
assert!( assert!(
@ -337,7 +329,7 @@ where
for (i, a) in self.app.args.values_mut().enumerate() { for (i, a) in self.app.args.values_mut().enumerate() {
if let Some(index) = a.index { if let Some(index) = a.index {
self.app.args.insert_key(KeyType::Position(index), i); self.app.args.insert_key(KeyType::Position(index as usize), i);
} else { } else {
if let Some(c) = a.short { if let Some(c) = a.short {
self.app.args.insert_key(KeyType::Short(c), i); self.app.args.insert_key(KeyType::Short(c), i);
@ -393,7 +385,7 @@ where
.app .app
.args .args
.keys() .keys()
.filter(|x| if let Position(_) = x { true } else { false }) .filter(|x| if let KeyType::Position(_) = x { true } else { false })
.count()) .count())
}) && self.positionals.values().last().map_or(false, |p_name| { }) && self.positionals.values().last().map_or(false, |p_name| {
!self !self
@ -494,7 +486,8 @@ where
} }
if starts_new_arg { if starts_new_arg {
self.seen.extend(self.cache); // ! add values to seen somewhere?
//self.seen.extend(self.cache);
if arg_os.starts_with(b"--") { if arg_os.starts_with(b"--") {
needs_val_of = self.parse_long_arg(matcher, &arg_os)?; needs_val_of = self.parse_long_arg(matcher, &arg_os)?;
debugln!( debugln!(
@ -566,9 +559,21 @@ where
} }
let low_index_mults = self.is_set(AS::LowIndexMultiplePositional) let low_index_mults = self.is_set(AS::LowIndexMultiplePositional)
&& pos_counter == (self.positionals.len() - 1); && pos_counter == (
//TODO make a macro for that
self
.app
.args
.keys()
.filter(|x| if let KeyType::Position(_) = x { true } else { false })
.count() - 1);
let missing_pos = self.is_set(AS::AllowMissingPositional) let missing_pos = self.is_set(AS::AllowMissingPositional)
&& (pos_counter == (self.positionals.len() - 1) && (pos_counter == (self
.app
.args
.keys()
.filter(|x| if let KeyType::Position(_) = x { true } else { false })
.count() - 1)
&& !self.is_set(AS::TrailingValues)); && !self.is_set(AS::TrailingValues));
debugln!( debugln!(
"Parser::get_matches_with: Positional counter...{}", "Parser::get_matches_with: Positional counter...{}",
@ -610,7 +615,12 @@ where
// Came to -- and one postional has .last(true) set, so we go immediately // Came to -- and one postional has .last(true) set, so we go immediately
// to the last (highest index) positional // to the last (highest index) positional
debugln!("Parser::get_matches_with: .last(true) and --, setting last pos"); debugln!("Parser::get_matches_with: .last(true) and --, setting last pos");
pos_counter = self.positionals.len(); pos_counter = self
.app
.args
.keys()
.filter(|x| if let KeyType::Position(_) = x { true } else { false })
.count();
} }
if let Some(p) = positionals!(self.app).find(|p| p.index == Some(pos_counter as u64)) { if let Some(p) = positionals!(self.app).find(|p| p.index == Some(pos_counter as u64)) {
if p.is_set(ArgSettings::Last) && !self.is_set(AS::TrailingValues) { if p.is_set(ArgSettings::Last) && !self.is_set(AS::TrailingValues) {
@ -622,13 +632,15 @@ where
)); ));
} }
if !self.is_set(AS::TrailingValues) if !self.is_set(AS::TrailingValues)
&& (self.is_set(AS::TrailingVarArg) && pos_counter == self.positionals.len()) && (self.is_set(AS::TrailingVarArg) && pos_counter == self
.app
.args
.keys()
.filter(|x| if let KeyType::Position(_) = x { true } else { false })
.count())
{ {
self.app.settings.set(AS::TrailingValues); self.app.settings.set(AS::TrailingValues);
} }
if self.cache.map_or(true, |name| name != p.name) {
self.cache = Some(p.name);
}
let _ = self.add_val_to_arg(p, &arg_os, matcher)?; let _ = self.add_val_to_arg(p, &arg_os, matcher)?;
matcher.inc_occurrence_of(p.name); matcher.inc_occurrence_of(p.name);
@ -714,7 +726,7 @@ where
} }
// Make sure we get the last one too // Make sure we get the last one too
self.seen.extend(self.cache); //self.seen.extend(self.cache);
if let Some(ref pos_sc_name) = subcmd_name { if let Some(ref pos_sc_name) = subcmd_name {
let sc_name = { let sc_name = {
@ -830,7 +842,7 @@ where
.setting(ArgSettings::MultipleValues) .setting(ArgSettings::MultipleValues)
.help("The subcommand whose help message to display"); .help("The subcommand whose help message to display");
pb._build(); pb._build();
parser.positionals.insert(1, pb.name); //parser.positionals.insert(1, pb.name);
parser.app.settings = parser.app.settings | self.app.g_settings; parser.app.settings = parser.app.settings | self.app.g_settings;
parser.app.g_settings = self.app.g_settings; parser.app.g_settings = self.app.g_settings;
} }
@ -1130,10 +1142,6 @@ where
// Default to "we're expecting a value later" // Default to "we're expecting a value later"
let ret = self.parse_opt(val, opt, false, matcher)?; let ret = self.parse_opt(val, opt, false, matcher)?;
if self.cache.map_or(true, |name| name != opt.name) {
self.cache = Some(opt.name);
}
return Ok(ret); return Ok(ret);
} else if let Some(flag) = self.app.args.get(KeyType::Short(c)) { } else if let Some(flag) = self.app.args.get(KeyType::Short(c)) {
debugln!("Parser::parse_short_arg:iter:{}: Found valid flag", c); debugln!("Parser::parse_short_arg:iter:{}: Found valid flag", c);
@ -1141,12 +1149,6 @@ where
// Only flags can be help or version // Only flags can be help or version
self.check_for_help_and_version_char(c)?; self.check_for_help_and_version_char(c)?;
ret = self.parse_flag(flag, matcher)?; ret = self.parse_flag(flag, matcher)?;
// Handle conflicts, requirements, overrides, etc.
// Must be called here due to mutablilty
if self.cache.map_or(true, |name| name != flag.name) {
self.cache = Some(flag.name);
}
} else { } else {
let arg = format!("-{}", c); let arg = format!("-{}", c);
return Err(ClapError::unknown_argument( return Err(ClapError::unknown_argument(
@ -1406,9 +1408,6 @@ where
); );
$_self.add_val_to_arg($a, OsStr::new(val), $m)?; $_self.add_val_to_arg($a, OsStr::new(val), $m)?;
if $_self.cache.map_or(true, |name| name != $a.name) {
$_self.cache = Some($a.name);
}
} else if $m.get($a.name).is_some() { } else if $m.get($a.name).is_some() {
debugln!( debugln!(
"Parser::add_defaults:iter:{}: has user defined vals", "Parser::add_defaults:iter:{}: has user defined vals",
@ -1418,10 +1417,6 @@ where
debugln!("Parser::add_defaults:iter:{}: wasn't used", $a.name); debugln!("Parser::add_defaults:iter:{}: wasn't used", $a.name);
$_self.add_val_to_arg($a, OsStr::new(val), $m)?; $_self.add_val_to_arg($a, OsStr::new(val), $m)?;
if $_self.cache.map_or(true, |name| name != $a.name) {
$_self.cache = Some($a.name);
}
} }
} else { } else {
debugln!( debugln!(
@ -1447,9 +1442,6 @@ where
}; };
if add { if add {
$_self.add_val_to_arg($a, OsStr::new(default), $m)?; $_self.add_val_to_arg($a, OsStr::new(default), $m)?;
if $_self.cache.map_or(true, |name| name != $a.name) {
$_self.cache = Some($a.name);
}
done = true; done = true;
break; break;
} }
@ -1466,7 +1458,7 @@ where
}; };
} }
for o in opts!(self.app) { for (k, o) in opts!(self.app) {
debug!("Parser::add_defaults:iter:{}:", o.name); debug!("Parser::add_defaults:iter:{}:", o.name);
add_val!(self, o, matcher); add_val!(self, o, matcher);
} }
@ -1488,18 +1480,10 @@ where
{ {
if let Some(ref val) = val.1 { if let Some(ref val) = val.1 {
self.add_val_to_arg(a, OsStr::new(val), matcher)?; self.add_val_to_arg(a, OsStr::new(val), matcher)?;
if self.cache.map_or(true, |name| name != a.name) {
self.cache = Some(a.name);
}
} }
} else { } else {
if let Some(ref val) = val.1 { if let Some(ref val) = val.1 {
self.add_val_to_arg(a, OsStr::new(val), matcher)?; self.add_val_to_arg(a, OsStr::new(val), matcher)?;
if self.cache.map_or(true, |name| name != a.name) {
self.cache = Some(a.name);
}
} }
} }
} }
@ -1526,7 +1510,7 @@ where
matcher.inc_occurrence_of(&*grp); matcher.inc_occurrence_of(&*grp);
} }
matcher.insert(&*opt.name); matcher.insert(&*opt.name);
} else if let Some(flg) = self.app.args.get(KeyType::Long(name)) { } else if let Some(flg) = self.app.args.get(KeyType::Long(&OsStr::new(name))) {
self.groups_for_arg(&*flg.name) self.groups_for_arg(&*flg.name)
.and_then(|grps| Some(matcher.inc_occurrences_of(&*grps))); .and_then(|grps| Some(matcher.inc_occurrences_of(&*grps)));
matcher.insert(&*flg.name); matcher.insert(&*flg.name);