Trait clap::CommandFactory
source · [−]pub trait CommandFactory: Sized {
fn into_app<'help>() -> Command<'help>;
fn into_app_for_update<'help>() -> Command<'help>;
fn command<'help>() -> Command<'help> { ... }
fn command_for_update<'help>() -> Command<'help> { ... }
}
Expand description
Required Methods
fn into_app_for_update<'help>() -> Command<'help>
fn into_app_for_update<'help>() -> Command<'help>
Deprecated, replaced with CommandFactory::command_for_update
Provided Methods
Build a Command
that can instantiate Self
.
See FromArgMatches::from_arg_matches_mut
for instantiating Self
.
Examples found in repository?
examples/tutorial_derive/04_04_custom.rs (line 45)
≺ ≻
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
fn main() {
let cli = Cli::parse();
// Let's assume the old version 1.2.3
let mut major = 1;
let mut minor = 2;
let mut patch = 3;
// See if --set-ver was used to set the version manually
let version = if let Some(ver) = cli.set_ver.as_deref() {
if cli.major || cli.minor || cli.patch {
let mut cmd = Cli::command();
cmd.error(
ErrorKind::ArgumentConflict,
"Can't do relative and absolute version change",
)
.exit();
}
ver.to_string()
} else {
// Increment the one requested (in a real program, we'd reset the lower numbers)
let (maj, min, pat) = (cli.major, cli.minor, cli.patch);
match (maj, min, pat) {
(true, false, false) => major += 1,
(false, true, false) => minor += 1,
(false, false, true) => patch += 1,
_ => {
let mut cmd = Cli::command();
cmd.error(
ErrorKind::ArgumentConflict,
"Can only modify one version field",
)
.exit();
}
};
format!("{}.{}.{}", major, minor, patch)
};
println!("Version: {}", version);
// Check for usage of -c
if let Some(config) = cli.config.as_deref() {
// todo: remove `#[allow(clippy::or_fun_call)]` lint when MSRV is bumped.
#[allow(clippy::or_fun_call)]
let input = cli
.input_file
.as_deref()
// 'or' is preferred to 'or_else' here since `Option::as_deref` is 'const'
.or(cli.spec_in.as_deref())
.unwrap_or_else(|| {
let mut cmd = Cli::command();
cmd.error(
ErrorKind::MissingRequiredArgument,
"INPUT_FILE or --spec-in is required when using --config",
)
.exit()
});
println!("Doing work using input {} and config {}", input, config);
}
}
fn command_for_update<'help>() -> Command<'help>
fn command_for_update<'help>() -> Command<'help>
Build a Command
that can update self
.
See FromArgMatches::update_from_arg_matches_mut
for updating self
.