この本を使用して勉強しています。
本日はコマンドラインから引数を取得する処理。
Clapを使用する方法が紹介されていたが、サンプルが古いのでそのまま使えず、結局公式のサンプルコードを見るハメに。
https://docs.rs/clap/latest/clap/
[dependencies]
clap = { version = "3.1.6", features = ["derive"] }
use clap::Parser;
#[derive(Parser, Debug)]
#[clap(
name = "My RPM program",
version = "1.0.0",
author = "Your name",
about = "Super awesome sample RPM calculator"
)]
struct Opts {
#[clap(short, long)]
verbose: bool,
#[clap(name = "FILE")]
formula_file: Option<String>,
}
fn main() {
let opts = Opts::parse();
match opts.formula_file {
Some(file) => println!("File specified: {}", file),
None => println!("No file specified."),
}
println!("Is verbosity specified?: {}", opts.verbose);
}
Clapを適用するには、Cargo.tomlの[dependencies]に一文を追加するだけでOK。
また、テキストではclap::Clapと書かれていたが、最新版ではclap::Parserが正解らしい。
置き換えたのはそれくらいか?
~/rust/samplecli$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/samplecli`
No file specified.
Is verbosity specified?: false
~/rust/samplecli$ cargo run -- input.txt
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/samplecli input.txt`
File specified: input.txt
Is verbosity specified?: false
~/rust/samplecli$ cargo run -- -v input.txt
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/samplecli -v input.txt`
File specified: input.txt
Is verbosity specified?: true
~/rust/samplecli$ cargo run -- -h
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/samplecli -h`
My RPM program 1.0.0
Your name
Super awesome sample RPM calculator
USAGE:
samplecli [OPTIONS] [FILE]
ARGS:
<FILE>
OPTIONS:
-h, --help Print help information
-v, --verbose
-V, --version Print version information
~/rust/samplecli$ cargo run -- -d
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/samplecli -d`
error: Found argument '-d' which wasn't expected, or isn't valid in this context
If you tried to supply `-d` as a value rather than a flag, use `-- -d`
USAGE:
samplecli [OPTIONS] [FILE]
For more information try --help
~/rust/samplecli$ cargo run -- input.txt input2.txt
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/samplecli input.txt input2.txt`
error: Found argument 'input2.txt' which wasn't expected, or isn't valid in this context
USAGE:
samplecli [OPTIONS] [FILE]
For more information try --help
「Rust勉強中、3/27の積み上げ」への1件のフィードバック