clap icon indicating copy to clipboard operation
clap copied to clipboard

derive error for missing `= value` for attributes is confusing

Open ravenexp opened this issue 3 years ago • 5 comments

Please complete the following tasks

Clap Version

3.1.18

Describe your use case

I'm trying to add an environment variable fallback for the --password option. I do not want the variable value to be accidentally revealed when --help is used.

I tried to write

#[clap(short, long, env = "PROGRAM_PASSWORD", hide_env_values)]
password: Option<String>,

but the error made me think that the attribute doesn't exist when it does:

error: unexpected attribute: hide_env_values

(Note: #3620 is for bool shorthand support)

Describe the solution you'd like

A clearer error message

Alternatives, if applicable

Additional Context

No response

ravenexp avatar May 13 '22 07:05 ravenexp

Boolean builder functions do not have an implied = true but you have to set it manually. The following should work

#[clap(short, long, env = "PROGRAM_PASSWORD", hide_env_values = true)]
password: Option<String>,

See https://github.com/clap-rs/clap/issues/3620

epage avatar May 13 '22 12:05 epage

Thanks! I should've tried that before opening an issue :face_exhaling:

I was getting

error: unexpected attribute: hide_env_values

when compiling, and it is not mentioned anywhere in the documentation, so I just assumed that this attribute was not implemented.

ravenexp avatar May 13 '22 12:05 ravenexp

Thats an artifact of how we parse the attributes. It isn't in our hard coded list of attributes that work without a = or (). It would be nice to improve that message but unsure what to say.

epage avatar May 13 '22 13:05 epage

Would you be opposed to adding the boolean attributes to the list that are accepted without a value? I imagine they all could be treated as true, but I would go through them and check. (To be clear, I’m volunteering.)

Otherwise, I suppose the error could be something like:

error: unexpected attribute without value: hide_env_values

I‘m not sure how much clearer that is, though.

danielparks avatar Jul 31 '22 05:07 danielparks

@danielparks see https://github.com/clap-rs/clap/issues/3620

epage avatar Aug 01 '22 17:08 epage