Commit graph

129 commits

Author SHA1 Message Date
Ed Page
78e4c90326 refactor(tests): Prepare for Special Type experiments
In experimenting on #1772, I want to write test cases for various
combinations of required or not, values vs occurrences, etc.  There
wasn't really a clear place to put these.

On top of that, I wanted there to be a clear place in the tests for
describing the behavior of special types, to make it easier to audit and
easier to see how a PR for #1772 changes things.

As part of this effort in organizing these tests, I reduced the number
of tests that use special types.  This better focuses these tests on the
cases they are intending to cover, rather than pulling in unrelated
features.  This makes it easier to audit special types and makes it so
failures give more focused results, making it easier to see what broke.
2021-11-04 13:12:01 -05:00
Ed Page
a9c6a5b531 refactor(tests): Remove unnecessary color disabling
The tests are using `to_string` which maps to `Display::fmt` and the
`Colorizer` version is colorless.  To get color, it is only supported
with `print()` which has to go to stdout / stderr and can't be directed
to an arbitrary stream.
2021-11-02 16:40:31 -05:00
Ed Page
4b7ed54d7e test(derive): Provide better error info
`Parser::parse_from` will call `exit` on failure and we don't just lose
backtrace information but we don't even know which of the tests running
in parallel panicked.  I ran into this when experimenting with
`clap_derive` and I couldn't tell what actually failed.

So let's switch to `Parse::try_parse_from`.

Errors went from:
```
test option_option ... ok
error: Found argument 'bar' which wasn't expected, or isn't valid in this context

USAGE:
    clap_derive [OPTIONS]

For more information try --help
error: test failed, to rerun pass '--test arg_enum'
```
To:
```
test option_option ... ok
test variant_with_defined_casing ... ok
test skip_variant ... ok
test default_value ... ok
test vector ... FAILED
test option_vector ... ok

failures:

---- vector stdout ----
thread 'vector' panicked at 'called `Result::unwrap()` on an `Err` value: Error { message: Formatted(Colorizer { use_stderr: true, color_when: Auto
, pieces: [("error:", Some(Red)), (" ", None), ("Found argument '", None), ("bar", Some(Yellow)), ("' which wasn't expected, or isn't valid in this
 context", None), ("\n\n", None), ("USAGE:\n    clap_derive [OPTIONS]", None), ("\n\nFor more information try ", None), ("--help", Some(Green)), ("
\n", None)] }), kind: UnknownArgument, info: ["bar"], source: None, backtrace: Backtrace }', clap_derive/tests/arg_enum.rs:388:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    vector

test result: FAILED. 15 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass '--test arg_enum'
```
2021-10-30 10:00:34 -05:00
Ed Page
93c26dd420 test(derive): Verify derive-genned errors are formatted
This is to help verify behavior added in #2943.  We separated the error
raising site from the error formatting site and this verifies that the
formatting actually happens.
2021-10-29 16:09:07 -05:00
Ed Page
d05622d015 fix(derive): Don't duplicate subcommand aliases
When an anonymous struct is inside of an enum, we end up applying the
App methods twice, once for the `augment_args` and once for variant.

This consolidates those calls.

Fixes #2898
2021-10-26 16:01:05 -05:00
Ed Page
69a7b7133a fix(derive): Use variants help_heading
Due to a copy/paste bug, we were reading the `help_heading` for
Subcommands from the enum's attribute and not the variant's attribute.

It doesn't make sense for the outer command's help_heading to control
the subcommands help_heading.

This does raise an interesting question on inheriting / propagating help_heading,
which I originally wrote the tests for.  We'd first need to answer
whether it should be built-in to the builder or derive-specific.
2021-10-26 12:49:43 -05:00
Ed Page
5d036d4d34 fix(derive): Support arg_enum everywhere
In working on converting unwraps to errors, I noticed that we did not
spport `arg_enum` for  `Option<Option<_>>` and `Option<Vec<_>>`, so this
addresses that.

My main motivation was to consolidate and make the logic more
consistent, the bug fix just fell out of that work.
2021-10-25 14:12:08 -05:00
Ed Page
e5f10c81bc feat(derive): Allow help_heading on flattening fields
Fixes #2928
2021-10-23 16:05:08 -05:00
bors[bot]
c4dfb1aede
Merge #2916
2916: fix ArgEnum multiline doc comment r=epage a=marjakm



Co-authored-by: Mattis Marjak <mattis.marjak@gmail.com>
2021-10-19 17:06:14 +00:00
Mattis Marjak
cba7ba5369 fix ArgEnum multiline doc comment 2021-10-19 18:34:54 +03:00
Ed Page
9f12bfec47 fix!: Rename ArgValue to PossibleValue
In considering potential work for #2683, I realized we might need a type to carry data for
each of the `multiple_values`.  `ArgValue` works both for that and for
possible values, so we need to come up with a better name for one or
both.  Changing `ArgValue`s name now would be ideal since its new in
clap3 and by renaming it, we can reduce churn for users.

While thinking about this, I realized I regularly get these mixed
up, so renaming `ArgValue` to `PossibleValue` I think will help clear
things up, regardless of #2683.
2021-10-19 10:10:37 -05:00
rhysd
012f318c97 feat(doc): Fix many typos in docs, comments and codes found by typos-cli 2021-10-19 10:38:22 +09:00
Julius Michaelis
16af4f230a cosmetic: spec_vals on separate lines for long help 2021-10-17 22:43:06 +09:00
Ed Page
65e4bf232d fix(derive): Don't leak help headings
Before, when `flatten`ing, the struct you flattened in could set the
help heading for all the following arguments.  Now, we scope each
`flatten` to not do that.

I had originally intended to bake this into the initial / final methods
but that would have required some re-work to allow capturing state
between them that seemed unnecessary.

Fixes #2803
2021-10-16 11:21:16 -05:00
bors[bot]
6eacd8a747
Merge #2882
2882: fix(derive): Ensure App help_heading is applied r=epage a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
2021-10-16 02:47:34 +00:00
Ed Page
22edac66d9 fix(derive): Ensure App help_heading is applied
We normally set all app attributes at the end.  This can be changed but
will require some work to ensure
- Top-level item's doc cmment ins our over flattened
- We still support `Args` / `Subcommand` be used to initialize an `App` when
  creating a subcommand

In the mean time, this special cases `help_heading` to happen first.
We'll need this special casing anyways to address #2803 since we'll need
to capture the old help heading before addings args and then restore it
after.  I guess we could unconditionally do that but its extra work /
boilerplate for when people have to dig into their what the derives do.

Fixes #2785
2021-10-15 14:19:16 -05:00
Ed Page
7b3d3401c0 fix(derive): Support 'update' with 'flatten'
When working on #2803, I noticed a disprecancy in the augment
behavior between `Arg`s and `Subcommand`s that makes it so `Arg`s can't
be flattened with the update functionality.
2021-10-15 14:19:16 -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
Ed Page
60c9c2e59a docs(derive): Use more-specific traits
This helps to raise visibility of the new derive traits.

I didn't touch ui tests because there were a lot and they didn't seem to
be as high value.
2021-10-12 07:51:11 -05:00
Pavan Kumar Sunkara
685601e002 fix: Add test for flattening struct in enum 2021-10-11 21:54:44 +01:00
Ed Page
6dd9d467ce fix(help)!: Consoldiate color settings
A lot of users expected `color` feature flag and `ColorAuto` etc to
control all colors.  Having this extra flag around is easy to miss and
adds to our overall settings bloat, making it harder to find settings
people want.

This completely removes it, rather than make it deprecated like
functions in #2617, because there is extra work to mark things
deprecated as Settings and we should decide on our strategy first before
investing time in addressing that issue.

Fixes #2806
2021-10-11 09:01:13 -05:00
Ed Page
d840d5650e fix(derive)!: Rename Clap to Parser.
Before #2005, `Clap` was a special trait that derived all clap traits it
detected were relevant (including an enum getting both `ArgEnum`,
`Clap`, and `Subcommand`).  Now, we have elevated `Clap`, `Args`,
`Subcommand`, and `ArgEnum` to be user facing but the name `Clap` isn't
very descriptive.

This also helps further clarify the relationships so a crate providing
an item to be `#[clap(flatten)]` or `#[clap(subcommand)]` is more likely
to choose the needed trait to derive.

Also, my proposed fix fo #2785 includes making `App` attributes almost
exclusively for `Clap`.  Clarifying the names/roles will help
communicate this.

For prior discussion, see #2583
2021-10-09 20:12:03 -05:00
bors[bot]
e3b23929b3
Merge #2823 #2824 #2827
2823: fix(derive): Subcommands not working within macro_rules r=epage a=epage



2824: docs(derive): Fix explanation on optional string list argument r=epage a=epage



2827: feat: Add backtraces to errors r=epage a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
2021-10-07 18:49:59 +00:00
bors[bot]
6c2daef7a1
Merge #2821 #2822
2821: test(derive): Port structopt flatten coverage r=epage a=epage



2822: feat(derive): Add support for lower/upper in rename_all r=epage a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
2021-10-07 16:02:20 +00:00
Ed Page
e42487f6e8 fix(derive): Subcommands not working within macro_rules
This carries over a test case from
https://github.com/TeXitoi/structopt/pull/448, and re-fixes it according
to the changes we've made since we forked.  I also tried to  identify
other cases and quote them to avoid playing whack-a-mole with this.

This is a part of #2809
2021-10-07 10:05:30 -05:00
Ed Page
f681e46414 test(derive): Port structopt flatten coverage
https://github.com/TeXitoi/structopt/pull/414 was ported in
a95195874 but the test was less exhaustive.  This updates our test to
match structopt's latest version of the test.

This is a part of #2809
2021-10-07 10:02:34 -05:00
Ed Page
e10b5281d6 feat(derive): Add support for lower/upper in rename_all
Some programs do not use anything to separate word boundaries.

For example a struct may contain the field `build_dir` while the flag is
`--builddir`.

This is a port of https://github.com/TeXitoi/structopt/pull/412

This is part of #2809
2021-10-07 10:02:34 -05:00
Ed Page
6594e28776
Merge pull request #2826 from epage/skip
feat(derive): Add skip attribute support for enum variants
2021-10-06 19:16:42 -05:00
Ed Page
16023cf157 feat(derive): Add skip attribute support for enum variants
> Resolves #493

This is a port of https://github.com/TeXitoi/structopt/pull/494

This is part of #2809
2021-10-06 13:52:02 -05:00
Ed Page
51c723a84f fix(derive): Fix the problem where the build fails due to the ambiguous type of map
> This PR closes #490. Please refer to #490 for the detail of the problem. Let me know if you want to make `convert_type` a function.

This is a port of https://github.com/TeXitoi/structopt/pull/491

This is part of #2809
2021-10-06 13:22:56 -05:00
Ed Page
6f95650a91
Merge pull request #2805 from epage/validate_default
fix(derive): Type check `default_value_t`
2021-10-06 08:54:59 -05:00
Ed Page
9afa08ff50 fix(derive): Support SubcommandsNegateReqs
Before there was no way to make `SubcommandsNegateReqs` with
`clap_derive` because it required a required field with a sentinel value
for when the required part was negated.  We blocked that.

This turned out simpler than I expected.

This came out of the discussion for #2255 but that issue is more
specifically about the panic, so not closing it.
2021-10-05 16:02:02 -05:00
Ed Page
d0b22b53ff fix(derive): Type check default_value_t
Fixes #2064
2021-10-04 13:02:47 -05:00
ModProg
08e8c53862 refactor(ArgEnum): replace unwrap with except, arg_value -> to_arg_value 2021-10-01 16:14:10 +02:00
ModProg
d53778ed5d fix ui 2021-09-29 19:31:05 +02:00
ModProg
480035ac9c ArgEnum: Slice instead of array, from_str in ArgEnum implemented 2021-09-29 18:33:43 +02:00
ModProg
ac1a9d6d13 address epage's remarks 2021-09-27 22:06:17 +02:00
ModProg
f002cdcc99 move alias implementation to ArgValue for derive 2021-09-27 01:18:47 +02:00
ModProg
b589a6ce06 feat(derive:arg_enum): use ArgValue in ArgEnum trait 2021-09-26 16:32:46 +02:00
corentin.regal
8df90fbd0e Allow subcmd name to be something else than a literal in clap derive 2021-09-06 09:13:20 +02: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
Pavan Kumar Sunkara
badcb3ac7b
Merge pull request #2684 from epage/update_ext
test: Cover external subcommand updates
2021-08-13 20:51:21 +01:00
Ed Page
2458d3934a test: Cover external subcommand updates
We don't know enough to do partial updates, so we are only doing full
updates.

Closes #2212
2021-08-13 12:33:12 -05:00
Ed Page
9a180c1511 feat(derive): Specify defaults by native expressions
Right now
- `default_value="something"` is a raw method
- `default_value` uses native types

This commit splits the meanings
- `default_value="something"` is a raw method
- `default_value_t` uses `T::default()`
- `default_value_t=expr` uses an expression that evaluates to `T`

This is meant to mirror the `value_of` / `value_of_t` API.

At the moment, this is limited to `T: Display` to work with clap's
default system.  Something we can look at in the future is a way to
loosen that restriction.  One quick win is to specialize when `arg_enum`
is set.  The main downside is complicating the processing of attributes
because it then means we need some processed before others.

Since this builds on `clap`s existing default system, this also means
users do not get any performance gains out of using `default_value_t`,
since we still need to parse it but we also need to convert it to a
string.

Fixes #1694
2021-08-13 12:20:54 -05:00
Ivan Tham
f484e2da11 Derive doc clap ordering for multiple Clap
Fix #2527
2021-08-11 23:31:00 +01:00
patrick-gu
eca8ba6098 Make multiple_values false with Option<Option<...>> 2021-08-01 14:50:31 -07:00
Pavan Kumar Sunkara
ff95eb2f5b
Merge pull request #2647 from clap-rs/msrv
Bump msrv to 1.54.0
2021-07-31 05:45:02 +01:00
Ed Page
52d3942ba6 fix(derive): Reduce compiler error noise
It looks like CI hasn't been running on this and we've introduced some
problems.  It looks like we had an off-by-one error in the check for
MSRV for deciding to run ui tests.
2021-07-30 10:19:21 +01:00
Ed Page
241d183b9c Bump MSRV to 1.54.0
- This makes it so `doc` compiles on stable

Fixes #2618
2021-07-30 10:19:21 +01:00
Ed Page
78aa86cfbb fix(derive): Don't mix implicit and explicit value_names
It turns out `value_name` appends, so by setting an implicit and
explicit `value_name`, the user gets both and `num_vals=2`.

There is still a question on `value_name` and whether its documentation
or behavior needs updating.  If that changes, then this can be
simplified by reverting back.

Fixes #2632
2021-07-28 09:46:13 -05:00