Commit Graph
26 Commits
Author SHA1 Message Date
Nate Cook a9b9644153 Stop removing underscores from CodingKey names in InputKey (#548)
When a property wrapper is applied to a property, the property's
storage is given a name with a prefixed underscore. That is,
for a property named `x`, the actual storage is named `_x`.

That prefixed storage is what is visible through reflection, so
when building an ArgumentSet from a command type's Mirror, we
need to remove the leading underscore. This is done when creating
an InputKey for each property.

However, InputKeys are also created from CodingKeys during
decoding of a ParsableCommand. These CodingKeys _do not_ have
the leading underscore that is visible, so any underscores
that appear are actually from the declaration of the property
with an underscored name. Removing leading underscores from
CodingKey names results in a mismatch when trying to find
the decoded value.

This change simplifies the InputKey type to use an array
path instead of an indirect enum and removes the leading
underscore dropping when creating an InputKey from a CodingKey.

rdar://104928743
2023-02-02 08:33:51 -06:00
Rauhul Varma e7f312e06e Cleanup Foundation usage (#528)
- Changes uses of import Foundation in ArgumentParser to only expose the
  symbols needed to avoid growing unintentional dependencies.
- Replaces direct usage of EXIT_FAILURE in MessageInfo with ExitCode,
  exposed as a result of the above change.
2022-11-21 22:27:42 -08:00
David Peterson 587d26a2aa Fixes incorrect value copying when ParseArguments has the same field name+type as the ParseCommand (Issue #322) (#495)
* Contains fixes and test cases for #322
2022-09-22 09:43:48 -05:00
Rauhul Varma e7765e1f39 Replace createHelp and includeHidden (#405)
- Replaces `ArgumentSet.init(_:creatingHelp:includeHidden:)` with
  `ArgumentSet.init(_:visibility:)`. `visibility` intentionally does not
  have a default value to ensure that callers only have the correct
  arguments. As part of this change `includeHidden` has been replaced
  throughout the codebase with `visibility`. This change also fixes a
  bug where arguments with hidden `visibility` were being displayed in the
  generated command usage string.
2022-02-14 23:54:57 -06:00
Rauhul Varma 185b45fa06 Combine HelpCommand and HelpHiddenCommand (#408) 2022-02-12 16:35:31 -06:00
Keith Smiley b547374049 Add --help-hidden for use with _hiddenFromHelp (#366)
Swift Package Manager adopted _hiddenFromHelp, the resulting help is
much more approachable for basic usage, but leaves no way to view
all the advanced options it accepts. This takes from swiftc's + clang's
playbook and adds a hidden `--help-hidden` flag that prints all help,
including those using `_hiddenFromHelp`
2022-02-11 14:25:22 -06:00
Nate Cook 5540737e97 Add customization point for command usage text (#400) 2022-02-11 13:41:27 -06:00
Rauhul Varma b3bef58985 Improvements to --dump-help (#335)
- Removes `HelpInfo` in favor of a recursively defined `CommandInfo`
  which contains more raw metadata about the source command.
  Additionally, introduces a top level `ToolInfo` type with a
  serialization version to aid future tooling.

- Updates tests to match the new serialized format.

- Renames `DumpHelpInfoGenerator` to `DumpHelpGenerator` to align the
  type with the `--dump-help` flag.
2021-08-26 14:21:45 -05:00
Kotaro Suto cfcb9cb0cd Add new built-in flag --dump-help (#310)
This commit will add a new builtin option named `--dump-help-info` which
outputs help information in JSON.
2021-07-07 13:09:21 -05:00
Nate Cook 992a7451d5 Convert all public enums to structs (#299) 2021-05-15 10:30:48 -05:00
Alfredo Delli Bovi 4793b0f4b9 Include help text in error message when validation fails (#283) 2021-03-06 13:11:58 -06:00
Nate Cook e5178a971d Remove special casing for CustomNSError (#276)
The error code in CustomNSError is actually _not_ appropriate to use as
the command's exit code.
2021-02-18 14:03:13 -06:00
Kenny 267558bb4d Beautify NSError cases when thrown 💄 (#272) 2021-02-15 17:17:13 -06:00
Nate Cook 75c4dcd2e7 Use the correct help flag in error messages (#263) 2021-01-16 02:32:24 -06:00
Sergey Petrachkov 53555a0450 Support Exit codes from thrown CustomNSError conformers (#244)
Introduce customnserror support, so exit code is calculated correctly, resolves #243.
2020-11-05 13:37:36 -06:00
Nate Cook c3c1a5fc77 Add an experimental customization point for the error label (#223)
SwiftPM uses "error:" instead of "Error:" to make its error
messages match the compiler.
2020-08-05 08:46:11 -05:00
Nate Cook 280700d361 Add completion script generation (#123)
Support for generating shell completion scripts for `ParsableCommand`
types, with customization points for `ExpressibleByArgument` types and
individual arguments and options. Zsh and Bash are supported in this
initial release.
2020-07-29 17:58:44 -05:00
Nate Cook 85196ee1d8 Additional help messages (#165)
* Add 'see help' messages to usage messages and the help screen

* Update tests for new help messages.

* Update guide examples with additional help messages
2020-05-22 17:04:14 -05:00
Nate Cook 44fb59ef87 Add APIs for generating the help screen (#142)
* Add public API for generating help text

* Add tests for `helpMessage()`

* Update documentation

* Use new rendered method instead of property
2020-05-14 12:43:21 -05:00
John Mueller 501bf60536 Display help when no arguments results in error (#140)
If a command cannot successfully run with zero arguments, print the error and the full help message instead of the short usage message.

This closes #134.
2020-05-14 09:08:19 -05:00
Nate Cook 31799bc1b4 Add built-in support for --version flag (#102)
* Add built-in support for --version flag

* Test that command-defined --version overrides the built-in.

* Document the `version:` parameter in CommandConfiguration

* Include --version in the generated help.
2020-03-30 12:36:21 -05:00
Nate Cook ddc828f8cb Add an API for converting an error to an exit code (#79)
* Add an API for converting an error to an exit code

* Make ExitCode more useful as a value type

* Update tests to use ExitCode values

* Typo fix

* Add a test for ExitCode.isSuccess

* Switch to just using ExitCode for tests
2020-03-21 12:44:32 -05:00
Nate Cook 9ebf401d27 Add support for exiting without printing an error. (#51)
This adds an `ExitCode` error type that stores an exit code,
and updates the exit machinery to (1) use `ExitCode` as the source
for exit codes, and (2) silently exit when the provided error
is an `ExitCode` instance.
2020-03-04 13:10:28 -06:00
Ingmar Stein 0a401ef48c Use EX_USAGE instead of EXIT_FAILURE for validation errors. (#15)
sysexits(3) documents the "preferable exit codes for programs". It
defines

```
     EX_USAGE (64)         The command was used incorrectly, e.g., with the
                           wrong number of arguments, a bad flag, a bad syntax
                           in a parameter, or whatever.
```

So it seems to be a good choice for this use case.

Glibc also defines EX_USAGE
2020-03-02 10:46:23 -06:00
Alexander Cyon d131c47155 Fixing 12 typos across 6 files. (#13) 2020-02-28 13:33:57 -06:00
Nate Cook f6ac7b8118 Initial import of ArgumentParser 2020-02-27 15:45:22 -06:00