Skip to content

Commit

Permalink
FIX: do not ignore groups so they can register themselves before done()
Browse files Browse the repository at this point in the history
  • Loading branch information
zmarcantel committed Sep 11, 2019
1 parent 5ac11c5 commit 571a98d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rags-rs"
version = "0.1.3"
version = "0.1.4"
authors = ["Zach Marcantel <[email protected]>"]
edition = "2018"
license = "GPL-3.0-or-later"
Expand Down
40 changes: 27 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ macro_rules! argparse {
}


enum ItemType {
Argument,
Subcommand,
Group,
}

/// Defines where the value (if any) associated with a given argument is located.
#[derive(Debug)]
enum ValueLocation {
Expand Down Expand Up @@ -493,15 +499,23 @@ impl Parser {
Ok(self)
}

fn should_ignore(&self, is_subcmd: bool) -> bool {
fn should_ignore(&self, item: ItemType) -> bool {
if self.parse_done {
return true;
}
if is_subcmd {
self.walk_depth != (self.commit_depth + 1)
} else {
self.walk_depth != self.max_depth
match item {
ItemType::Argument => {
self.walk_depth != self.max_depth
}
ItemType::Subcommand => {
self.walk_depth != (self.commit_depth + 1)
}
ItemType::Group => {
// never ignore a group as there is no side-effect, and needs to be registered for the ensuing done()
false
}
}

}

fn commit_next_level(&mut self) {
Expand Down Expand Up @@ -764,7 +778,7 @@ impl Parser {
where <T as FromStr>::Err: std::fmt::Display
{

if self.should_ignore(false) { return Ok(self); }
if self.should_ignore(ItemType::Argument) { return Ok(self); }

// only add help if it is wanted
if self.wants_help() {
Expand Down Expand Up @@ -837,7 +851,7 @@ impl Parser {
) -> Result<&'a mut Parser, Error>
{

if self.should_ignore(false) { return Ok(self); }
if self.should_ignore(ItemType::Argument) { return Ok(self); }

if self.wants_help() {
self.printer.add_arg(
Expand Down Expand Up @@ -909,7 +923,7 @@ impl Parser {
) -> Result<&'a mut Parser, Error>
{

if self.should_ignore(false) { return Ok(self); }
if self.should_ignore(ItemType::Argument) { return Ok(self); }

if self.wants_help() {
self.printer.add_arg(
Expand Down Expand Up @@ -986,7 +1000,7 @@ impl Parser {
where <T as FromStr>::Err: std::fmt::Display
{

if self.should_ignore(false) { return Ok(self); }
if self.should_ignore(ItemType::Argument) { return Ok(self); }

if self.wants_help() {
self.printer.add_arg(
Expand Down Expand Up @@ -1087,7 +1101,7 @@ impl Parser {
// must move into it unconditionally
self.walk_next_level();

if self.should_ignore(true) {
if self.should_ignore(ItemType::Subcommand) {
return Ok(self);
}

Expand Down Expand Up @@ -1136,7 +1150,7 @@ impl Parser {
return Err(Error::NestedGroup(orig, name));
}

if self.should_ignore(false) { return Ok(self); }
if self.should_ignore(ItemType::Group) { return Ok(self); }

self.curr_group = Some(name);
if self.wants_help() {
Expand Down Expand Up @@ -1170,7 +1184,7 @@ impl Parser {
) -> Result<&'a mut Parser, Error>
where <T as FromStr>::Err: std::fmt::Display
{
if self.should_ignore(false) { return Ok(self); }
if self.should_ignore(ItemType::Argument) { return Ok(self); }

if self.has_variadic {
return Err(Error::UnorderedPositionals(name));
Expand Down Expand Up @@ -1225,7 +1239,7 @@ impl Parser {
) -> Result<&'a mut Parser, Error>
where <T as FromStr>::Err: std::fmt::Display
{
if self.should_ignore(false) { return Ok(self); }
if self.should_ignore(ItemType::Argument) { return Ok(self); }

if self.has_variadic {
return Err(Error::MultipleVariadic(name));
Expand Down

0 comments on commit 571a98d

Please sign in to comment.