use winnow_args::color::{Color, Paint, Palette, Theme};
use winnow_args::{Args, Subcommand, ValueEnum, report_with, words};
const THEME: Theme = Theme {
truecolor: Some(Palette {
header: Paint::fg(Color::Rgb(38, 166, 154)).bold(),
..Palette::DEFAULT_256
}),
..Theme::DEFAULT
};
#[derive(Args, Debug)]
#[arg(
name = "tool",
version = "0.1.0",
after_help = "Examples:\n $ tool use -g node@20\n $ tool ls --json"
)]
struct Cli {
#[arg(short, long, count, global)]
verbose: u8,
#[arg(short = 'C', long, value_name = "DIR", env = "TOOL_CD")]
cd: Option<String>,
#[arg(short, long, default = "4", help_heading = "Performance")]
jobs: Option<u32>,
#[arg(long, default = "auto")]
color: Option<When>,
#[arg(long, negate, default = "true")]
progress: bool,
#[arg(subcommand)]
command: Option<Command>,
}
#[derive(ValueEnum, Debug)]
enum When {
Auto,
Always,
Never,
}
#[derive(Subcommand, Debug)]
enum Command {
#[arg(alias = "u")]
Use(UseArgs),
#[arg(alias = "list")]
Ls(LsArgs),
}
#[derive(Args, Debug)]
#[arg(arg_required_else_help)]
struct UseArgs {
#[arg(short, long)]
global: bool,
#[arg(positional, value_name = "TOOL@VERSION")]
tools: Vec<String>,
}
#[derive(Args, Debug)]
struct LsArgs {
#[arg(long)]
json: bool,
}
fn main() {
let args: Vec<_> = std::env::args_os().skip(1).collect();
let cli = match Cli::parse_words(&words(&args)) {
Ok(cli) => cli,
Err(e) => std::process::exit(report_with(&e, "tool", &THEME)),
};
println!(
"verbose {}, cd {:?}, jobs {:?}, color {:?}, progress {}",
cli.verbose, cli.cd, cli.jobs, cli.color, cli.progress
);
match cli.command {
Some(Command::Use(u)) => println!("use: global {}, tools {:?}", u.global, u.tools),
Some(Command::Ls(l)) => println!("ls: json {}", l.json),
None => {}
}
}