use argh::{ArgsInfo, FromArgs};
use argh_complete::Generator;
#[derive(FromArgs, ArgsInfo)]
struct MyCmd {
#[argh(switch, short = 'v')]
verbose: bool,
#[argh(subcommand)]
cmd: Subcommands,
}
#[derive(FromArgs, ArgsInfo)]
#[argh(subcommand)]
enum Subcommands {
Completion(CompletionCmd),
DoThings(DoThingsCmd),
DoMoreThings(DoMoreThingsCmd),
}
#[derive(FromArgs, ArgsInfo)]
#[argh(subcommand, name = "completion")]
struct CompletionCmd {
#[argh(positional)]
shell: String,
}
#[derive(FromArgs, ArgsInfo)]
#[argh(subcommand, name = "do-things")]
struct DoThingsCmd {
#[argh(option, short = 'n', default = "5")]
count: usize,
#[argh(switch, short = 'q')]
quick: bool,
}
#[derive(FromArgs, ArgsInfo)]
#[argh(subcommand)]
enum MoreThingsSubcommands {
ThingOne(ThingOneCommand),
ThingTwo(ThingTwoCommand),
}
#[derive(FromArgs, ArgsInfo)]
#[argh(subcommand, name = "one")]
struct ThingOneCommand {
#[argh(switch, short = 's')]
slow: bool,
}
#[derive(FromArgs, ArgsInfo)]
#[argh(subcommand, name = "two")]
struct ThingTwoCommand {
#[argh(switch, short = 'q')]
quick: bool,
}
#[derive(FromArgs, ArgsInfo)]
#[argh(subcommand, name = "do-more-things")]
struct DoMoreThingsCmd {
#[argh(subcommand)]
cmd: MoreThingsSubcommands,
}
fn main() {
let args: MyCmd = argh::from_env();
if args.verbose && matches!(args.cmd, Subcommands::Completion(_)) {
println!("Doing things verbosely ")
}
match args.cmd {
Subcommands::Completion(cmd) => {
let cmd_info = MyCmd::get_args_info();
let mut command_name = String::new();
if let Some(arg0) = std::env::args().next() {
command_name = std::path::Path::new(&arg0)
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();
}
if command_name.is_empty() {
command_name = cmd_info.name.to_string();
}
match cmd.shell.as_str() {
"bash" => {
println!("{}", argh_complete::bash::Bash::generate(&command_name, &cmd_info))
}
"zsh" => {
println!("{}", argh_complete::zsh::Zsh::generate(&command_name, &cmd_info))
}
"fish" => {
println!("{}", argh_complete::fish::Fish::generate(&command_name, &cmd_info))
}
"nushell" => {
println!(
"{}",
argh_complete::nushell::Nushell::generate(&command_name, &cmd_info)
)
}
_ => eprintln!("Unsupported shell: {}", cmd.shell),
}
}
Subcommands::DoThings(cmd) => {
println!("Doing {} things (quick: {})", cmd.count, cmd.quick);
}
Subcommands::DoMoreThings(cmd) => match cmd.cmd {
MoreThingsSubcommands::ThingOne(cmd) => {
println!("Doing more thing one (slow: {})", cmd.slow);
}
MoreThingsSubcommands::ThingTwo(cmd) => {
println!("Doing more thing two (quick: {})", cmd.quick);
}
},
}
}