Commit Graph
10 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
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
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 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
Rauhul Varma df75d7009e Fix dump help crash (#387)
- Fixes #375
- Fixes a bug with the experimental dump help generator which would
  occur when a ParsableCommand has non-Argument properties.
2022-01-10 15:58:49 -06:00
Yi Xie afe0b9588e Fix compile failure on iOS for Mac Catalyst support (#356)
* Fix compile failure on iOS

* Add sortedKeys API check for tvOS and watchOS
2021-09-13 16:24:33 -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