Skip to content

Commit

Permalink
command: add Argument.name()
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed May 24, 2024
1 parent f130f9d commit f4bcae6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
**Improvements**

* Add `Command.pos_args()` and `key_args()` for filtering argument types.
* Add `Argument.name()` to identify arguments.

# 0.2.0 (2024-05-05)

Expand Down
15 changes: 15 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub struct Argument {
}

impl Argument {
/// Returns a name for the argument -- either the key, if given, or value.
pub fn name(&self) -> &str {
match self.key.as_deref() {
Some(key) => key,
None => &self.value,
}
}

/// Parses the argument value as a T using core::str::parse(). Convenience
/// method that returns an improved error message as a boxed error to ease
/// error handling in a [`Runner`](crate::Runner).
Expand Down Expand Up @@ -115,6 +123,13 @@ mod tests {
assert_eq!(cmd.key_args(), vec![&cmd.args[0], &cmd.args[1], &cmd.args[2]]);
}

/// Tests Argument.name().
#[test]
fn argument_name() {
assert_eq!(arg!("value").name(), "value");
assert_eq!(arg!("key" => "value").name(), "key");
}

/// Basic tests of Argument.parse(). Not comprehensive, since it dispatches
/// to core::str::parse().
#[test]
Expand Down

0 comments on commit f4bcae6

Please sign in to comment.