Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions argh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,16 @@ pub trait FromArgs: Sized {
}
}

impl<T: FromArgs> FromArgs for Box<T> {
fn from_args(command_name: &[&str], args: &[&str]) -> Result<Self, EarlyExit> {
T::from_args(command_name, args).map(Box::new)
}

fn redact_arg_values(command_name: &[&str], args: &[&str]) -> Result<Vec<String>, EarlyExit> {
T::redact_arg_values(command_name, args)
}
}

/// A top-level `FromArgs` implementation that is not a subcommand.
pub trait TopLevelCommand: FromArgs {}

Expand All @@ -645,14 +655,18 @@ pub trait SubCommands: FromArgs {
}
}

impl<T: SubCommand> SubCommands for T {
const COMMANDS: &'static [&'static CommandInfo] = &[T::COMMAND];
}

/// A `FromArgs` implementation that represents a single subcommand.
pub trait SubCommand: FromArgs {
/// Information about the subcommand.
const COMMAND: &'static CommandInfo;
}

impl<T: SubCommand> SubCommands for T {
const COMMANDS: &'static [&'static CommandInfo] = &[T::COMMAND];
impl<T: SubCommand> SubCommand for Box<T> {
const COMMAND: &'static CommandInfo = T::COMMAND;
}

/// Trait implemented by values returned from a dynamic subcommand handler.
Expand Down
9 changes: 6 additions & 3 deletions argh/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn subcommand_example() {
#[argh(subcommand)]
enum MySubCommandEnum {
One(SubCommandOne),
Two(SubCommandTwo),
Two(Box<SubCommandTwo>),
}

#[derive(FromArgs, PartialEq, Debug)]
Expand All @@ -185,10 +185,13 @@ fn subcommand_example() {
}

let one = TopLevel::from_args(&["cmdname"], &["one", "--x", "2"]).expect("sc 1");
assert_eq!(one, TopLevel { nested: MySubCommandEnum::One(SubCommandOne { x: 2 }) },);
assert_eq!(one, TopLevel { nested: MySubCommandEnum::One(SubCommandOne { x: 2 }) });

let two = TopLevel::from_args(&["cmdname"], &["two", "--fooey"]).expect("sc 2");
assert_eq!(two, TopLevel { nested: MySubCommandEnum::Two(SubCommandTwo { fooey: true }) },);
assert_eq!(
two,
TopLevel { nested: MySubCommandEnum::Two(Box::new(SubCommandTwo { fooey: true })) }
);
}

#[test]
Expand Down