Commit Graph
91 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
matsuji 99e2e97fd9 Improve fish completion (#376, #534) (#535)
A generated fish script didn't complete an option after an argument.

The cause is the generated fish script doesn't accept an input text which already has arguments.
For example, when the input is `repeat -`, the script can complete `--count`, but when the text is `repeat foo -`, the script cannot complete `repeat foo --count`, because the input text already has the argument "foo".

To fix the issue, `FishCompletionsGenerator` got a capability that can accept a text which has arguments.
2023-01-09 10:55:41 -06:00
Rauhul Varma bc29743b72 Update tests with easier to read diff output (#529)
- Renames AssertEqualStringsIgnoringTrailingWhitespace to
  AssertEqualStrings and updates the implementation to require matching
  trailing whitespace.
- Updates AssertEqualStrings to include much easier to read diff output
  when CollectionDifference is available. This should add developers
  when tests fail by providing more clear errors.
2022-11-21 22:29:52 -08:00
Rauhul Varma e48e467b90 Fix deprecation warnings in unit tests (#526)
- Fixes deprecation warnings in unit tests by marking tests which cover
  deprecated functionality as @available(*, deprecated) themselves.
2022-11-21 22:24:42 -08:00
Rob RussoandRob Russo 8f9fa6fc9f Reword Enums to Remove "Master" (#521)
Co-authored-by: Rob Russo <robert_russo@apple.com>
2022-11-15 12:30:40 -06:00
Nate Cook b80fb05f45 Add API for titling an option group (#492)
This change lets you provide a title for option groups, which is used
when generating the help screen. Titled option groups, when they exist,
are placed between the ARGUMENTS and OPTIONS section of the help.
Multiple option groups with the same title are coalesced into a single
group.

For example, this command declaration:

    struct Extras: ParsableArguments {
      @Flag(help: "Print extra output while processing.")
      var verbose: Bool = false

      @Flag(help: "Include details no one asked for.")
      var oversharing: Bool = false
    }

    @main
    struct Example: ParsableCommand {
      @OptionGroup(title: "Extras")
      var extras: Extras

      @Argument var name: String?
      @Option var title: String?
    }

yields this help screen:

    USAGE: example [--verbose] [--oversharing] [<name>] [--title <title>]

    ARGUMENTS:
      <name>

    EXTRAS:
      --verbose               Print extra output while processing.
      --oversharing           Include details no one asked for.

    OPTIONS:
      --title <title>
      -h, --help              Show help information.
2022-09-26 18:47:40 -05: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 774de9c409 Fix help display for non-String RawRepresentables (#494)
RawRepresentable types that have a non-String raw value are having
values displayed in the help screen by converting the RawRep value
into a string. However, these values are by default parsed by their
raw value, so we should use that for display instead.

This is accomplished by adding a defaultValueDescription
implementation for all ExpressibleByArgument-conforming RawValue
types, and then basing the allValues implementation on that.
This generalizes the existing overloads for String-based RawRep types,
while also allowing users who customize their ExpressibleByArgument
implementation to provide the correct help value for clients.
2022-09-20 14:25:03 -05:00
Nate Cook 7506042c65 Fix default display in help for EnumerableFlag and other types (#486)
In the help for flags like --prefix/--no-prefix, use the name of the
default flag instead of true/false. When EnumerableFlag types
have separate help strings, show the correct default flag name
on the default flag in the help.
2022-09-14 11:16:15 -05:00
Rauhul Varma 607021b737 Unify @Argument and @Option initialization paths (#477)
- Fixes #466.
- Adds initializers to ArgumentDefinition generic over a Container type.
  The Container type must conform to a new internal protocol
  ArgumentDefinitionContainer which describes functionality like default
  set of help options for the argument defined by the property wrapper,
  etc.
- Adds overloads for Optional @Arguments and @Options with default
  values which emit deprecation warning to guide users towards using the
  non-Optional versions.
2022-09-10 18:40:31 -05:00
Nate Cook 09106ba1ca Add a validation message for an invalid decoder (#487)
If a ParsableArguments type doesn't implement init(from:) correctly,
it isn't decodable by the parser. This improves the validation
failure message for such types.
2022-09-09 15:12:31 -05:00
Ian 67fa3c00cd Fix the defaultValueDescription for string enums (#476)
defaultValueDescription is always the enum case name, but that's not a
valid argument when the enum value differs from the case name. Extend
ExpressibleByArgument to use rawValue for defaultValueDescription for
string enums.
2022-08-26 16:49:30 -07:00
Rauhul Varma f1c6afd175 Fixup help options for built-in flags (#474)
- Fixes a bug where built-in flags such as --help and --version were not
  properly marked with isOptional which resulted in them appearing in
  generated content (such as completion scripts and manuals) as required
  arguments.
2022-08-26 00:13:12 -07:00
Luciano Almeida 78213f3192 Improving edit distance string extension (#446) 2022-06-04 22:26:16 -05:00
konomae 68b94a4c73 List valid options in error messages for enum array argument (#445)
When an array of argument values 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 #401.
2022-05-17 11:36:30 -05:00
Rauhul Varma 4f63cee952 Hide hidden subcommands from completions (#443) 2022-04-23 18:56:15 -07:00
Kth dd6efd05cb Fix error message for @Option array without values (#435) 2022-04-03 23:58:31 -05:00
Nate Cook d7a5a794c5 Make @OptionGroup(visibility:) a public API (#419) 2022-03-14 23:51:10 -05:00
Nate Cook c959b3a5d2 Hide optional flags when a command has too many options (#416)
The usage string feature that only shows positional args and required
options/flags was incorrectly allowing through flags with type
`Bool?`.
2022-03-07 18:40:34 -06: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 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 e3673688cf Add abillity to generate hidden help (#410)
- Updates helpMessage(columns:) and helpMessage(for:columns:) with an
  includeHidden argument defaulted to false to allow for clients to
  programmatically generate hidden help.
2022-02-12 16:11:07 -08:00
Rauhul Varma 7fdd562901 Update OptionGroup to use Visibility (#407) 2022-02-12 16:34:05 -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
Adam 4cdcc1781d ArgumentHelp.Visibility levels API (#390) 2022-01-21 09:33:25 -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
Aaron Gyes 7e04f56c1d Rename ...using_command fish function, set $cmd in local scope (#377)
__fish_* should not be used by external projects.
set -l cmd in case user has a global or universal `cmd` defined.
2021-12-10 13:31:20 -06:00
Craig Siemens d7f63849fb Updated the zsh completions to escape the command name for a custom completion. (#364) 2021-12-07 10:47:43 -06:00
Nate Cook 9b6827d348 Mark the dump help feature as experimental for now (#350)
* Mark `--dump-help` as experimental
* Mark the dumpHelp method as underscored
2021-09-01 16:42:17 -05: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
Rauhul Varma d4d1879c9d Propogate ExpressibleByArgument conformance (#341)
- Adds an overload of ArgumentDefinition.init with a generic constraint
  on ExpressibleByArgument that propogates the conformance to the
  construction of ArgumentDefinition.Help. This allows the
  allValueStrings of the type conforming to ExpressibleByArgument to
  become the allValues property of the help object.
2021-07-29 13:23:50 -05:00
Nate Cook 685341f629 Use standard path for --dump-help help/completions, remove completions (#339) 2021-07-20 12:59:36 -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
François Lamboley 91fbf3887d Fix default value description for URL (#321) 2021-06-04 09:59:00 -05:00
Nate Cook 959aa19cdf Add test for issue #278 (#312)
It appears this issue was fixed some time in the past; closes #278.
2021-05-17 15:31:49 -05:00
Nate Cook 267f707294 Use custom help flags in completion scripts (#308)
* Standardize the help and version flag generation
* Simplify some help generation code
* Support custom help flags in completion scripts
2021-05-14 10:27:05 -05:00
Miguel A. Perez Ojito ee32b80940 Hide option group with new OptionGroup constructor (#301) 2021-04-24 21:28:10 -05:00
Miguel A. Perez Ojito f314199a3d Exclude supercommands from help (#300)
* Ability to exclude super commands from --help
2021-04-21 12:17:32 -05:00
Nate Cook cdb0e71584 Convert realValue method to function, update docs (#298) 2021-04-15 01:28:48 -05:00
Mike WermuthandMike bafa74a9e4 Fix invalid state error when decoding an unparsed optional value (#290)
Fixes “Internal error. Invalid state while parsing command-line arguments.” that is encountered when an unparsed value is optional.

Root causes:
- `ParsedArgumentsContainer.decodeNil` returns false for optional values because it only does a `!contains(key)` check. This should instead return nil if the value of the element is nil.
- The decoder did not know about unparsed input origins that and would result in unexpected behavior when decoding nil default values.
- The `value` of `Mirror.Child` is defined as `Any` but this is confusing because the value could be `Optional<Any>` which is not equal to `nil` even when the `Optional` case is `.none`.

Co-authored-by: Mike <mike.wermuth@icloud.com>
2021-04-15 01:07:24 -05:00
Mike Wermuth 080372b7cd Hide flag inversion when help is hidden (#289)
Fixes an issue where the inversion of a flag would not be hidden whe the ArgumentHelp shouldDisplay value is false.

Added a unit test to check for this behavior.
2021-03-12 01:07:12 -06:00
Alfredo Delli Bovi 26744de9d8 Fix zsh dashes in command names (#284) 2021-03-06 13:10:43 -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
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
Kenny 267558bb4d Beautify NSError cases when thrown 💄 (#272) 2021-02-15 17:17:13 -06:00
Nate Cook 605a2330c5 Suppress hidden arguments from completion scripts (#271) 2021-02-11 10:41:46 -06:00
Nate Cook 9fe9374a1f Remove deprecated property wrapper initializers (#262) 2021-01-16 00:47:11 -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