Commit Graph
32 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
Tiago Lopes 3b6f81459b Fix .postTerminator usage message (#542)
* Fix synopsis for .postTerminator parsing strategy

* Add test for .postTerminator usage message generation
2023-01-13 08:23:44 -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
Nate Cook 4de228195c Show hidden args/opts/flags with --help-hidden (#412)
- Make ArgumentSet(_:visibility:) filter correctly
  The ArgumentSet initializer was previously only filtering out option
  groups with visibility lower than requested. With this change, the
  resulting ArgumentSet only includes values that are valid for display.
  In addition, this moves the visibility parameter out of
  UsageGenerator.synopsis(); that type needs to have the correct
  visibility level at initialization.

- Mark non-parsed properties as private
  This applies to properties that are defined without a property
  wrapper. This kind of property should never be included in the help,
  since they aren't included in the command-line tool's UI.
2022-02-15 14:57:43 -06:00
Rauhul Varma 88af18f986 Change ArgumentVisibility into a struct (#413)
- Changes ArgumentVisibility from an enum to a struct. This will allow
  ArgumentParser to add cases in the future without breaking clients
  that could have been exhaustively switching across all cases. It also
  allows us to implement protocol conformances on the internal type and
  avoid exposing them on the public type.
2022-02-15 12:55:27 -06: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
Rauhul Varma 1e6cf8bf25 Forward ArgumentVisibility to ArgumentDefinition (#406)
- Renames ArgumentHelp.Visibility to ArgumentVisibility.
- Replaces ArgumentDefinition.shouldDisplay with a visibility property
  whose value is derived from ArgumentHelp.visibility.
2022-02-12 00:54:20 -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
Matt Zanchelli 9e14482156 Correct Typos (#388) 2022-01-11 16:47:13 -06:00
Daniel Duan 90f76c14b4 List valid options in error messages (#382)
When an option value fails to parse, no custom error message is
provided, and a list of valid candidate values is available, include the
list as part of the error message.

Addresses #344.
2022-01-05 11:02:31 -06: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
Gonzalo RH 530a754555 Included help message when a required value is missing. (#324) 2021-06-09 11:28:00 -05:00
Rauhul Varma f4353dbe3a Simplify synopsis string generation (#316)
- Removes unused codepaths.
- Simplifies synopsis string codepaths by removing optionality. This
  complexity is moved to the caller who is now responsible for filtering
  out hidden arguments and options. This change is desirable as it
  allows the caller to determine if the argument should be hidden. For
  example, while it makes sense to hide arguments in help text, it may
  not make sense to hide them when dumping the arguments for another
  tool to consume.
2021-05-22 10:59:37 -05:00
Rauhul Varma 860afdad31 Clean up internal property nesting (#315)
- Removes one layer of help properties by directly including the members
  of ArgumentHelp in ArgumentDefinition.Help. This also results in the
  discussion field which previously existed in both structures, now
  having a single source of truth. Adds helper method for setting each
  of these members using an instance of ArgumentHelp. Makes previously
  optional Strings into plain Strings and updates points of use to check
  for the empty string case.
2021-05-20 19:03:19 -05:00
Alfredo Delli Bovi 4793b0f4b9 Include help text in error message when validation fails (#283) 2021-03-06 13:11:58 -06:00
Karoy Lorentey 5bfb39ac07 Generate useful synopsis for commands with many options (#275)
ArgumentParser is configured not to emit detailed synopsis when it would contain more than a dozen entries. This makes sense; however, eliding all information makes the synopsis rather useless.

While commands may have dozens of options, in most cases, only a few of them are required — so we can keep the synopsis short but still useful by only displaying the required parts.

* Include all positional arguments in shortened synopsis
2021-02-16 09:30:31 -06:00
Nate Cook e99a8ef488 Allow variable properties in parsable types (#268)
* Allow variable properties in parsable types

This captures the default value for non-parsable properties
when building the ArgumentSet, which in turn get set as initial
values before decoding.
2021-02-15 15:28:13 -06:00
Nate Cook 75c4dcd2e7 Use the correct help flag in error messages (#263) 2021-01-16 02:32:24 -06:00
Drew McCormack 41f5fe52a3 Gave a more descriptive error message for when a non-argument variable causes a parsing failure. (#256) 2021-01-05 10:22:31 -06:00
Nate Cook 344537137b Flatten the ArgumentSet storage into a single array (#235)
This removes the nesting inside the ArgumentSet data structure, which
had semantic meaning in an earlier version. This flattening, plus a
switch to using dictionary lookup instead of linear scanning, provides
another performance boost.
2020-09-01 21:07:35 -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
ibrahim oktay 59f39ec127 Use the first long name in usage (#179)
* Use the first long name in usage (#167)

Sorting names for option has been changed.
* Usage will choose the first long name, if available, or otherwise the first short name
* Help screen will show short names, then long names, in the order of their declaration.

Solves (#167)

* Fix adding same name more than once.

Checkin long names in sort caused single dash long names to be treated as a short name.
Fixed sort to check “short” instead of “long”

Update current test to cover more cases.

* Add isShort control to Name

*Replace sortedNames with partitionedNames

*Extract uniquifying logic into SequenceExtensions.swift

* Refactor short test with computed property

* Use pattern-matching instead of equality comparison
2020-06-09 17:14:06 -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
1081d08b1d Improve errors messages thrown from a transform closure (#115)
* Add a test for @Option transfrom

* Updated `testValidation_Fail()` transform test

It now checks for the validation error text thrown from a `transform` closure.

* Improved transform `@Option` tests

Added a test for the defaut error message and renamed object to imply the tests are for `@Options` only

* Added a `CustomParserErrorConvertible` protocol

Opting in an error types to this will prevent automatic error messages from being generated.

* Add associated value to `.unableToParseValue`

Added a `customMessage` (`String?`) associated value to `ParseError.unableToParseValue(…)`. Setting this will by-pass any automatic error generation.

* Convert transform throws into `unableToParseValue`

Errors thrown by the `transform` closure are caught and convered into a `ParserError.unableToParseValue(…)` error. If the thrown error also confirms to `CustomParserErrorConvertible` the `customMessage` associated value is of `.unableToParseValue` is set, otherwise it is nil.

Implemented for `@Option` and `@Argument`.

* Added transform tests

Added `ParsableArguments` and `ParsableCommand` tests for single values and arrays.

Testing for correctly parsing and transforming values. Throwing a custom error and improved default error messages.

* Add default value to `unableToParseValue`

`customMessage` now has a default value of `nil`

* Removed `CustomParserErrorConvertible`

Updated `unableToParseValue` to take an optional `Error` assocated value. If this error is not nil `unableToParseValueMessage(…)` makes best-efforts to create a custom error message.

* Updated tests to new error mssages format

* Reverted public access of ValidationError.message

* Improved coding standards

`catch` brases on the same line and 2 space indents.

* Simplified `unableToParseValueMessage(…)` logic

Append custom error message to all “unableToParse” errors if it is not nil

* Added a “Handling Transform Errors” section

* Improved switch/case statements

* Added docs link to Handling Transform Errors

* Update Documentation/05 Validation and Errors.md

Co-Authored-By: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Update Documentation/05 Validation and Errors.md

Co-Authored-By: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Update Documentation/05 Validation and Errors.md

Co-Authored-By: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Update Documentation/05 Validation and Errors.md

Co-Authored-By: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Fixed comment and docs typos

Co-Authored-By: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Fixed minor code formatting etc.

Co-Authored-By: Nate Cook <natecook@apple.com>

* Improved Documentation

Reduced the code used in the transform closure. Also fixed typing and formatting.

* Added error examples

* Doc edits via code review

Co-Authored-By: Nate Cook <natecook@apple.com>

* Converted TransformEndToEndTests.swift to

2-space indentation

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
Co-authored-by: Nate Cook <natecook@apple.com>
2020-04-21 09:31:57 -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
Guillaume Lessard 53a00f51fb Pass information to ParserError in order to show user a better error message (#52)
* revert some of the cleanup from 6b3a0a1

* Pass information to ParserError to diagnose mutually exclusive flags
2020-03-04 11:24:27 -06:00
Klaas Pieter Annema fd5b49c8dc Provide suggestions for unknown options (#10)
* Add String#editDistance(to:)

Uses levenshtein distance to determine how much two strings differ.

See: https://en.wikipedia.org/wiki/Levenshtein_distance

* Simplify unknownOptionMessage

The logic of Name#synopsisString was repeated in unknownOptionMessage.
This change sit so that unknownOptionMessage defer to Name's
implementation instead.

* Provide suggestions for unknown options
2020-03-02 11:21:03 -06:00
Wildchild9 6f58e68a6a Fixed Various Grammar & Spelling Mistakes (#17)
* Corrected grammatical and spelling errors in files in Documentation.

* Correct various spelling, grammar, and formatting mistakes in code documentation.
2020-03-02 10:54:10 -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