Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't specify multiple vector elements using env vars #4999

Open
2 tasks done
johananl opened this issue Jul 7, 2023 · 2 comments
Open
2 tasks done

Can't specify multiple vector elements using env vars #4999

johananl opened this issue Jul 7, 2023 · 2 comments
Labels
C-bug Category: Updating dependencies

Comments

@johananl
Copy link

johananl commented Jul 7, 2023

Please complete the following tasks

Rust Version

rustc 1.70.0 (90c541806 2023-05-31)

Clap Version

4.3.11

Minimal reproducible code

use clap::Parser;

#[derive(Parser)]
struct Cli {
    #[arg(short, env)]
    foo: Vec<i32>,
}

fn main() {
    let cli = Cli::parse();
    println!("{:?}", cli.foo);
}

Steps to reproduce the bug with the above code

cargo run -- -f 1 -f 2

Output: [1, 2]

FOO=1 cargo run

Output: [1]

FOO="1 2" cargo run

Output: error: invalid value '1 2' for '-f <FOO>': invalid digit found in string

FOO="1,2" cargo run

Output: error: invalid value '1,2' for '-f <FOO>': invalid digit found in string

FOO="1, 2" cargo run

Output: error: invalid value '1, 2' for '-f <FOO>': invalid digit found in string

FOO="[1, 2]" cargo run

Output: error: invalid value '[1, 2]' for '-f <FOO>': invalid digit found in string

Actual Behaviour

When using env on a vector field, multiple values can be passed by specifying the argument multiple times on the CLI, however the same can't be done when using env vars (because the OS doesn't support specifying the same env var twice).

Expected Behaviour

I think it makes sense to have consistency between CLI args and env vars when using the env option even when the field is a vector. If that's not possible, the user should probably know about the expected behavior or limitation.

Potential solutions I can think of:

  1. Parse env var values when using env on vector fields and split the string into vector elements. Something like FOO="[1, 2]" can likely be used.
  2. Document the fact that when using env on vector fields, only one value can be specified.
  3. Disallow env on vector fields.

Additional Context

No response

Debug Output

No response

@johananl johananl added the C-bug Category: Updating dependencies label Jul 7, 2023
@johananl johananl changed the title Can't specify multiple vector element using env vars Can't specify multiple vector elements using env vars Jul 7, 2023
@epage
Copy link
Member

epage commented Jul 7, 2023

You can use value_delimiter to split the env variable though that also means that CLI values will also be split

@johananl
Copy link
Author

Thanks @epage. I wonder if there is a way to reconcile the behavior of CLI args and env vars for vector values, as well as whether it would make sense to do so. The problem of course isn't in clap itself, it's the fact that at the OS level it's meaningless to specify the same env var twice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Updating dependencies
Projects
None yet
Development

No branches or pull requests

2 participants