Commit graph

187 commits

Author SHA1 Message Date
Ed Page
53948db66f style: Make clippy happy 2022-05-25 10:25:57 -05:00
Ed Page
78b6f1ee64 fix(parser): Default construct ArgMatches::remove_many's Iterator 2022-05-25 10:19:05 -05:00
Ed Page
2891a73b2e fix(parser): Default construct ArgMatches::get_many's Iterator 2022-05-25 10:19:05 -05:00
Ed Page
a4e8cc96f9 fix(parser): Default construct ArgMatches::get_raw's Iterator 2022-05-25 10:19:05 -05:00
Ed Page
ed45de2f03 fix(parser): Clean up remove types
The remove functions no longer return `Arc` but the core type, at the
cost of requiring `Clone`.  I originally held off on this
in #3732 in the hope of gracefully transition the derive and requiring
`Clone` would have been a breaking change but when it came to #3734, I didn't
find a way to make it work without a breaking change, so I made it
opt-in.  This means I can force the `Clone` requirement now.

I added the requirement for `Clone` everywhere else in the hopes that in
the future, we can drop the `Arc` without a breaking change.
2022-05-24 16:25:07 -05:00
Ed Page
c6a3871544 fix(error): Include PossibleValuesParser values in error 2022-05-24 14:53:08 -05:00
Ed Page
15616bbd13 fix(error): Remove usage from value validation
When to show usage?  We are currently mixed about it.  For `validator`,
we didn't show it at all.  Sometimes we show the used arguments and
sometimes we don't.

With `ValueParser`, I ran into the problem that we weren't showing the
used arguments like we had previously in some cases.  In deciding how to
solve this, I went with the simplest route for now and removed it as the
usage likely doesn't add much context to help people solve their
problem, more so the recommendation for help.  We'll see how the
feedback is on this and adjust.
2022-05-23 20:56:04 -05:00
Ed Page
acdf26ac38 style: Make clippy happy 2022-05-17 17:13:08 -05:00
Ed Page
91fe9eedc9 feat(parser): Transfer ownership of sub-matches 2022-05-17 16:58:38 -05:00
Ed Page
d2ca18130e feat(parser): Allow removing values from ArgMatches
This allows reducing the use of `Clone`, especially in `clap_derive`.
2022-05-16 15:08:47 -05:00
Ed Page
792fb54990 fix(parser): Be consistently strict with ArgMatches types
This gives us more implementation freedom for the future.
2022-05-16 15:08:47 -05:00
Ed Page
c7bd8c891d perf(parser): Don't allocate on every call 2022-05-16 15:08:47 -05:00
Ed Page
36236957e4 fix(parser): Allow invalid id lookups without panics
Fixes #3621
2022-05-16 15:08:47 -05:00
Ed Page
c331be98a5 fix(parser): Separate dev errros from user errors 2022-05-16 15:08:47 -05:00
Ed Page
66570ee9d0 refactor(parser): Set source at start of occurrence 2022-05-16 15:08:47 -05:00
Ed Page
7cb2a096d0 refactor(parser): Force explicit occurrence start 2022-05-16 15:08:47 -05:00
Ed Page
0d6be4e1a8 refactor(parser): Consistently start occurrences 2022-05-16 15:08:47 -05:00
Ed Page
5532bfc838 refactor(parser): Clarify env code 2022-05-16 15:08:47 -05:00
Ed Page
4845296949 refactor(parser): Clarify default value code 2022-05-16 15:08:47 -05:00
Ed Page
8a58884459 refactor(parser): Initialize MatchedArg from its source 2022-05-16 15:08:47 -05:00
Ed Page
e6b1468477 fix(parser): Set source/occurrences for external subcommands 2022-05-16 15:08:47 -05:00
Ed Page
d538012a74 refactor(parser): separate concerns over external subcommands 2022-05-16 15:08:47 -05:00
Ed Page
0e04367826 fix(parser): Consistently log parser 2022-05-16 15:08:47 -05:00
Ed Page
c74d3fb045 refactor(parser): Reduce visibility 2022-05-16 15:08:47 -05:00
Ed Page
144cd4c3b7 refactor(parser): Group default/env 2022-05-16 15:08:47 -05:00
Ed Page
8c3f540638 refactor(parser): Clarify name for starting occurrences 2022-05-16 15:08:47 -05:00
Ed Page
f296c5ca78 refactor(parser): Fix naming of ty 2022-05-16 15:08:47 -05:00
Ed Page
bc6da813bd fix(parser): Unify handling of AnyValue's type id 2022-05-16 15:08:47 -05:00
Ed Page
9920d5baf5 fix(parser): Improve AnyValue debug output 2022-05-16 15:08:47 -05:00
Ed Page
8b582c3007 refactor(parser): Make AnyValue opaque
This gives a little more implementation flexibility
2022-05-16 15:08:47 -05:00
Ed Page
f16a6fb5a6 refactor(parser): Move AnyValue definition closer to use 2022-05-16 15:08:47 -05:00
Ed Page
2902f190e4 refactor(error): More flexible callers 2022-05-16 15:08:47 -05:00
Ed Page
73fc240c57 refactor(env): Generalize bool policy 2022-05-16 15:08:47 -05:00
Ed Page
200f6626db feat(parser): Add type information to arg values
To set the type, we offer
- `ValueParser::<type>` short cuts for natively supported types
- `TypedValueParser` for fn pointers and custom implementations
- `value_parser!(T)` for specialized lookup of an implementation
  (inspired by #2298)

The main motivation for `value_parser!` is to help with `clap_derive`s
implementation but it can also be convinient for end-users.

When reading, this replaces nearly all of our current `ArgMatches` getters with:
- `get_one`: like `value_of_t`
- `get_many`: like `values_of_t`

It also adds a `get_raw` that allows accessing the `OsStr`s without
panicing.

The naming is to invoke the idea of a more general container which I
want to move this to.

The return type is a bit complicated so that
- Users choose whether to panic on invalid types so they can do their
  own querying, like `get_raw`
- Users can choose how to handle not present easily (#2505)

We had to defer setting the `value_parser` on external subcommands,
for consistency sake, because `Command` requires `PartialEq` and
`ValueParser` does not impl that trait.  It'll have to wait until a
breaking change.

Fixes #2505
2022-05-16 15:08:33 -05:00
Ed Page
dcf69d1c87 refactor(parser): Track str/OsStr values via Box
Unfortunately, we can't track using a `ValueParser` inside of `Command`
because its `PartialEq`.  We'll have to wait until a breaking change to
relax that.

Compatibility:
- We now assert on using the wrong method to look up defaults.  This
  shouldn't be a breaking change as it'll assert when getting a real
  value.
- `values_of`, et al delay panicing on the wrong lookup until the
  iterator is being processed.
2022-05-12 16:13:30 -05:00
Ed Page
cf0282bdae refactor: Remove dead code 2022-05-12 10:35:52 -05:00
Ed Page
4fea1515b6 feat: Expose builder/parser modules
The top-level API for clap is getting a bit bloated.  By exposing these
modules, we'll be able to continue to add new, less commonly used types
while keeping the main API focused.
2022-05-12 06:17:08 -05:00