14 Commits
Author SHA1 Message Date
Rauhul Varma 10d80282e5 Enable swift-format checking (#711)
Adopts the common swift-mmio and swift-argument-parser format and enables CI checking.

Fixes: #702
2025-02-10 14:58:19 -08:00
Lev Walkin 511a72aea8 Add Sendable conformance (#582)
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.
2023-11-17 10:06:47 -06: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 185b45fa06 Combine HelpCommand and HelpHiddenCommand (#408) 2022-02-12 16:35:31 -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
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
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
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
John Mueller 501bf60536 Display help when no arguments results in error (#140)
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.
2020-05-14 09:08:19 -05:00
1081d08b1d Improve errors messages thrown from a transform closure (#115)
* 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>
2020-04-21 09:31:57 -05:00
Nate Cook 31799bc1b4 Add built-in support for --version flag (#102)
* 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.
2020-03-30 12:36:21 -05:00
Guillaume Lessard 53a00f51fb Pass information to ParserError in order to show user a better error message (#52)
* revert some of the cleanup from 6b3a0a1

* Pass information to ParserError to diagnose mutually exclusive flags
2020-03-04 11:24:27 -06: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