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
Nate Cook f94e4b9951 Disfavor deprecated property wrapper inits (#522)
This change is to make sure that the non-optional generic parameter
type is chosen for @Argument and @Option properties that provide a
default value. Previously, an optional type might be chosen depending
on how the `help` parameter was spelled, due to strange overload resolution.
In particular, using the `.init` caused selection of the deprecated
optional overload:

    // Unexpected:
    // Infers `Value` as `Optional<AbsolutePath>`
    @Argument(help: .init("The path"))
    var path = AbsolutePath("/")

    // Expected:
    // Infers `Value` as `AbsolutePath`
    @Argument(help: "The path")
    var path = AbsolutePath("/")

This addresses the issue by marking the deprecated overloads as
disfavored. rdar://102383455
2022-11-16 09:30:27 -06:00
Adam McNight 2b352c08c2 Deprecate Option initializer and add a new one with parameters in order (#391) 2022-01-25 10:43:44 -06:00
Mike Lewis 8dfa177102 Allow required array arguments, options, and flags (#196)
* Allow required arrays in `Option`s

Un-deprecates (but changes the semantics of) an initializer for an array value type without a default, forcing the user to specify at least one value from the command line.

* Allow required arrays in `Argument`s

Extends the parent commit to arguments, still un-deprecating and changing the semantics of the previous initializer to force users to provide a value on the command line.

* Allow required arrays in `Flag`s

Extends the previous commits to flags, still un-deprecating and changing the semantics of the previous initializer to force users to provide a value on the command line.

* Add default-value section to documentation
2020-08-14 15:00:07 -05:00
Nate Cook 35ceb59427 Nate/continue default initialization (#193)
* Use type inference for flags / options

* Use default value syntax for arg/option arrays

* Allow a default for flag arrays

* Fix some whitespace

* Allow arguments validations to warn instead of fail

* Move nonsense flag warning to argument validation

* Update guides/readme with default literal syntax
2020-06-23 10:43:50 -05:00
Mike Lewis c0f9a5feee Allow normal Swift default property initialization syntax (#170)
* Allow normal Swift default property initialization syntax

This change allows the normal `var foo = "blah"` default initialization 
syntax for `Option`s, as a parallel initialization method as using the 
`default` parameter.

* Add simple tests for default property initialization

* Centralize some constructor logic into a private `init`

Preparing for another no-initial value `init` to be added and the existing one with a `default` parameter to be deprecated

* Deprecate previous `Option.init` with `default` parameter

It's replaced with an `init` containing no default value parameter, which will be used when the user does not provide any value.

Also add a (most likely unnecessary) sanity test to make sure initializations without a default value still work.
Also copy out documentation to allow clean removal of the older `init` when the time comes.

* Document added test cases

* Correct punctuation

* Extend standard default initialization syntax to `Option`s with `transform` parameters

* Actually replace previous `init` with private version

This mirrors the non-transform variants, and should have been included in the previous commits

* Clean up usage of default parameter values

Private `init` doesn't need defaults, and the deprecated public ones shouldn't have it to avoid confusion with the new methods

* Clean up documentation

Treat new initializers as the originally intended way to allow for clean removal of the deprecated methods
Also add some additional documentation to the deprecated methods to help point users in the right direction

* Extend standard default initialization to `Argument`s

* Extend standard default initialization to `Flag`s

* Default flags with inversions to nil/required

* Extend standard default initialization to no-inversion boolean `Flags`

Prints a warning when that default value is `true` instead of `false`, as the flag value will be pinned regardless of user input

* Eliminate deprecation spam from default value initialization

All examples and unit tests have been transitioned to the new syntax, with the exception of `SourceCompatEndToEndTests`, which should not have the old style removed until it is no longer valid source.

* Add source compatibility tests for new default syntax and associated changes

* Update top-level documentation
2020-06-22 23:12:51 -05:00
John Mueller f07d2fb107 Enable default for option and argument arrays (#186)
* Enable default for option and argument arrays

* Moved array reset to ParsedValues
2020-06-22 13:53:31 -05:00
Nate Cook 4ac7d890e5 Fix tests so that they build with Linux test discovery 2020-03-17 11:15:26 -05:00
griffin-stewieandNate Cook 747d6e5748 Add support for default Argument value (#75)
* Add support for default Argument value

* Add test code

* Fix documentation

Now `argument` have default parameter as well.

* Add missing comma

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

* Add missing colon

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

* Add UsageGenerationTests

Co-authored-by: Nate Cook <natecook@apple.com>
2020-03-12 00:28:33 -05:00
Elliott Williams 34300696f5 Prefix test target names with "ArgumentParser" (#74)
* Prefix testing and test helper targets with ArgumentParser

* Replace SAP with ArgumentParser in imports and CMakeLists
2020-03-10 12:44:07 -05:00