This change adds conditional `Sendable` conformance to all
property wrapper types when their `Value` is `Sendable`, enabling
commands to be used in concurrent contexts. Some notes on
the implementation:
* Fix flag exclusivity issues
This derives the `hasUpdated` check from the parsed values data type,
rather than storing it in the closure (which breaks sendability) or
passing it through the closure invocation (which wasn't finished
enough to actually work).
* Mark all `transform` methods as `@Sendable`
This allows for a stronger, compiler-supported guarantee of
sendability when a compound `ParsableArguments` or `ParsableCommand`
type is marked `Sendable`. Most transformations shouldn't be a
problem, since the general case is that these are pure string ->
value transformations.
In cases where making such a transformation sendable is impossible,
an author can always change the property to be just a string and
perform the transformation within the context of the command's
execution, in either the `run()` or `validate()` methods.
* Add `@preconcurrency` to Sendable closure APIs
This adds the `@preconcurrency` attribute to all public APIs that
have changed to take a `@Sendable` closure. This will ease the
migration path for sendable adoption for ArgumentParser users, since
a warning will only appear for using these APIs (like the `transform`
parameter in an @Option or @Argument) once they've turned on strict
concurrency checking.
I'm also backing out changes that avoided those warnings in the tests
and examples, since in most cases those warnings are spurious;
unapplied functions don't capture state. See
https://forums.swift.org/t/pitch-inferring-sendable-for-methods-and-key-path-literals/68011
for more on this and hopefully an upcoming fix for these issues.
* Raise minimum Swift version to 5.7
In order to provide `@preconcurrency` support, the package needs to
have a minimum Swift requirement of 5.7. This makes that change and
updates the README to indicate this for the next version.
- 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.
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`
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>
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.
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.
* 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>
* 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.
* Corrected grammatical and spelling errors in files in Documentation.
* Correct various spelling, grammar, and formatting mistakes in code documentation.