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

specifying default value makes field required #2043

Closed
jgarvin opened this issue Jul 31, 2020 · 10 comments
Closed

specifying default value makes field required #2043

jgarvin opened this issue Jul 31, 2020 · 10 comments
Assignees
Labels
A-derive Area: #[derive]` macro API C-bug Category: Updating dependencies
Milestone

Comments

@jgarvin
Copy link

jgarvin commented Jul 31, 2020

How to reproduce

With this declaration passing the argument is not required:

#[clap(short, long)]
cores: Vec<u32>,

with this declaration it is:

#[clap(short, long, default_value = "")]
cores: Vec<u32>,

I would expect specifying a default value to imply optional, so this doesn't seem like it can be intentional.

Version

  • Rust: rustc 1.46.0-nightly (16957bd4d 2020-06-30)
  • Clap: "3.0.0-beta.1"
@jgarvin jgarvin added the C-bug Category: Updating dependencies label Jul 31, 2020
@pksunkara
Copy link
Member

I think you are trying to assign your own semantics to clap here. Having a default value makes the arg optional but the value of the arg will not be undefined but will instead be the default value.

@CreepySkeleton
Copy link
Contributor

default_value doesn't work with Vec. I don't think it should because it implies "only one value" while Vec is all about multiple values. Ideally, this should be a compile time error.

But default_valueS must work as described - its presence should not affect requiredness. This is a bug in derive.

@CreepySkeleton CreepySkeleton self-assigned this Aug 3, 2020
@CreepySkeleton CreepySkeleton added the A-derive Area: #[derive]` macro API label Aug 3, 2020
@CreepySkeleton
Copy link
Contributor

@jgarvin The temporary workaround is #[clap(required = false)].

@pksunkara
Copy link
Member

@CreepySkeleton can you explain what exactly the problem is and what the solution should be. I am afraid that I don't understand.

@CreepySkeleton
Copy link
Contributor

CreepySkeleton commented Aug 12, 2020

@pksunkara

#[derive(Clap)]
struct Opts {
    // this option is required because it's not Option (please excuse the pun)
    a: u32,

    // this option is NOT required, despite the fact it's not Option
    // if absent, the default value will be used
    #[clap(default_value = "1")]
    b: u32,

    // this should be a compile time error because default_value (singular)
    // is kinda meaningless with `Vec`
    //
    // This case is what OP complained about: it turns out the field somehow becomes required (WTF???).
    #[clap(default_value = "1")]
    c: Vec<u32>,

    // this should be a legit - and preferable! - usage
    // the field is not required
    #[clap(default_values = &["1"])]
    d: Vec<u32>
}

@pksunkara pksunkara added this to the 3.0 milestone Aug 12, 2020
@ldm0 ldm0 assigned ldm0 and unassigned CreepySkeleton Mar 13, 2021
@pitschr

This comment has been minimized.

@abonander

This comment has been minimized.

@epage

This comment has been minimized.

@abonander

This comment has been minimized.

@epage
Copy link
Member

epage commented Oct 5, 2021

With master, I can't reproduce this issue

use clap::Clap;

#[derive(Clap, Debug)]
struct Opts {
    // this option is required because it's not Option (please excuse the pun)
    //a: u32,

    // this option is NOT required, despite the fact it's not Option
    // if absent, the default value will be used
    //#[clap(default_value = "1")]
    //b: u32,

    // this should be a compile time error because default_value (singular)
    // is kinda meaningless with `Vec`
    //
    // This case is what OP complained about: it turns out the field somehow becomes required (WTF???).
    #[clap(default_value = "1")]
    c: Vec<u32>,
    // this should be a legit - and preferable! - usage
    // the field is not required
    //#[clap(default_values = &["1"])]
    //d: Vec<u32>,
}

fn main() {
    let opts = Opts::parse(); // panics
    dbg!(opts);
}
❯ cargo run
   Compiling test-clap v0.1.0 (/home/epage/src/personal/test-clap)
    Finished dev [unoptimized + debuginfo] target(s) in 0.60s
     Running `target/debug/test-clap`
[src/main.rs:27] opts = Opts {
    c: [
        1,
    ],
}
❯ cargo run -- --help
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/test-clap --help`
test-clap 

USAGE:
    test-clap [C]...

ARGS:
    <C>...    [default: 1]

FLAGS:
    -h, --help       Print help information
    -V, --version    Print version information

Am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-bug Category: Updating dependencies
Projects
None yet
Development

No branches or pull requests

7 participants