pub struct Arg { /* private fields */ }std only.Expand description
A single argument a command accepts.
Build one with Arg::flag, Arg::option, or Arg::positional, then
attach it with Command::arg. The name is the key
used to read the parsed result back out of a Matches.
§Examples
use cli_forge::Arg;
let verbose = Arg::count("verbose").short('v').help("increase verbosity");
let define = Arg::option("define").short('D').multiple(true);
let files = Arg::positional("files").multiple(true).required(true);Implementations§
Source§impl Arg
impl Arg
Sourcepub fn count(name: impl Into<String>) -> Arg
pub fn count(name: impl Into<String>) -> Arg
Define a counting flag: a switch that may be repeated, whose occurrences
are tallied. -v, -vv, -vvv (or -v -v -v, or --verbose --verbose)
count 1, 2, 3. Read the count with
Matches::count.
§Examples
use cli_forge::{App, Arg, Command};
let mut app = App::new("demo");
app.register(Command::new("run").arg(Arg::count("verbose").short('v')));
let m = app.try_parse_from(["run", "-vvv"]).unwrap();
assert_eq!(m.subcommand().unwrap().1.count("verbose"), 3);Sourcepub fn positional(name: impl Into<String>) -> Arg
pub fn positional(name: impl Into<String>) -> Arg
Define a positional argument, filled by bare values in order.
§Examples
use cli_forge::Arg;
let path = Arg::positional("path").default(".");Sourcepub fn short(self, short: char) -> Arg
pub fn short(self, short: char) -> Arg
Set a one-letter short form (-x). Ignored for positionals.
Sourcepub fn long(self, long: impl Into<String>) -> Arg
pub fn long(self, long: impl Into<String>) -> Arg
Override the long form (--name). Defaults to the argument’s name.
Ignored for positionals.
Sourcepub fn required(self, required: bool) -> Arg
pub fn required(self, required: bool) -> Arg
Require the argument. Parsing fails with
ParseError::MissingRequired if it
is absent and has no default. Has no effect on flags or counts (they are
simply present or not).
Sourcepub fn multiple(self, multiple: bool) -> Arg
pub fn multiple(self, multiple: bool) -> Arg
Collect every occurrence into a list instead of keeping a single value.
For an option, each --name v appends a value:
-D A -D B yields ["A", "B"]. For a positional, it
becomes variadic and absorbs every remaining bare value: a b c yields
["a", "b", "c"] (put it last). Read the values with
Matches::values. Ignored for flags and counts.
§Examples
use cli_forge::{App, Arg, Command};
let mut app = App::new("cc");
app.register(
Command::new("build")
.arg(Arg::option("include").short('I').multiple(true))
.arg(Arg::positional("sources").multiple(true)),
);
let m = app.try_parse_from(["build", "-I", "a", "-I", "b", "x.c", "y.c"]).unwrap();
let (_, build) = m.subcommand().unwrap();
assert_eq!(build.values("include").collect::<Vec<_>>(), ["a", "b"]);
assert_eq!(build.values("sources").collect::<Vec<_>>(), ["x.c", "y.c"]);