Commit graph

181 commits

Author SHA1 Message Date
shir0kamii
fb4755d1c3 feat(derive): Don't abort when non-unit variant is skipped 2022-03-31 18:49:21 +02:00
shir0kamii
ee3d12ec56 fix(derive): Abort on non-unit variant 2022-03-30 03:49:14 +02:00
Ed Page
cb937641fa fix(derive): Allow other attributes with subcommand that has subcommands
This was overlooked when we added support for `#[clap(subcommand)]` to
variants.

Fixes #3504
2022-02-23 09:23:33 -06:00
Ed Page
ddac492302 fix: Rename IntoApp to CommandFactory
This is part of the `App` rename.

Previously, I was concerned about not being able to deprecate

For backwards compatibility, we still expose the `IntoApp` name.
2022-02-15 08:24:00 -06:00
Ed Page
c3fec1fa75 fix: Update derive->Command function name
No good solution for transitioning the trate name, unfortnately, since
we can't mark `use`s as deprecated (we can, it just does nothing).

I got rid of the `into` prefix because that implies a `self` parameter
that doesn't exist.
2022-02-14 16:04:10 -06:00
Ed Page
e8010e79a9 refactor: Update app variables to cmd 2022-02-14 15:55:56 -06:00
Ed Page
c4144d7d6c docs: Update App references to Command 2022-02-14 15:33:49 -06:00
Ed Page
4895a32e81 fix: Deprecate SubcommandRequiredElseHelp
Now that we can use `SubcommandRequired |
ArgRequiredElseHelp`, this setting offers little value but requires we
track required subcommands with two different settings.  Deprecating as
the cost is not worth the benefit anymore.

Issue #3280 will see the derive updated
2022-02-11 15:31:25 -06:00
Ed Page
85908ce039 refactor(derive): Remove redundant code paths
I'm assuming this is from when structopt always set author, version, and
about and you had to apply other attributes to remove them.
2022-02-11 13:19:14 -06:00
Ed Page
272f840178 feat: Replace core set of AppSettings with functions
This is a part of #2717

Some settings didn't get getters because
- They are transient parse settings (e.g. ignore errors)
- They get propagated to args and should be checked there

`is_allow_hyphen_values_set` is a curious case.  In some cases, we only
check the app and not an arg.  This seems suspicious.
2022-02-11 12:35:09 -06:00
Ed Page
5290f82133 feat: Override DeriveDisplayOrder behavior with App::next_display_order
For the derive API, you can only call `next_display_order` when dealing
with a flatten.  Until we offer app attributes on arguments, the user can workaround with
this no-op flattens.

This is a part of #1807
2022-02-07 19:19:11 -06:00
Ed Page
c00f71ec4a feat: Add App::next_help_heading
This clarifies the intent and prepares for other functions doing the
same, like `next_display_order`.  This will then open us to name
`subcommand_help_heading` and `display_order` similar.

The deprecation is waiting on 3.1.

This is part of #1807 and #1553.
2022-02-07 19:19:01 -06:00
Rob Donnelly
a19bca8a10 docs(error): Update link to explicit bool parser example 2022-01-31 21:58:54 -08:00
omjadas
86c83d296f
feat: Add default_value_os_t (#3333)
The order of suffixes allows us to preserve the original builder function name.

This is a part of #2813
2022-01-24 15:32:21 -06:00
Ed Page
3326a11be0 fix(derive): Don't assume use clap::ArgEnum
The error was when doing `#[clap(arg_enum, default_value_t = ...)]`.

Good example of why we should minimize `use`, at least in tests
(besides reducing merge conflicts, code churn, etc).
2022-01-10 15:41:58 -06:00
Noa
ffd991f0ae
chore: Fix clap_derive license headers 2022-01-04 14:25:48 -06:00
Daniel Eades
2986a9eee0 style: remove unnecessary lazy evaluations 2022-01-04 09:20:17 -06:00
Daniel Eades
7f71d642cd style: prefer 'char' to 'str' for single character patterns 2022-01-04 09:20:08 -06:00
Ed Page
6b9ae5404c fix(derive): Don't enit warnings
We missed covering `Args` warnings when using struct variants.

Fixes #3245
2022-01-03 11:35:34 -06:00
Ed Page
42f6c72caa chore: Upgrade heck 2022-01-01 20:31:53 -06:00
Ed Page
8924dd7a1e feat(derive): Don't require Display for default ArgEnum
While I'm unsure how much type specialization we should do, we
intentionally have the `arg_enum` attribute for doing special behavior
based on it, so let's take advantage of it.

Fixes #3185
2021-12-16 09:11:32 -06:00
Ed Page
d55f040bbd fix(derive): Set both about/long_about with doc comments
The main care about is that when we override a `flatten` / `subcommand`
doc comment in a parent container, that we make sure we take nothing
from the child container, rather than implicitly taking one `about` ut
not `long_about`.

To do this, and to play the most safe with long help detection, we reset
`long_about` to default when there is no doc comment body to use for
`long_about`.

Fixes #2983
2021-12-14 11:07:57 -06:00
Ed Page
7c10b5a9b4 fix(derive): Treat default_value_os like default_value
The test went from panicing to not-panicing

Fixes #3031
2021-12-13 16:25:49 -06:00
Ed Page
43d3ac37a6 fix(derive): Allow aliased Option with ArgEnum 2021-12-10 10:41:55 -06:00
Frankie Foston
1285c0f885 fix: unqualified result types causing compilation failures with derive implementations 2021-12-10 14:13:26 +00:00
Janik Rabe
7b1ea308a0 fix(derive): Use fully qualified types
This fixes `#[derive(Parser)]` when a custom `Result` type is in scope.
2021-12-08 21:07:49 +00:00
Ed Page
cf623d9e53 fix(derive): Update structopt links
Fixes #1671
2021-12-08 11:28:25 -06:00
Ed Page
093de2b83b docs: Add back in logo 2021-12-07 17:45:57 -06:00
Ed Page
b2836c07a7 fix: Gracefully handle empty authors 2021-12-06 11:30:26 -06:00
Ed Page
c23f9230d1 chore: Remove stale boiletplate 2021-12-06 11:04:41 -06:00
Ed Page
b46abb7236 docs: Re-order items in docs
This contains two types of re-ordering:
- Internal, putting the focus of each file first.
- Public, re-arranging items and grouping impl blocks to try to better
  organize the documentation and find related items.

The main weakness of the docs work is in `Args`.  Its just a mess.  In
particular, we should probably link the simple casesm like `required` to
the advanced impl block.

Fixes #55
2021-12-04 19:18:28 -06:00
Ed Page
500def4904 fix(derive): Smooth out transition for structopt
Adding in a `StructOpt` derive with `structopt` attributes, with
deprecation notices.  Unofrtunately, not as many deprecation warnings as
I would like.  Apparently, you can't do them for a `use` of a derive?  I
also wanted to inject code that would trigger a deprecation notice for
attributes but that would require enough of a refactor that I didn't
consider it worth it.  We are at least providing a transition window
even if it means we'll have to remvoe it next major release without a
deprecation warning.
2021-12-02 20:18:33 -06:00
Ed Page
64d052f8b8 fix: Remove untested IntoApp derive 2021-12-02 20:01:00 -06:00
Ed Page
1cdb2250e8 fix(derive): Define multiple policy for Special Types
Before:
- `bool`: a flag
- `Option<_>`: not required
- `Option<Option<_>>` is not required and when it is present, the value
  is not required
- `Vec<_>`: multiple values, optional
- `Option<Vec<_>>`: multiple values, min values of 0, optional

After:
- `bool`: a flag
- `Option<_>`: not required
- `Option<Option<_>>` is not required and when it is present, the value
  is not required
- `Vec<_>`: multiple occurrences, optional
  - optional: `Vec` implies 0 or more, so should not imply required
- `Option<Vec<_>>`: multiple occurrences, optional
  - optional: Use over `Vec` to detect when no option being present when
    using multiple values

Motivations:

My priorities were:
1. Are we getting in the users way?
2. Does the API make sense?
3. Does the API encourage best practices?

I was originally concerned about the lack of composability with
`Option<Option<_>>` and `Option<Vec<_>>` (and eventually `Vec<Vec<_>>`).
It prescribes special meaning to each type depending on where it shows
up, rather than providing a single meaning for a type generally.  You
then can't do things like have `Option<_>` mean "required argument with
optional value" without hand constructing it.  However, in practice the
outer type correlates with the argument occurrence and the inner type
with the value.   It is rare to want the value behavior without also the
occurrence behavior.  So I figure it is probably fine as long as people
can set the flags to manually get the behavior they want.

`Vec<_>` implies multiple occurrences, rather than multiple values.
Anecdotally, whenever I've used the old `Arg::multiple`, I thought I was
getting `Arg::multiple_occurrences` only.  `Arg::multiple_values`,
without any bounds or delimiter requirement, can lead to a confusing
user experience and isn't a good default for these.  On top of that, if
someone does have an unbounded or a delimiter multiple values, they are
probably also using multiple occurrences.

`Vec<_>` is optional because a `Vec` implies 0 or more, so we stick to
the meaning of the rust type.  At least for me, I also rarely need a
required with multiple occurrences argument but more often need optional
with multiple occurrences.

`Option<Vec<_>>` ends up matching `Vec<_>` which can raise the question
of why have it.  Some users might prefer the type.  Otherwise, this is
so users can detect whether the argument is present or not when using
`min_values(0)`.  Rather than defining an entire policy around this and
having users customize it, or setting `min_values(0)` without the rest
of a default policy, this gives people a blank slate to work from.

Another design option would have been to not infer any special-type
settings if someone sets a handful of settings manually, which would
have avoided the confusion in Issue clap-rs/clap 2599 but I see that
being confusing (for someone who knows the default, they will be
expecting it to be additive; which flags disable inferred settings?) and
brittle (as flags are added or changed, how do we ensure we keep this
up?).

Tests were added to ensure we support people customizing the behavior to
match their needs.

This is not solving:
- `Vec<Vec<_>>`, see clap-rs/clap 2924
- `(T1, T2)`, `Vec<(T1, T2)>`, etc, see clap-rs/clap 1717
- `Vec<Option<_>>` and many other potential combinations

Fixes clap-rs/clap 1772
Fixes clap-rs/clap 2599
See also clap-rs/clap 2195
2021-11-29 13:16:12 -06:00
Ed Page
57e5fc2b07 fix: Rename to Arg::ignore_case like ArgSettings
Fixes #32
2021-11-29 10:34:42 -06:00
Ed Page
7e899cd340 Revert "Deprecate Arg::help in favour of Arg::about"
This reverts commits 24cb8b1..d0abb37 from clap-rs/clap#1840

This is part of #16.  clap-rs/clap#1840 wasn't the right call but we
don't have time to make the decision now, so instead of having one
option and changing it in 4.0, this reverts back to clap2 behavior.
2021-11-18 12:25:49 -06:00
kuviman
aa6594b334 feat(derive): Generic support
This is a port of clap-rs/clap#3023
2021-11-17 15:33:59 -06:00
Ed Page
9167fdebf3 fix(derive): Don't emit warnings
Looks like this is coming from `update_from_arg_matches` where we do a
ladder of `if __clap_arg_matches.is_present(...)` that clippy wants to
be `else if`s.  While for human edited code, that does clarify intent,
for machine generated code that is rarely read, its a pain to do, so
silencing it.

Unfortunately, it isn't in a group we can overall silence.

Fixes #3017
2021-11-12 12:52:18 -06: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
53e10b41e3 fix(derive)!: Error, don't panic!
The derive is generating `Error::raw` (from scratch or by converting
existing erors) and then the inherent `Parser` methods format them.

Fixes #2255
2021-10-26 14:26:50 -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
bors[bot]
d6462884ed
Merge #2941
2941: Fix a path incompatible with re-exports r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
2021-10-25 15:40:33 +00:00
Ed Page
345694fea8 fix(derive): Allow subcommands with re-exporting
Our approach to re-exporting is to not fully qualify symbols in `clap`.
This is a place where a leading `::` got introduced, so its being
removed.
2021-10-25 09:44:38 -05:00
Ed Page
ed987933cf test: Verify clap_derive README
Fixes #2920
2021-10-25 09:21:45 -05:00
Ed Page
e5f10c81bc feat(derive): Allow help_heading on flattening fields
Fixes #2928
2021-10-23 16:05:08 -05:00
Ed Page
3a697af253 fix(derive): Avoid name collisions with users
PR #2751 highlighted a problem we have where the variable names we use
could collide with users.  Rather than parse out when or not to use
special names, and worry about people keeping that up to date through
refactors, I globally renamed all variables by adding a `__clap_`
prefix, which looks like what serde does to solve this problem.

I audited the result with `cargo expand`.  I didn't add any tests
because any tests would be reactionary and would give us a false sense
of protection since any new code could hit this with anything we do.
Our best route for naming is consistency so people are likely to notice
and copy.

Fixes #2934
2021-10-23 12:55:30 -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