[英]clap default value - how to use it
Playing with Rusts clap crate for the first time.第一次玩 Rusts 拍板箱。 And I wonder how to use the default value of a command line option, when the option was not specified at the command line.而且我想知道如何在命令行未指定选项时使用命令行选项的默认值。
Given I specified a default_value
in the yaml file (see snippet below), I expected the matches.value_of("VERBOSE")
to return the default value if there is no other value given in the command line.鉴于我在 yaml 文件中指定了一个default_value
(参见下面的代码段),如果命令行中没有给出其他值,我希望matches.value_of("VERBOSE")
返回默认值。
Instead, I get:相反,我得到:
thread 'main' panicked at 'called
Option::unwrap()
on aNone
value', src/main.rs:18:6线程“主”在“在None
值上调用Option::unwrap()
”时惊慌失措,src/main.rs:18:6
I was googling for a while but it seems, no one really gives a canonical example of how it is supposed to work.我在谷歌上搜索了一段时间,但似乎没有人真正给出一个规范的例子来说明它应该如何工作。
...
args:
- config:
short: c
long: config
value_name: CONFIG
help: Specifies the config file to use.
takes_value: true
default_value: ""
- verbose:
short: v
long: verbose
value_name: VERBOSE
help: Sets verbosity. 0 = silent, > 0 = verbose.
takes_value: true
default_value: "1"
Here, my feeble attempt... compiling but not working (panics if empty command line is used).在这里,我的微弱尝试......编译但无法正常工作(如果使用空命令行,则会出现恐慌)。
// ...
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml).get_matches();
let verbosity =
matches.value_of("VERBOSE")
.and_then(|s| s.parse::<u8>().ok())
.unwrap();
As this obviously does not use the default values, my simple question is: How do I do it right?由于这显然不使用默认值,所以我的简单问题是:我该怎么做?
The problem is you either have a small misconception about what value_name
(which is used only for the CLI help option) is, or you missed, that the argument names are case sensitive.问题是您要么对value_name
(仅用于 CLI 帮助选项)有一个小小的误解,要么您错过了参数名称区分大小写的想法。
You have two options to make this work as expected:您有两种选择可以使这项工作按预期进行:
YAML: YAML:
...
args:
- config:
short: c
long: config
value_name: CONFIG
help: Specifies the config file to use.
takes_value: true
default_value: "1"
- verbose:
short: v
long: verbose
value_name: VERBOSE
help: Sets verbosity. 0 = silent, > 0 = verbose.
takes_value: true
default_value: "1"
Rust: Rust:
use clap::{load_yaml, App};
fn main() {
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml).get_matches();
let verbosity =
matches.value_of("verbose")
.and_then(|s| s.parse::<u8>().ok())
.unwrap();
println!("{}", verbosity);
}
...
args:
- config:
short: c
long: config
value_name: CONFIG
help: Specifies the config file to use.
takes_value: true
default_value: "1"
- VERBOSE:
short: v
long: verbose
value_name: VERBOSE
help: Sets verbosity. 0 = silent, > 0 = verbose.
takes_value: true
default_value: "1"
Rust Rust
use clap::{load_yaml, App};
fn main() {
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml).get_matches();
let verbosity =
matches.value_of("VERBOSE")
.and_then(|s| s.parse::<u8>().ok())
.unwrap();
println!("{}", verbosity);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.