Rustのclapで必須なサブコマンドのあるコマンドを作るときはarg_required_else_help(false)を書かないと引数なしで実行したときにエラーじゃなくてヘルプが表示されるの想定外
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(version)]
struct Opt {
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
/// Add file.
Add(Add),
/// Remove file.
Remove(Remove),
}
#[derive(Args, Debug)]
pub struct Add {
/// File to add.
file: Option<PathBuf>,
}
#[derive(Args, Debug)]
pub struct Remove {
/// File to remove.
file: Option<PathBuf>,
}
fn main() {
let opt = Opt::parse();
println!("{opt:?}");
}@@ -3,7 +3,7 @@
use clap::{Args, Parser, Subcommand};
#[derive(Debug, Parser)]
-#[command(version)]
+#[command(version, arg_required_else_help(false))]
struct Opt {
#[command(subcommand)]
command: Command,