【问题标题】:Parse Structopt flag from anywhere in the input从输入中的任何位置解析 Structopt 标志
【发布时间】:2021-07-04 21:46:04
【问题描述】:

我希望能够在我的命令行工具中使用“通用”标志,该工具使用StructOpt。也就是说,如果我有一个标志(例如--debug),无论该标志在输入中的哪个位置,我都希望它的行为相同:

$ mycli --debug alpha
$ mycli alpha --debug

alpha--debug 在此示例中只是有用的占位符;实际的子命令和标志会有所不同。)

实际行为:

$ cargo run -- --debug alpha                                                                                               nford 14:36:32
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/scratch-rust --debug alpha`
Debug mode enabled!
Alpha subcommand selected.
$ cargo run -- alpha --debug                                                                                               nford 14:36:27
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/scratch-rust alpha --debug`
error: Found argument '--debug' which wasn't expected, or isn't valid in this context

USAGE:
    scratch-rust alpha

预期行为:

$ cargo run -- --debug alpha                                                                                               nford 14:36:32
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/scratch-rust --debug alpha`
Debug mode enabled!
Alpha subcommand selected.
$ cargo run -- alpha --debug       
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/scratch-rust --debug alpha`
Debug mode enabled!
Alpha subcommand selected.

这是我的main.rs 代码:

use std::str::FromStr;
use structopt::StructOpt;
use std::io::{Error, ErrorKind};

#[derive(Debug, StructOpt)]
/// Specific task being executed; one task might imply others, depending on flags.
pub enum Subcommand {
    Alpha,
    Omega,
}

impl FromStr for Subcommand {
    type Err = Error;
    fn from_str(subcommand_input: &str) -> Result<Self, Self::Err> {
        match subcommand_input {
            "alpha" => Ok(Subcommand::Alpha),
            "omega" => Ok(Subcommand::Omega),
            _ => Err(Error::new(ErrorKind::Other, "Unrecognized subcommand.")),
        }
    }
}

#[derive(Debug, StructOpt)]
#[structopt(
    name = "Minimal Example CLI",
    about = "A minimal example of multi-location command line inputs.",
)]
pub struct CLIOpts {
    /// Set logging to verbose mode.
    // short and long flags (-d, --debug) will be deduced from the field's name
    #[structopt(short, long)]
    pub debug: bool,

    #[structopt(subcommand)]
    pub cmd: Subcommand,
}


fn main() {
    let args = CLIOpts::from_args();
    if args.debug {
        println!("Debug mode enabled!");
    }
    match args.cmd {
        Subcommand::Alpha => println!("Alpha subcommand selected."),
        Subcommand::Omega => println!("Omega subcommand selected."),
    }
}

这是我的Cargo.toml 文件(上面的示例在没有structopt 依赖项的情况下不起作用):

[package]
name = "scratch-rust"
version = "0.1.0"
edition = "2018"

[dependencies]
structopt = "*"

有没有办法做到这一点,而无需在每个子命令级别复制“通用”标志?

【问题讨论】:

  • 我不知道 structopt,但它是基于 clap 的,并且 clap 有一个 global 标志来使参数按照你想要的方式运行。我假设 structopt 有一个反映这个标志的属性。

标签: rust structopt


【解决方案1】:

Clap 有一个 global() 方法来处理你想要的参数。您可以使用 raw attributes 访问 Structopt 的此方法:

pub struct CLIOpts {
    /// Set logging to verbose mode.
    // short and long flags (-d, --debug) will be deduced from the field's name
    #[structopt(short, long, global = true)]
    pub debug: bool,

    #[structopt(subcommand)]
    pub cmd: Subcommand,
}

(注意为debug 添加的global 属性)

应用程序现在可以按照您希望的方式运行:

$ cargo run -- --debug alpha
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/scratch-rust --debug alpha`
Debug mode enabled!
Alpha subcommand selected.
$ cargo run -- alpha --debug
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/scratch-rust alpha --debug`
Debug mode enabled!
Alpha subcommand selected.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多