Skip to content

Improve support for options with multiple values #349

@zroug

Description

@zroug

It would be good if StructOpt would make it easier to have options that take multiple values, like for example --range min max. Currently this is achievable by using a Vec<_> with the number_of_values option:

#[derive(StructOpt)]
struct Args {
    #[structopt(long, number_of_values = 2)]
    range: Vec<u32>
}

This works:

program --range 1 10 
 => [1, 10]

But the default settings are not very good for this use case because this works too, which is unintended:

program --range 1 --range 10 
 => [1, 10]
program
 => []

The workaround is to also set required and multiple when number_of_values is used and it would be good if that would be the default:

#[derive(StructOpt)]
struct Args {
    #[structopt(long, number_of_values = 2, required = true, multiple = false)]
    range: Vec<u32>
}

Of course it would be even better if fixed sized arrays or tuples with different value types would be supported directly:

#[derive(StructOpt)]
struct Args {
    #[structopt(long)]
    range: [u32; 2]
}

#[derive(StructOpt)]
struct Args {
    #[structopt(long)]
    range: (u8, u64)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions