pub trait FromArgs: Sized {
// Provided method
fn from_args<S: AsRef<OsStr>>(
command_name: &[&str],
args: &[S],
) -> Result<Self, EarlyExit> { ... }
}Expand description
Types which can be constructed from a set of command-line arguments.
Provided Methods§
Sourcefn from_args<S: AsRef<OsStr>>(
command_name: &[&str],
args: &[S],
) -> Result<Self, EarlyExit>
fn from_args<S: AsRef<OsStr>>( command_name: &[&str], args: &[S], ) -> Result<Self, EarlyExit>
Construct the type from an input set of arguments.
The first argument command_name is the identifier for the current
command. In most cases, users should only pass in a single item for the
command name, which typically comes from the first item from
std::env::args_os(). Implementations however should append the
subcommand name in when recursively calling FromArgs::from_args for
subcommands. This allows argp to generate correct subcommand help
strings.
The second argument args is the rest of the command line arguments.
§Examples
/// Command to manage a classroom.
#[derive(Debug, PartialEq, FromArgs)]
struct ClassroomCmd {
#[argp(subcommand)]
subcommands: Subcommands,
}
#[derive(Debug, PartialEq, FromArgs)]
#[argp(subcommand)]
enum Subcommands {
List(ListCmd),
Add(AddCmd),
}
/// List all the classes.
#[derive(Debug, PartialEq, FromArgs)]
#[argp(subcommand, name = "list")]
struct ListCmd {
/// List classes for only this teacher.
#[argp(option, arg_name = "name")]
teacher_name: Option<String>,
}
/// Add students to a class.
#[derive(Debug, PartialEq, FromArgs)]
#[argp(subcommand, name = "add")]
struct AddCmd {
/// The name of the class's teacher.
#[argp(option)]
teacher_name: String,
/// The name of the class.
#[argp(positional)]
class_name: String,
}
let args = ClassroomCmd::from_args(
&["classroom"],
&["list", "--teacher-name", "Smith"],
).unwrap();
assert_eq!(
args,
ClassroomCmd {
subcommands: Subcommands::List(ListCmd {
teacher_name: Some("Smith".to_string()),
})
},
);
// Help returns an error with `EarlyExit::Help`.
let early_exit = ClassroomCmd::from_args(
&["classroom"],
&["help"],
).unwrap_err();
match early_exit {
EarlyExit::Help(help) => assert_eq!(
help.generate_default(),
r#"Usage: classroom <command> [<args>]
Command to manage a classroom.
Options:
-h, --help Show this help message and exit.
Commands:
list List all the classes.
add Add students to a class.
"#.to_owned(),
),
_ => panic!("expected help"),
};
// Help works with subcommands.
let early_exit = ClassroomCmd::from_args(
&["classroom"],
&["list", "help"],
).unwrap_err();
match early_exit {
EarlyExit::Help(help) => assert_eq!(
help.generate_default(),
r#"Usage: classroom list [--teacher-name <name>]
List all the classes.
Options:
--teacher-name <name> List classes for only this teacher.
-h, --help Show this help message and exit.
"#.to_owned(),
),
_ => panic!("expected help"),
};
// Incorrect arguments will error out.
let err = ClassroomCmd::from_args(
&["classroom"],
&["lisp"],
).unwrap_err();
assert_eq!(
err,
argp::EarlyExit::Err(argp::Error::UnknownArgument("lisp".into())),
);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.