Commit graph

625 commits

Author SHA1 Message Date
Pavan Kumar Sunkara
e63760e461 Allow possible_values to take string vector reference
Makes it more backward compatible and flexible
2021-10-17 17:01:18 +01:00
bors[bot]
1c2b09e57b
Merge #2890
2890: feat: Expose clap-style errors to users r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
2021-10-17 00:12:07 +00:00
Ed Page
b9cc585997 feat: Expose clap-style errors to users
This gives users the basic error template for quick and dirty messages.
In addition to the lack of customization, they are not given anything to help
them with coloring or for programmayic use (info, source).

This is something I've wanted many times for one-off validation that
can't be expressed with clap's validation or it just wasn't worth
the hoops.  The more pressing need is for #2255, I need `clap_derive`
to be able to create error messages and `Error::with_description` seemed
too disjoint from the rest of the clap experience that it seemed like
users would immediately create issues about it showing up.

With this available, I've gone ahead and deprecated
`Error::with_description` (added in 58512f2fc), assuming this will be
sufficient for users needs (or they can use IO Errors as a back door).
I did so according to the pattern in #2718 despite us not being fully
resolved on that approach yet.
2021-10-16 19:03:17 -05:00
bors[bot]
d78d5a3744
Merge #2899
2899: Allow RegexSet for validator_regex r=epage a=pksunkara



Co-authored-by: Pavan Kumar Sunkara <pavan.sss1991@gmail.com>
2021-10-16 21:40:07 +00:00
Pavan Kumar Sunkara
7dc176ab2a Allow RegexSet for validator_regex 2021-10-16 22:30:52 +01:00
bors[bot]
b835ce9061
Merge #2817
2817: Add support for Multicall executables as subcommands with a Multicall setting r=pksunkara a=fishface60



Co-authored-by: Richard Maw <richard.maw@gmail.com>
2021-10-16 00:32:52 +00:00
Ed Page
81e877c6c2 fix(help): Partial fix for 'help help'
Who knew people need to ask `help` for how to use `help`?

While auditing `MultpleValues`, I saw commented out code.   Looks
its commit (f230cfedc) was part of a large refactor and updating that
part fell through the cracks.  Just simply updating it didn't quite get
it to work.  The advantage of this approach is it gets us closer to how
clap works on its own.

In clap 2.33.3, `cmd help help` looks like
```
myapp-help
Prints this message or the help of the given subcommand(s)

USAGE:
    test-clap help [subcommand]...

ARGS:
    <subcommand>...    The subcommand whose help message to display
```

But clap3 master looks like:
```
myapp

USAGE:
    myapp [SUBCOMMAND]

OPTIONS:
    -h, --help    Print custom help about text

SUBCOMMANDS:
    help      Print this message or the help of the given subcommand(s)
    subcmd
```

This change improves it to:
```
myapp-help

Print this message or the help of the given subcommand(s)

USAGE:
    myapp help [SUBCOMMAND]...

ARGS:
    <SUBCOMMAND>...    The subcommand whose help message to display

OPTIONS:
    -h, --help    Print custom help about text
```

We still have global arguments showing up (like `-h`) that will error but its
an improvement!  In general, I'd like to find a way to leverage clap's stanard
behavior for implementing this so we don't have to worry about any of these
corner cases in the future.

Note: compared to clap2, I changed `<subcommand>` to `<SUBCOMMAND>` because I
believe the standard convention is for value names to be all caps (e.g.
`clap_derive` has been updated to default to that).
2021-10-15 18:24:15 -05:00
Richard Maw
b92f2c0339 fixup! formatting lint 2021-10-15 21:14:26 +01:00
Richard Maw
636ecaffa2 fixup! make new tests compile but not run without unstable-multicall 2021-10-15 20:59:33 +01:00
Ed Page
6ee5fc4d5d fix(app): Rename generaet_usage to render_usage
This is inline with all of our other help-related functions that return
strings.

This is a part of #2164

BREAKING CHANEG: `App::generate_usage` (added in v3) ->
`App::render_usage`.
2021-10-15 09:36:44 -05:00
Ed Page
7f05c15a5e fix: Give Arg::help_heading priority over App::help_heading
PR #1211 originally added `help_heading` with the current priority
(`App::help_heading` wins).

In #1958, the author had proposed to change this

> Note that I made the existing arg's heading a priority over the one in App::help_heading

This was reverted on reviewer request because

> Thanks for the priority explanation. Yes, please make the app's help
> heading a priority. I can see how it would be useful when people might
> want to reuse args.

Re-using args is an important use case but this makes life difficult
for anyone using `help_heading` with `clap_derive` because the user
can only call `App::help_heading` once per struct.  Derive users can't get
per-field granularity with `App::help_heading` like the builder API.

As a bonus, I think this will be the least surpising to users.  Users
generally expect the more generic specification (App) to be overridden by the
more specific specification (Arg).  Honestly, I had thought this PR is
how `help_heading` worked  until I dug into the code with #2872.

In the argument re-use cases, a workaround is for the reusable arguments
to **not** set their help_heading and require the caller to set it as
needed.

Fixes #2873
2021-10-14 18:23:10 -05:00
bors[bot]
11453065c5
Merge #2871 #2872 #2876
2871: Better positional checks r=epage a=pksunkara



2872: Iterate on help_heading to prepare for derive support r=pksunkara a=epage



2876: Generate/bash: add possible_values to completion when available r=pksunkara a=nstinus



Co-authored-by: Pavan Kumar Sunkara <pavan.sss1991@gmail.com>
Co-authored-by: Ed Page <eopage@gmail.com>
Co-authored-by: Nicolas Stinus <nstinus@latourtrading.com>
2021-10-14 20:23:54 +00:00
Ed Page
072d038d8f fix: Apply app help heading in App::args()
We aren't setting it when bulk-adding arguments.
2021-10-14 13:23:36 -05:00
Ed Page
a570976d92 fix(app): Make App and Arg help_heading consistent
In part, this is just fixing a papercut where someone will try to use
the API in the same way between the two but it fails and they'll have to
consult the docs / rust-analyzer.

The bigger reason is that this is more derive-friendly for dealing with #2803
since we will be able to just ask for the current help heading
before running the app and then restore it back, rather than having to
conditionalize the revert logic.
2021-10-14 11:39:51 -05:00
Ed Page
61c9e6265b fix(help)!: Merge OPTIONS / FLAGS default groups
For those that want the original behavior, you can usxe
`arg.help_heading(Some("FLAGS"))` on your flags.  Limitations:
- This will not give you a special sort order
- This will not get a `[FLAGS]` added to usage

For templates, we removed `{unified}` and `{flags}`.  To help people
catch these, a debug_assert was added.

I'm unsure but I think there might be a change in behavior in calcuating
when to show `[OPTION]` in usage.  The old code only looked at
`required` while flags looked only at arg groups.  We now look at both.

Ideally we'd add these in `_build` and remove special casing for
no-groups except in the sort order of groups.  I feel like thats best
left for later.

This also reduced the scope of `App`s public API.
`get_*_with_no_heading` seemed a bit specialized to be in the public
API.  #2853 looks at splitting it out into its own PR.

BREAKING CHANGE: Multiple
- `UnifiedHelpMessage` removed
- `{flags}` and `{unified}` are removed and will assert when present.
- `get_*_with_no_heading` removed

Fixes #2807
2021-10-13 11:42:10 -05:00
Pavan Kumar Sunkara
efeb02cd34 fix!: Make color settings an enum 2021-10-13 13:54:44 +01:00
Richard Maw
694329b363 fixup! Make hostname a non-derive example and rename via Cargo.toml 2021-10-12 22:38:43 +01:00
Richard Maw
b2180e9d72 fixup! Gate Multicall behind unstable feature 2021-10-12 20:35:59 +01:00
Ed Page
f48261a2a2 Partial Revert of "Change MissingArgumentOrSubcommand to DisplayHelpOnMissingArgumentOrSubcommand and don't use stderr"
This partially reverts commit 7f627fc.

This reverts the error code change but not the `ErrorKind` change.  I
was mixed on whether we should do that or not.  The benefit is it makes
it so people can check the Kind for cases like #2021.  On the other
hand, it doesn't seem that hard to re-implement the feature.

Fixes #2767
2021-10-12 14:09:08 -05:00
bors[bot]
6d046c7aac
Merge #2848
2848: fix: Deprecate Macro API r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
2021-10-12 07:36:50 +00:00
Ed Page
350ab9daf7 fix(help): Show [OPTIONS] with help_heading
This was changed in #1989 without an explanation:
- In the help template, there isn't a way to expose with help_headings,
  so show all.
- In usage, we don't know whether the user wants to see `[FLAGS]` /
  `[OPTIONS]` or not, so let's default to showing them.
2021-10-11 19:13:53 -05:00
Ed Page
0a53fafddf fix: Deprecate Macro API
Fixes #2835
2021-10-11 15:06:38 -05:00
Richard Maw
d062f8f3ad fixup! Remove --install from tests/subcommands 2021-10-11 20:46:33 +01:00
Richard Maw
f14db03eec Add Multicall setting
If the AppSettings::Multicall setting is given
then argv0 is parsed as the first subcommand argument
or parsed as normal with any other name.
2021-10-11 20:00:24 +01:00
bors[bot]
e8ec11e57f
Merge #2837
2837: fix: Gate App::replace r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
2021-10-09 16:27:41 +00:00
Ed Page
dfbeb71077 fix: Gate App::replace
We are concerned about the level of polish of this feature and are
unsure enough about its future, we've decided to gate it to unblock the
v3 release.
2021-10-09 10:49:10 -05:00
Kevin K
14c8850019
tests: fixes test to new default behavior
This commit corrects tests to not expect the `-V,--version` flag when no
version information has been provided.
2021-10-09 11:12:50 -04:00
Ed Page
56a6a7418e fix: Allow unicode-aware case insensitivity with ArgValue
This also opens us up to being more unicode aware in other places, like
our sorting of arguments in the help.

Fixes #2792
2021-10-09 07:21:45 -05:00
Ed Page
62eff1f8d3
Merge pull request #2804 from epage/multiple
fix(parser): Allow multiple_occurrecnes with positional args
2021-10-04 13:57:33 -05:00
Ed Page
8e780e364d fix(parser): Allow multiple_occurrecnes with positional args
This unblocks
- Defining repeated tuples in positional arguments
- Potentially using this in #1772

Fixes #2784
2021-10-04 12:09:54 -05:00
patrick-gu
edd0124af0
Fix disabling the default help and version subcommands/flags (#2796)
* Check for `DisableHelpFlag` along with `NoAutoHelp`

Also applies for `DisableHelpSubcommand` and `DisableVersionFlag` with `NoAutoVersion`

* Add tests for overriding help and version, and disabling version.

These override by disabling the default and adding a new one.

* Don't use `default_missing_value` for `override_version_using_short`

* Check errors by the API.

This changes the `disabled_version_long` and `disabled_version_short` tests.

This is opposed to comparing the stderr output.
2021-10-04 15:01:09 +01:00
ModProg
d3f0534939 add hidden aliases to ArgValue 2021-09-26 22:46:23 +02:00
Pavan Kumar Sunkara
86fda8bacd
Merge pull request #2786 from jonathanmorley/assert-whitespace
assert on leading and trailing whitespace
2021-09-24 20:25:17 +01:00
Morley, Jonathan
1d3a292d6b assert on leading and trailing whitespace 2021-09-24 12:38:15 -04:00
Pavan Kumar Sunkara
699a3f76d6 Implement BitOr for settings 2021-09-23 14:57:10 +05:30
Roland Fredenhagen
5580e8c465
ArgValue builder (#2758)
* feat(arg_value): ArgValue can be used for possible_values

Through the ArgValue it is possible:

* `hide` possible_values from showing in completion, help and validation
* add `about` to possible_values in completion

* Resolved a few change-requests by epage

* make clippy happy

* add ArgValue::get_visible_value

* remove verbose destructering

* rename ArgValue::get_hidden to ArgValue::is_hidden

* add test for help output of hidden ArgValues

* Documentation for ArgValue

There is an issue that required to implement From<&ArgValue> for
ArgValue. We should probably find a solution without that.

* fix requested changes by epage

* fix formatting

* add deref in possible_values call to remove From<&&str>

* make clippy happy

* use copied() instad of map(|v|*v)

* Finishing up for merge, hopefully

* changes requested by pksunkara
2021-09-19 10:29:09 +00:00
patrick-gu
d79c59ab17 Add test for default_missing_values with value_delimiter 2021-09-18 15:47:03 -07:00
patrick-gu
90dde89ec4 Apply value_delimiter to default values 2021-09-18 15:15:25 -07:00
Pavan Kumar Sunkara
76859b16bb
Merge pull request #2773 from tangmi/fix-large-display-order
Add test for very large display_order values and a possible fix
2021-09-17 19:39:37 +01:00
Mustafa Guler
50b9e65d39 Fix bug with duplicate error messages
This addresses a bug that causes duplicate flags reported in user-facing
error messages when two flags require one-another but also are required
under other conditions. The fix involves removing duplicates in unrolled
requirements, which addresses the user-facing aspect of this bug.
2021-09-17 10:47:06 -04:00
Michael Tang
cc592ed800 Add test for very large display_order values and a possible fix.
Fixes #2772
2021-09-16 19:11:30 -07:00
patrick-gu
044f9c5669
Don't suggest help or --help when not applicable (#2749)
* Don't suggest `help` or `--help` when not applicable

* Apply suggestions from code review

Co-authored-by: Pavan Kumar Sunkara <pavan.sss1991@gmail.com>

* Update test usage to match intended

Co-authored-by: Pavan Kumar Sunkara <pavan.sss1991@gmail.com>
2021-09-04 20:10:24 +00:00
Pavan Kumar Sunkara
f085fa64f4
Merge pull request #2722 from epage/group
fix(yaml): Don't panic on multiple groups
2021-08-19 07:40:41 +01:00
Ed Page
bd25b5f615 fix(yaml): Don't panic on multiple groups
Because we gradually build the `ArgGroup` as we parse the YAML, we don't
use `ArgGroup::new`.  Clap3 introduced an internal `id` in addition to
the public `name` and it appears that this custom initialization code
was not updated.

This shows the problem with publically exposing `impl Default`.
Choices:
- Remove `impl Default`
  - Always valid
  - Requires spreading invariants
  - Callers can't implement code the same way we do
- Add `ArgGroup::name`
  - Can be constructed in an invalid state
  - Centralizes invariants
  - A caller could implement code like the yaml logic

I decided to go with `ArgGroup::name`.

Fixes #2719
2021-08-18 15:16:44 -05:00
Ed Page
aeaf01e3e7 fix: Provide path to avoid UTF-8 panics
Before, validating UTF-8 was all-or-nothing and would cause a `panic` if
someone used the right API with non-UTF-8 input.

Now, all arguments are validated for UTF-8, unless opted-out.  This
ensures a non-panicing path forward at the cost of people using the
builder API that previously did `value_of_os` need to now set this flag.

Fixes #751
2021-08-18 14:15:18 -05:00
Donough Liu
0394ae6102
Fix handling of number_of_values when displaying arg (#2701)
* Remove dead logic

* Outline common logic

* Fix issue 1571
2021-08-17 09:17:18 +00:00
Pavan Kumar Sunkara
9dd75987df Remove TakesValue restriction for HideEnv* 2021-08-15 19:40:24 +01:00
Pavan Kumar Sunkara
203613d5e3
Merge pull request #2696 from ldm0/override
Make `overrides_with` working when `MultipleValues` is enabled.
2021-08-14 10:41:40 +01:00
liudingming
a85857dfb0 Make overrides_with working when MultipleValues is enabled. 2021-08-14 16:34:26 +08:00
Pavan Kumar Sunkara
441ff68c2d
Merge pull request #2694 from clap-rs/env-feature
Add env feature gate
2021-08-14 03:11:21 +01:00