【Rust】コマンド入力とargs

fn main() {

    let command_name = std::env::args().nth(0).unwrap_or("CLI".to_string());
    let name = std::env::args().nth(1).unwrap_or("WORLD".to_string());
    println!("Hello {} via {}!", name, command_name);
}

Running `target/debug/kakeibo arg1 arg2`
Hello arg1 via target/debug/kakeibo!

use clap::Parser;

#[derive(Parser)]
#[clap(version = "1.0")]
struct Args {
    arg1: String,
    arg2: String
}

fn main() {
    let _args = Args::parse();
}
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[clap(version = "1.0")]
struct App {
    #[clap(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// 新しい口座を作る
    New,
    /// 口座に入金する
    Deposit,
    /// 口座から出金する
    Withdraw,
    /// CSVからインポートする
    Import,
    /// レポートを出力する
    Report,
}

fn main() {
    let _args = App::parse();
}

$ ./target/debug/kakeibo -h
Usage: kakeibo

Commands:
new 新しい口座を作る
deposit 口座に入金する
withdraw 口座から出金する
import CSVからインポートする
report レポートを出力する
help Print this message or the help of the given subcommand(s)

Options:
-h, –help Print help
-V, –version Print version