Expand description
A flag/switch/argument that has a long name
You can specify it multiple times, bpaf
would use items past the first of each short
and long
as
hidden aliases.
Combinatoric usage
#[derive(Debug, Clone)]
pub struct Options {
switch: bool,
arg: usize,
username: String,
}
pub fn options() -> OptionParser<Options> {
let switch = short('s') // first `short` creates a builder
.short('S') // second switch is a hidden alias
.long("switch") // visible long name
.long("also-switch") // hidden alias
.help("Switch with many names")
.switch(); // `switch` finalizes the builder
let arg = long("argument") // long is also a builder
.short('a')
.short('A')
.long("also-arg")
.help("Argument with names")
.argument::<usize>("ARG");
let username = long("user")
.short('u')
.env("USER1")
.help("Custom user name")
.argument::<String>("USER");
construct!(Options {
switch,
arg,
username
})
.to_options()
}
Derive usage
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub struct Options {
#[bpaf(short, long, short('S'), long("also-switch"))]
/// Switch with many names
switch: bool,
#[bpaf(short, long("argument"), short('A'), long("also-arg"))]
/// Argument with names
arg: usize,
#[bpaf(short, long("user"), env("USER1"), argument("USER"))]
/// Custom user name
username: String,
}
Examples
As usual switch is optional, arguments are required
% app -a 42 -u Bobert
Options { switch: false, arg: 42, username: "Bobert" }
Help displays only visible aliases (and a current value for env arguments)
% app --help
Usage: [-s] -a ARG -u USER
Available options:
-s, --switch Switch with many names
-a, --argument <ARG> Argument with names
-u, --user <USER> [env:USER1 = "pacak"]
Custom user name
-h, --help Prints help information
But you can still use hidden aliases, both short and long
% app --also-switch --also-arg 330 --user Bobert
Options { switch: true, arg: 330, username: "Bobert" }
And unless there’s many
or similar modifiers having multiple aliases doesn’t mean
you can specify them multiple times:
% app -A 42 -a 330 -u Bobert
-a is not expected in this context
Also hidden aliases are really hidden and only meant to do backward compatibility stuff, they won’t show up anywhere else in completions or error messages
% app -a 42 -A 330 -u Bobert
No such flag: `-A`, did you mean `-u`?
Examples found in repository?
More examples
examples/negative.rs (line 6)
5 6 7 8 9 10 11 12 13 14 15 16 17
fn main() {
let age = long("age").argument::<i64>("AGE");
let msg = "\
To pass a value that starts with a dash requres one one of two special syntaxes:
This will pass '-1' to '--age' handler and leave remaining arguments as is
--age=-1
This will transform everything after '--' into non flags, '--age' will handle '-1'
and positional handlers will be able to handle the rest.
--age -- -1";
let num = age.to_options().descr(msg).run();
println!("age: {}", num);
}
examples/derive_show_asm.rs (line 52)
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 94 95 96 97 98 99 100 101 102 103 104
fn parse_manifest_path() -> impl Parser<PathBuf> {
long("manifest-path")
.help("Path to Cargo.toml")
.argument::<PathBuf>("PATH")
.parse(|p| {
if p.is_absolute() {
Ok(p)
} else {
std::env::current_dir()
.map(|d| d.join(p))
.and_then(|full_path| full_path.canonicalize())
}
})
.fallback_with(|| std::env::current_dir().map(|x| x.join("Cargo.toml")))
}
#[derive(Debug, Clone, Bpaf)]
pub struct Format {
/// Print interleaved Rust code
pub rust: bool,
#[bpaf(external(color_detection))]
pub color: bool,
/// include full demangled name instead of just prefix
pub full_name: bool,
}
#[derive(Debug, Clone, Bpaf)]
pub enum Syntax {
/// Generate assembly using Intel style
Intel,
/// Generate assembly using AT&T style
Att,
}
impl ToString for Syntax {
fn to_string(&self) -> String {
match self {
Syntax::Intel => String::from("llvm-args=-x86-asm-syntax=intel"),
Syntax::Att => String::from("llvm-args=-x86-asm-syntax=att"),
}
}
}
fn color_detection() -> impl Parser<bool> {
let yes = long("color")
.help("Enable color highlighting")
.req_flag(true);
let no = long("no-color")
.help("Disable color highlighting")
.req_flag(false);
construct!([yes, no]).fallback_with::<_, &str>(|| Ok(true))
}