Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

Optional positional arguments that get skipped if corresponding validation fails #164

Open
epage opened this issue Dec 6, 2021 · 1 comment

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by ducaale
Tuesday Sep 01, 2020 at 18:51 GMT
Originally opened as clap-rs/clap#2122


Describe your use case

I am trying re-implement HTTPie cli using clap-rs/structopt. I would like to have something like this

yahc.exe [METHOD] <URL> [REQUEST_ITEM]...

where METHOD is an optional positional arg enum with a default value

Describe the solution you'd like

It would be good if clap-rs allowed multiple optional positional arguments that get filled with a default value whenever the corresponding value fails to parse. For example, if we had:

test.exe [arg1] [arg2] [arg3]...

where:

  • arg1 has a type of enum Arg1 { Foo, Bar } and defaults to Arg1::Bar
  • arg2 has a type of u32 and defaults to 0
  • arg3 has a type of Vec<String>

a user can use the CLI app in the following ways:

  • test.exe: arg1 = default_value, arg2 = default_value, arg3 = default_value
  • test.exe foo: arg1 = foo, arg2 = default_value, arg3 = default_value
  • test.exe 1: arg1 = default_value, arg2 = 1, arg3 = default_value
  • test.exe hello: arg1 = default_value, arg2 = default_value, arg3 = [hello]
  • test.exe foo hello: arg1 = foo, arg2 = default_value, arg3 = [hello]

Alternatives, if applicable

I tried using the following but they didn't fit my use case:

  • AllowMissingPositional can only be used when the last positional argument is required
  • optional subcommands cannot be followed by positional args if the subcommand is omitted

Additional context

clap-rs/clap#2120

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pksunkara
Thursday May 20, 2021 at 18:28 GMT


Might be related to #975

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant