English 中文(简体)
如何将指挥线的争辩与非股权混为一谈?
原标题:How to parse command line argument to non-unit enum with clap?

我有这个想法:

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
pub enum TheAge {
    Adult,
    Age(u8)
}

并且,

#[derive(Parser)]
#[command(author, version, about, long_about)]
pub struct Cli {
    #[arg(short, long, value_enum)]
    pub age: TheAge
}

这有误:

error: `#[derive(ValueEnum)]` only supports unit variants. Non-unit variants must be skipped

当我从表格中删除<代码>Age(u8)时,本汇编。

任何关于如何使用并非单位变量的 en?

问题回答

#[derive(ValueEnum)]不支持非unit变量,因此你可以得出。

如果你看一下所需物品,那就说明为什么:

impl ValueEnum for TheAge {
    fn value_variants() -> & a [Self] { todo!() }
    fn to_possible_value(&self) -> Option<PossibleValue> { todo!() }
}

<代码> 价值_variants

一切可能的论点价值都显示出来。

如果“所有可能的数值”包括每一条<代码>u8 (即使只有257个数值,它仍是一个幻觉。) 不可能笼统地产生所有类型的价值,因此,你可以打上“条码”(ValueEnum)]# [derive(ValueEnum)],在某些情况下,通过手法加以实施(例如,值为enum,只有几种变量)。

相反,你可以实施<条码>From<&str>,用于这一构件。

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum TheAge {
    Adult,
    Age(u8)
}

const LEGAL_ADULT_AGE: u8 = 18;
impl From<&str> for TheAge {
    fn from(v: &str) -> TheAge {
        v.parse::<u8>().map_or(TheAge::Adult, |a| {
            if a >= LEGAL_ADULT_AGE {
                TheAge::Adult
            } else {
                TheAge::Age(a)
            }
        })
    }
}

fn main() {
    dbg!(Cli::parse_from(["", "--age", "18"]).age); // and above -> Adult
    dbg!(Cli::parse_from(["", "--age", "17"]).age); // and below -> Age(17)
    dbg!(Cli::parse_from(["", "--age", "anything_else"]).age); // -> Adult
}




相关问题
Finding the Highest Value in an Enumeration

I m writing a method which determines the highest value in a .NET enumeration so I can create a BitArray with one bit for each enum value: pressedKeys = new BitArray(highestValueInEnum<Keys>());...

Conversion of Enum to Enumerable

To convert Enum to Enumerable ,I use public enum Flags { Trivial=1, Minor, Major, Critical } IEnumerable<int> n = Enumerable.Range((int)Flags.Trivial, (...

Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

Enum scoping issues

I try to keep things as local as possible, so I put enums at class scope, even if they are shared between two classes (I put it in the class that "goes better" with it.) This has worked out great, but ...

How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

C++ enum value initialization

I have an enum declared in my code as: enum REMOTE_CONN { REMOTE_CONN_DEFAULT = 0, REMOTE_CONN_EX_MAN = 10000, REMOTE_CONN_SD_ANNOUNCE, REMOTE_CONN_SD_IO, REMOTE_CONN_AL, ...

WCF: Enforce equal DataContracts on both sides

I m wondering if it is possible to have WCF make sure that the DataContracts on both sides of a connection are exactly the same (and throw an exception when trying to connect if they are not). For ...

热门标签