aobatact / clap-serde

Provides a wrapper to deserialize clap app using serde.
Apache License 2.0
22 stars 3 forks source link

Should 'required = false' in arg. allow default value to be undefined? #50

Closed pintoflager closed 1 year ago

pintoflager commented 1 year ago

If argument is set as required = false it seems to have no impact on argument rendered as an 'option'?

Only way I was able to add optional flags was to give them default_value and hide it from the --help with hide_default_value = true

I was testing with this toml:

name = "Service provider X"
version = "0.1"
author = "author"
about = "Commands to invoke service provider scripts."

[subcommands.node]
about = "Commands for managing server nodes" 
subcommand_required = true

[subcommands.node.subcommands.create]
about = "Create new server nodes"
arg_required_else_help = true

[subcommands.node.subcommands.create.args.name]
help = "Node name within the group"
forbid_empty_values = true

[subcommands.node.subcommands.create.args.group]
# default_value = "something"
# hide_default_value = true
short = "g"
long = "group"
help = "Node group name"

cmd: target/debug/test node create FirstArg --group GroupName

Above fails when --group is defined, works without the flag though. Starts working as expected with or without --group flag when default_value = "something" is uncommented.

Not a big issue, I was just curious. Thank you for your work on this, seems to be the only way to extend clap commands runtime.

epage commented 1 year ago

In addition to describing the scenario, could you copy/paste the output?

pintoflager commented 1 year ago

Sure thing mate, this is what it says:

error: Found argument 'GroupName' which wasn't expected, or isn't valid in this context

I was expecting behaviour like I have with my main cli when I use key: Option<String>. With that I don't have to give default value for the argument.

epage commented 1 year ago

This is using clap v3 where it defaults flags to not take a value. You need to explicitly ask it to do so. This was changed in clap v4, so when clap_serde is updated to that, what you have should work.

The derive does a lot of stuff automatically for you based on the intent of your argument.

pintoflager commented 1 year ago

Ah, got it. I'm a happy derive user on my main cli and never really did any testing without it. Live and learn. thanks