Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

clap_derive: support tuples as shortcut for number_of_values = N, different validators #141

Open
epage opened this issue Dec 6, 2021 · 1 comment

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by CreepySkeleton
Monday Mar 02, 2020 at 17:44 GMT
Originally opened as clap-rs/clap#1717


Transferred from: TeXitoi/structopt#349

Sometimes we want to to take a fixed number of values that should always be passed together or not at all. For instance, let's take RGBA: four numbers, first three must be 0 <= N <= 255, the last must be 0 <= N <= 1.0. This can be naturally expressed via tuples:

fn validate_alpha(a: String) -> Result<f32, String> {
    let a = a.parse()?;
    if a > 1 {
        Err(format!("alpha cannot be > 0.1"))
    } else {
        Ok(a)
    }
}

#[derive(Clap)]
struct Args {
    #[clap(
        long,
        // `parse` can be extended to take multiple validators
        // `_` means default `try_from_str = FromStr::from_str`
        parse(_, _, _, try_from_str = validate_alpha)
    )]
    color: (u8, u8, u8, f32), // `number_of_values = 4`, different parsers 
    
    pair: Option<(String, Path)> // they can also be optional 
}
@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by CreepySkeleton
Saturday Apr 18, 2020 at 13:29 GMT


Moving it to 3.0 milestone since this would be a breaking change theoretically.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant