- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个命令行工具,它有两个可能的参数:
--version
(将打印出版本号并退出)--out
(这是一些输出文件的路径,魔法将被注入(inject)其中)。如果用户通过 --version
我不关心 --out
因为我打印版本并且我完成了但是如果他们没有通过 --version
我想要 --out
是必需的。
这就是我所拥有的,但我想知道是否有任何方法可以只使用 structopt 来做到这一点?
看来我最终可能需要将所有参数设为可选并自己进行所有验证...
#![feature(custom_attribute)]
#[macro_use]
extern crate structopt;
use structopt::StructOpt;
use std::path::PathBuf;
#[derive(Debug, StructOpt)]
#[structopt(name = "structopt_playing", about = "Just playing")]
struct Opt {
#[structopt(short = "v", long = "version")]
version: bool,
#[structopt(short = "o", long = "out", parse(from_os_str))]
output_file: Option<PathBuf>,
}
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
fn main() {
let opt = Opt::from_args();
if opt.version {
println!("Version: {}", VERSION);
return;
}
if !opt.output_file.is_some() {
println!("Oh, now I feel like I'm alone...you need to pass --out");
return;
}
println!("Now I'm going to need to do something with {:?}",
opt.output_file.unwrap());
}
最佳答案
使用required_unless
:
#[derive(Debug, StructOpt)]
#[structopt(name = "structopt_playing", about = "Just playing")]
struct Opt {
#[structopt(short = "v", long = "version")]
version: bool,
#[structopt(short = "o", long = "out", parse(from_os_str), required_unless = "version")]
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
output_file: Option<PathBuf>,
}
$ ./target/debug/stropt
error: The following required arguments were not provided:
--out <output_file>
USAGE:
stropt --out <output_file>
For more information try --help
$ ./target/debug/stropt --out hello
Now I'm going to need to do something with "hello"
$ ./target/debug/stropt --version
Version: 0.1.0
Clap提供了大量的相关配置选项:
required_unless
required_unless_all
required_unless_one
conflicts_with
conflicts_with_all
需要
requires_if
requires_ifs
required_if
required_ifs
requires_all
旁注:您根本不需要在此代码中使用 #![feature(custom_attribute)]
。
关于rust - 如何根据 structopt 中另一个参数的存在使参数可选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49877823/
我想要做的是将用户提供的命令行参数作为单个字符串访问。 首先,我这样做: let lst: Vec = std::env::args().collect(); let res: String = ls
我正在使用 structopt 来定义可以使用的参数 mfe -s opt1 -s opt2 -s opt2 this_is_an_argument 或 mfe -s opt1 opt2 opt3 t
我使用 structopt 来解析我的 Rust 应用程序的命令行参数。有问题的标志如下:query(位置)和 case_sensitive(可选)。 #[derive(StructOpt, Debu
我想制作 StructOpt使用枚举,这样每次用户通过 -d sunday它被解析为 Day::Sunday : #[macro_use] extern crate structopt; use st
我有一个命令行工具,它有两个可能的参数: --version(将打印出版本号并退出) --out(这是一些输出文件的路径,魔法将被注入(inject)其中)。 如果用户通过 --version 我不关
引用"Git" example of StructOpt ,我不明白我应该如何使用参数中的数据。 我对 Rust 还很陌生,所以我猜这很明显。不幸的是,我能找到的所有枚举示例都只在对象上执行 prin
我正在寻找一种方法来初始化 structopt Vec默认情况下具有多个项目的字段。我可以为单个项目做到这一点: use structopt::StructOpt; #[derive(Debug, S
我有一个 fn main,它通过 StructOpt .. Args::from_args 解析参数。 有没有办法创建这个 Args 对象而不实际启动可执行文件进行测试?我可以直接创建一个 Args
问题 我有一个采用不同选项的命令,这些选项的相对顺序对于命令的语义很重要。例如,在 command --config A --some-option --config-file B --random-
我有这个代码: #[derive(StructOpt)] pub struct Opt { /// Data stream to send to the device #[struct
例如,使用 运行我的应用程序 ./app --foo=bar get 效果很好,但是 ./app get --foo=bar 产生错误: error: Found argument '--foo' w
我想有两个相互冲突的选项,但其中一个必须是必需的: #[macro_use] extern crate structopt; use structopt::StructOpt; #[derive(St
我正在尝试使用 Rust 和出色的 structopt crate 编写一个小型 CLI 应用程序。 当我使用 Notepad++(编写代码)和 Conemu(运行 Cargo 命令)时,一切正常。
我是一名优秀的程序员,十分优秀!