https://rust-cli.github.io/book/tutorial/cli-args.html#parsing-cli-arguments-with-structopt ## [[clap]]を使う オススメ。 #2021/02/28 現在beta版のv3でないとstructには対応していない。 ```rust use clap::Clap; #[derive(Clap, Debug)] #[clap(version = "0.1", author = "tadashi-aikawa")] struct Opts { /// The path of the config file to load #[clap(short, long, default_value = ".tatsuwo.yaml")] config: String, #[clap(short, long, parse(from_occurrences))] verbose: i32, #[clap(subcommand)] subcmd: SubCommand, } #[derive(Clap, Debug)] enum SubCommand { /// タツヲが吠える Uho(UhoOpts), } #[derive(Clap, Debug)] struct UhoOpts {} fn main() { let opts: Opts = Opts::parse(); eprintln!("opts = {:?}", opts); match opts.subcmd { SubCommand::Uho(_) => { for _ in 0..opts.verbose { println!("ウホホ💓") } } } } ``` ## structoptを使う [[clap]]のbetaが嫌でstructを使いたい場合は[[structopt]]を使う。 ```rust use structopt::StructOpt; #[derive(StructOpt, Debug)] struct Cli { pattern: String, #[structopt(parse(from_os_str))] path: std::path::PathBuf, } fn main() { let args = Cli::from_args(); eprintln!("args = {:?}", args); } ```