Commit Graph
18 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
Nate Cook a8b48bc389 Add two new ArgumentArrayParsingStrategy options (#496)
This adds two new parsing options for argument arrays, and renames
`.unconditionalRemaining` to `.captureForPassthrough`.

- `.allUnrecognized` collects all the inputs that weren't used during
parsing. This essentially suppresses all "unrecognized flag/option"
and "unexpected argument" errors, and makes those extra inputs
available to the client.
- `.postTerminator` collects all inputs that follow the `--`
terminator, before trying to parse any other positional arguments.
This is a non-standard, but sometimes useful parsing strategy.
2022-10-07 16:18:52 -05: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 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
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
Adam 4cdcc1781d ArgumentHelp.Visibility levels API (#390) 2022-01-21 09:33:25 -06: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
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
Nate Cook 992a7451d5 Convert all public enums to structs (#299) 2021-05-15 10:30:48 -05: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 2a4664a40b Add support for joined short options (#240)
This supports joined arguments, like '-Ddebug' or '-v4'. Joined arguments
need to be explicitly declared as `.customShort("D", allowingJoined: true)`.
2021-01-16 00:18:23 -06: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 c87d0d0dde Remove ExpressibleByArgument conformance from Optional (#173)
* Remove ExpressibleByArgument conformance for Optional

It turns out that the conditional conformance for Optional was a bad idea, and
it should be handled more like Array, with specific initializers for the Optional
case. Primarily, this is because providing a default value for an optional property
doesn't make sense -- the default is already nil, and a non-nil default means that
the property will never be nil and therefore shouldn't be optional.

* Drop duplicated argument definitions

d1 and d6 are duplicates of c2 and c, respectively.

* Correctly mark optional args/options as optional

* Correct documentation for Option/Argument

* Use the correct parameter name in the documentation
2020-06-01 21:47:22 -05:00
Nate CookandKyle Macomber e870503c45 Add an EnumerableFlag protocol (#65)
* Add EnumerableFlag protocol

This addresses the need for providing name specifications for enum
flags, since property wrappers can't be used for enum cases.

* Incorporate updated flag-handling logic

* Include test of multiple names for enumerable flags

* Add documentation for EnumerableFlag protocol

* Add `static func help(for:)` to EnumerableFlag

* Update docs to cover `EnumerableFlag`

* Update default value documentation

* Revise the Flag type docs

* Update Documentation/02 Arguments, Options, and Flags.md

Co-authored-by: Kyle Macomber <kmacomber@apple.com>

Co-authored-by: Kyle Macomber <kmacomber@apple.com>
2020-05-10 17:08:44 -05: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
Nate Cook f6ac7b8118 Initial import of ArgumentParser 2020-02-27 15:45:22 -06:00