clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
13.66k stars 1.02k forks source link

Allow longer shorts #5377

Open jhesqqq opened 4 months ago

jhesqqq commented 4 months ago

Please complete the following tasks

Clap Version

4.5.1

Describe your use case

Let short flag allow more characters than one to facilitate user input.

I know that short flags implement their combined behavior by default in clap, which prevents short flags from using more than two characters. For example, ls -l-a = ls -la, which I think is a great feature.

However, implementing only this functionality ignores the case where the user does not want to enter the long flag if there are too many parameters. For example, the user wants to type xxx-oJ instead of xxx --oJ

Describe the solution you'd like

I have a solution, which is that clap can open a method to control whether I want this default composition behavior or not.

When I turn off the composition behavior, the short flag length is at least 1 or greater. For example,-oJ, the output is json, there is no combined behavior here, just a single instance.

When I enable the composition behavior (which is the default), the short flag is limited to a length of 1. Which is now the composition behavior.

Alternatives, if applicable

No response

Additional Context

No response

epage commented 4 months ago

Renamed the title to focus on the problem, rather than solution.

I know this has been discussed before but not finding the Issues or Discussions for it. #1210 is either similar or the same thing, depending on your perspective.

There are several problems to solve

If this is more like #1210, then #2468 is likely the way forward.

jhesqqq commented 4 months ago

I noticed that in v2, clap short allowed multiple characters, but only the first one was retrieved. For example, short("abc") appears as -a on the command line.

API: With clap v3, we intentionally switched short from accepting &str to char to better represent this limitation in type system

I think developing a way to regulate whether or not composition behavior is used is a very important feature for developers and users. The right sacrifice can sometimes be a big help.

Behavior: we have to deal with people who want true shorts with combining of multiple flags + a value with people who want multi-character shorts. There might even people who want both.

  • If its both, that adds its own complexities.
  • If its either, then we have to balance the scope of how much this is needed vs the compile time and binary size of making this large of a behavior swap conditional

I am aware of these issues and hope such issues can be resolved.

If this is more like https://github.com/clap-rs/clap/issues/1210, then https://github.com/clap-rs/clap/issues/2468 is likely the way forward.

Finally, I wish clap better and better!