35 Commits

Author SHA1 Message Date
Bassam (Sam) Khouri a78d98b92d Augment Option to support default as flag option (#830)
In some use cases, there is a need to have an option argument behave
like a flag.

This change introduced 4 new intialiazers to `Option` that accept a
`defaultAsFlag` value.

With the following usage:

```swift
struct Example: ParsableCommand {
    @Option(defaultAsFlag: "default", help: "Set output format.")
    var format: String?
    func run() {
        print("Format: \(format ?? "none")")
    }
}
```

The `defaultAsFlag` parameter creates a hybrid that supports both patterns:
  - **Flag behavior**: `--format` (sets format to "default")
  - **Option behavior**: `--format json` (sets format to "json")
  - **No usage**: format remains `nil`

As a user of the command line tool, the `--help` output clearly distinguishes
between the the hybrid and regular usages.

```
OPTIONS:
  --format [<format>]     Set output format. (default as flag: default)
````

Note the `(default as flag: ...)` text instead of regular `(default: ...)`,
and the optional value syntax `[<value>]` instead of required `<value>`.

Fixes: #829
2026-03-23 14:47:26 -05:00
Nate Cook 60bdd07373 Eliminate two deprecation warnings in Examples/math (#879) 2026-03-20 20:58:05 +00:00
Ross Goldberg 04695ec544 Escape single quotes in shellCommand completion for fish. (#811)
Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-09-19 12:39:07 -05:00
Konstantin Krokhin 83ad8da9d0 docs: update help text for readability in Repeat example and README (#787)
* docs: update help text for readability in Repeat example and README

* Update help text snapshots for repeat command to fix test failures
2025-08-18 14:29:17 -05:00
Ross Goldberg 151e5ec606 Replace the cursor index within the completing word parameter of custom-completion closures with a String completion prefix parameter (#770)
* Replace the cursor index within the completing word parameter of custom-completion closures with a `String` `completionPrefix` parameter.

* Simplify String.prefix(…) call in CommandParser.swift.
2025-05-19 00:32:23 -05:00
Ross Goldberg 42585ad612 Add 2 index arguments to custom shell completion calls (#763)
They indicate to the Swift custom completion function:

1. the word for which completions are being requested.
2. the location of the cursor within that word.
2025-05-07 16:32:11 -05:00
Nate Cook 134451f572 Fix up license headers and enable check (#746) 2025-02-24 06:53:19 -08:00
Nate Cook 5bb54b937c Specify availability for remainder of platforms (#741) 2025-02-16 21:08:42 -06:00
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
Bri Peticca 83c5134a24 Add ability to provide descriptions for CaseEnumerable @Option values (#647)
Since `ExpressibleByArgument` already maintains a list of
enumerable values for an argument, we can extend this to serve
as an ordered list for a new dictionary property that maps the
value name to its description, if applicable. The new property
is a static variable on `ExpressibleByArgument` labelled
`allValueDescriptions`.

If the description string for a value is the same as the value
string, it's assumed that the description is not implemented.

The new value strings are used in the help screen, in the 
dump-help JSON output, and in the generated manual.
2024-09-30 14:17:48 -05:00
Kotaro Suto 1d191b00ea Prefer if-let shorthand in CountLines.swift (#638) 2024-05-09 10:33:30 -07:00
Danny Canter 2b962934d1 Add aliases support for sub commands (#627)
This adds support for aliases for subcommands via a new parameter to
CommandConfigurations constructors. The aliases are passed as an array
of strings, where the default is just an empty array that signifies there
are no aliases. The aliases are supported regardless of if a different
commandName is chosen or not. This also updates how subcommands show up
in the help text. Any aliases are now displayed to the right of the original
command.

In addition to the functionality itself, this change:

1. Updates some of the EndToEnd parsing tests to make sure they function
while using aliases.
2. Sprinkles mentions where I saw fit in the documentation.
3. Updates the Math example to have aliases for `math stats average`
(`math stats avg`), and `math multiply` (`math mul`).

`math`'s help text now looks like the below:

```
~ math --help
OVERVIEW: A utility for performing maths.

USAGE: math <subcommand>

OPTIONS:
  --version               Show the version.
  -h, --help              Show help information.

SUBCOMMANDS:
  add (default)           Print the sum of the values.
  multiply, mul           Print the product of the values.
  stats                   Calculate descriptive statistics.

  See 'math help <subcommand>' for detailed help.

~ math stats --help
OVERVIEW: Calculate descriptive statistics.

USAGE: math stats <subcommand>

OPTIONS:
  --version               Show the version.
  -h, --help              Show help information.

SUBCOMMANDS:
  average, avg            Print the average of the values.
  stdev                   Print the standard deviation of the values.
  quantiles               Print the quantiles of the values (TBD).

  See 'math help stats <subcommand>' for detailed help.
```

and use of the aliases:

```
~ math mul 10 10
100

~ math stats avg 10 20
15.0
```

This change does NOT add any updates to the shell completion logic for
this feature.

Fixes #248
2024-04-30 18:20:03 -05:00
Cœur 1c8215f18a Prefer let for private configurations (#617) 2024-02-28 11:29:01 -06:00
Nate Cook 1ed0ac0925 Switch count-lines to macOS 12 only (#576)
CI is allowing a `guard #available(macOS 12)` in the tests to pass,
but then reporting macOS version 10.16 both in ProcessInfo and in
the guard in the actual `CountLines.run()` method, which results
in an inconsistency. Switching the minimum platform for 
`count-lines` until we can sort out the configuration issue.
2023-06-28 12:28:39 -05:00
Nate Cook 9f103e4e68 Include more platforms in availability (#539) 2023-01-09 11:11:22 -06:00
Nate Cook 0bac2cc0f1 Allow default nil values for optional properties (#480)
This adds underscored initializers that let library users add `= nil` to
declarations of optional `@Option` and `@Argument` properties. Previously,
default values have been available for properties of non-optional types
only.

These new initializers use `_OptionalNilComparisonType` as the wrapped
value parameter, so only a `nil` literal is acceptable in the default
value position. This avoids the problem of declaring an optional property
with a non-`nil` default, which ends up negating the purpose of an optional.
2022-08-30 22:23:15 -05:00
Kth 18b0039973 Fix Repeat's endless printing (#437)
Co-authored-by: Nate Cook <natecook@apple.com>
2022-04-02 15:13:09 -05:00
Saleem Abdulrasool b054991507 build: complete the changes from #423 (#425)
This adds `-parse-as-library` to the example targets which use `@main`
resulting in a compile failure as `@main` is not processed unless the
target is marked as library.

Authored-by: Saleem Abdulrasool <compnerd@compnerd.org>
2022-03-16 07:49:48 -05:00
Nate Cook 777930b99d Remove platform requirement from Package.swift (#427)
Adding the platform requirement is a source breaking change; this moves
the requirement down to the `async` symbols instead.
2022-03-16 07:30:46 -05:00
Saleem Abdulrasool 7dd164a685 build: repair the build after #404 (#423)
This fixes the build with CMake after #404.
2022-03-15 16:02:18 -05:00
Nate Cook 055e0eaf8c Fix broken links/incorrect variance calculation (#422) 2022-03-15 12:05:09 -05:00
Nate Cook 1141ed1e1b Support an async entry point for commands (#404)
Adds a new `AsyncParsableCommand` protocol, which provides a
`static func main() async` entry point and can call through to the root
command's or a subcommand's asynchronous `run()` method. For this
asynchronous execution, the root command must conform to `AsyncParsableCommand`,
but its subcommands can be a mix of asynchronous and synchronous commands.

Due to an issue in Swift 5.5, you can only use `@main` on an
`AsyncParsableCommand` root command starting in Swift 5.6.
This change also includes a workaround for clients that are using Swift 5.5.
Declare a separate type that conforms to `AsyncMainProtocol` and add the `@main`
attribute to that type.

```
@main enum Main: AsyncMain {
    typealias Command = <#command#>
}
```
2022-03-14 18:14:09 -05:00
Nate Cook 605a2330c5 Suppress hidden arguments from completion scripts (#271) 2021-02-11 10:41:46 -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
Adam Sharp a4e4ae4e12 Update documentation and examples to use natural property wrapper syntax (#210)
Since https://github.com/apple/swift-argument-parser/pull/207, property
wrapper usage with no arguments can now use the more natural syntax
without the `()`. Updates documentation and example code accordingly.
2020-07-28 11:34:12 -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
Brad Larson 2c708bd252 Fixing math example on Linux. (#174) 2020-06-02 19:13:37 -05:00
Nate Cook aad1ac085b Make ParsableCommand.run() a mutating method (#163)
Co-authored-by: Erik Little <nuclear.ace@gmail.com>
2020-06-02 14:07:16 -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
Saleem Abdulrasool 215a8bfab7 CMake adjustments (#69)
* build: update cmake for TestHelpers rename

* build: make CMake build on macOS work

Unfortunately, XCTest importing is not working for some reason
currently.  However, this allows you to build on Darwin as follows:

```
cmake -G Ninja -D CMAKE_BUILD_TYPE=Release -D BUILD_TESTING=NO -S . -B out
ninja -C out
```

Windows and Linux builds remain functional.

* build: refactor Examples build with CMake

Although this goes contrary to best practices for CMake, this helps keep
the source tree a bit leaner for the Examples.
2020-03-09 10:26:49 -05:00
Nate Cook 9ebf401d27 Add support for exiting without printing an error. (#51)
This adds an `ExitCode` error type that stores an exit code,
and updates the exit machinery to (1) use `ExitCode` as the source
for exit codes, and (2) silently exit when the provided error
is an `ExitCode` instance.
2020-03-04 13:10:28 -06:00
Nate Cook a1d6e00c61 Fix usage message for subcommand validation errors
When a validation error is thrown from a subcommand, we aren't correctly
including the command stack, so only the root command's usage is shown.
This bundles the validation error together with the command stack so the
error message has all the context it needs.
2020-03-02 17:15:31 -06:00
Saleem Abdulrasool b9eb417354 build: add a CMake based build for Windows (#44)
Since swift-package-manager doesn't work on Windows yet, add a CMake
based build system.
2020-03-02 16:26:31 -06:00
Nate Cook f6ac7b8118 Initial import of ArgumentParser 2020-02-27 15:45:22 -06:00