clap/examples/multicall-busybox.rs

49 lines
1.6 KiB
Rust
Raw Normal View History

2022-05-23 15:50:11 +00:00
use std::path::PathBuf;
use std::process::exit;
2022-05-23 15:50:11 +00:00
use clap::{value_parser, Arg, Command};
2022-02-12 03:48:29 +00:00
fn applet_commands() -> [Command<'static>; 2] {
[
2022-02-12 03:48:29 +00:00
Command::new("true").about("does nothing successfully"),
Command::new("false").about("does nothing unsuccessfully"),
]
}
fn main() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new(env!("CARGO_CRATE_NAME"))
.multicall(true)
.subcommand(
2022-02-12 03:48:29 +00:00
Command::new("busybox")
.arg_required_else_help(true)
.subcommand_value_name("APPLET")
.subcommand_help_heading("APPLETS")
.arg(
Arg::new("install")
.long("install")
.help("Install hardlinks for all subcommands in path")
.exclusive(true)
.takes_value(true)
.default_missing_value("/usr/local/bin")
2022-05-23 15:50:11 +00:00
.value_parser(value_parser!(PathBuf))
.use_value_delimiter(false),
)
.subcommands(applet_commands()),
)
.subcommands(applet_commands());
2022-02-14 21:47:20 +00:00
let matches = cmd.get_matches();
let mut subcommand = matches.subcommand();
if let Some(("busybox", cmd)) = subcommand {
if cmd.is_present("install") {
unimplemented!("Make hardlinks to the executable here");
}
subcommand = cmd.subcommand();
}
match subcommand {
Some(("false", _)) => exit(1),
Some(("true", _)) => exit(0),
_ => unreachable!("parser should ensure only valid subcommand names are used"),
}
}