Skip to content

Commit 7566a6a

Browse files
committed
Merge branch 'main' into async
2 parents b2975c4 + 9a71866 commit 7566a6a

33 files changed

+343
-154
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/Packages
44
/*.xcodeproj
55
.swiftpm
6+
.vscode
67
.*.sw?

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,18 @@ OPTIONS:
6666
-h, --help Show help for this command.
6767
```
6868

69-
For more information and documentation about all supported options,
70-
see the library's documentation in Xcode.
69+
## Documentation
7170

72-
## Examples
71+
For guides, articles, and API documentation see the
72+
[library's documentation on the Web][docs] or in Xcode.
73+
74+
- [ArgumentParser documentation][docs]
75+
- [Getting Started with ArgumentParser](https://apple.github.io/swift-argument-parser/documentation/argumentparser/gettingstarted)
76+
- [`ParsableCommand` documentation](https://apple.github.io/swift-argument-parser/documentation/argumentparser/parsablecommand)
77+
78+
[docs]: https://apple.github.io/swift-argument-parser/documentation/argumentparser/
79+
80+
#### Examples
7381

7482
This repository includes a few examples of using the library:
7583

@@ -79,8 +87,8 @@ This repository includes a few examples of using the library:
7987

8088
You can also see examples of `ArgumentParser` adoption among Swift project tools:
8189

82-
- [`indexstore-db`](https://github.com/apple/indexstore-db/pull/72) is a simple utility with two commands.
83-
- [`swift-format`](https://github.com/apple/swift-format/pull/154) uses some advanced features, like custom option values and hidden flags.
90+
- [`swift-format`](https://github.com/apple/swift-format/) uses some advanced features, like custom option values and hidden flags.
91+
- [`swift-package-manager`](https://github.com/apple/swift-package-manager/) includes a deep command hierarchy and extensive use of option groups.
8492

8593
## Project Status
8694

Sources/ArgumentParser/Completions/CompletionsGenerator.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Glibc
1515
import Darwin
1616
#elseif canImport(CRT)
1717
import CRT
18+
#elseif canImport(WASILibc)
19+
import WASILibc
1820
#endif
1921

2022
/// A shell for which the parser can generate a completion script.

Sources/ArgumentParser/Completions/FishCompletionsGenerator.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ struct FishCompletionsGenerator {
22
static func generateCompletionScript(_ type: ParsableCommand.Type) -> String {
33
let programName = type._commandName
44
let helper = """
5-
function __fish_\(programName)_using_command
6-
set cmd (commandline -opc)
5+
function _swift_\(programName)_using_command
6+
set -l cmd (commandline -opc)
77
if [ (count $cmd) -eq (count $argv) ]
88
for i in (seq (count $argv))
99
if [ $cmd[$i] != $argv[$i] ]
@@ -14,7 +14,7 @@ struct FishCompletionsGenerator {
1414
end
1515
return 1
1616
end
17-
17+
1818
"""
1919

2020
let completions = generateCompletions(commandChain: [programName], [type])
@@ -37,11 +37,11 @@ struct FishCompletionsGenerator {
3737
}
3838
}
3939

40-
let prefix = "complete -c \(programName) -n '__fish_\(programName)_using_command"
40+
let prefix = "complete -c \(programName) -n '_swift_\(programName)_using_command"
4141
/// We ask each suggestion to produce 2 pieces of information
4242
/// - Parameters
4343
/// - ancestors: a list of "ancestor" which must be present in the current shell buffer for
44-
/// this suggetion to be considered. This could be a combination of (nested)
44+
/// this suggestion to be considered. This could be a combination of (nested)
4545
/// subcommands and flags.
4646
/// - suggestion: text for the actual suggestion
4747
/// - Returns: A completion expression

Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ extension ArgumentDefinition {
185185

186186
case .custom:
187187
// Generate a call back into the command to retrieve a completions list
188-
let commandName = commands.first!._commandName
188+
let commandName = commands.first!._commandName.zshEscapingCommandName()
189189
return "{_custom_completion $_\(commandName)_commandname \(customCompletionCall(commands)) $words}"
190190
}
191191
}

Sources/ArgumentParser/Documentation.docc/ArgumentParser.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,12 @@ the `ArgumentParser` library parses the command-line arguments,
3737
instantiates your command type,
3838
and then either calls your `run()` method or exits with a useful message.
3939

40-
```
41-
$ repeat hello --count 3
42-
hello
43-
hello
44-
hello
45-
$ repeat --help
46-
USAGE: repeat [--count <count>] <phrase>
47-
48-
ARGUMENTS:
49-
<phrase> The phrase to repeat.
50-
51-
OPTIONS:
52-
--count <count> The number of times to repeat 'phrase'.
53-
-h, --help Show help for this command.
54-
$ repeat --count 3
55-
Error: Missing expected argument 'phrase'.
56-
Help: <phrase> The phrase to repeat.
57-
Usage: repeat [--count <count>] <phrase>
58-
See 'repeat --help' for more information.
59-
```
40+
![The output of the Repeat command, declared above.](repeat.png)
41+
42+
#### Additional Resources
43+
44+
- [`ArgumentParser` on GitHub](https://github.com/apple/swift-argument-parser/)
45+
- [`ArgumentParser` on the Swift Forums](https://forums.swift.org/c/related-projects/argumentparser/60)
6046

6147
## Topics
6248

@@ -102,3 +88,4 @@ Usage: repeat [--count <count>] <phrase>
10288
### Advanced Topics
10389

10490
- <doc:ManualParsing>
91+
- <doc:ExperimentalFeatures>

Sources/ArgumentParser/Documentation.docc/Articles/CustomizingHelp.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ OPTIONS:
7676

7777
## Customizing Help for Commands
7878

79-
In addition to configuring the command name and subcommands, as described in [Command and Subcommands](03%20Commands%20and%20Subcommands.md), you can also configure a command's help text by providing an abstract and discussion.
79+
In addition to configuring the command name and subcommands, as described in <doc:CommandsAndSubcommands>, you can also configure a command's help text by providing an abstract and discussion.
8080

8181
```swift
8282
struct Repeat: ParsableCommand {
@@ -185,7 +185,7 @@ OPTIONS:
185185

186186
## Hiding Arguments and Commands
187187

188-
You may want to suppress features under development or experimental flags from the generated help screen. You can hide an argument or a subcommand by passing `shouldDisplay: false` to the property wrapper or `CommandConfiguration` initializers, respectively.
188+
You may want to suppress features under development or experimental flags from the generated help screen. You can hide an argument or a subcommand by passing `visibility: .hidden` to the property wrapper or `CommandConfiguration` initializers, respectively.
189189

190190
`ArgumentHelp` includes a `.hidden` static property that makes it even simpler to hide arguments:
191191

Sources/ArgumentParser/Documentation.docc/Articles/DeclaringArguments.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ struct Example: ParsableCommand {
199199
}
200200
```
201201

202-
Throw an error from the `transform` function to indicate that the user provided an invalid value for that type. See [Handling Transform Errors](./05%20Validation%20and%20Errors.md#handling-transform-errors) for more about customizing `transform` function errors.
202+
Throw an error from the `transform` function to indicate that the user provided an invalid value for that type. See <doc:Validation> for more about customizing `transform` function errors.
203203

204204
## Using flag inversions, enumerations, and counts
205205

@@ -255,7 +255,7 @@ struct Example: ParsableCommand {
255255
}
256256
```
257257

258-
The flag names in this case are drawn from the raw values — for information about customizing the names and help text, see the [`EnumerableFlag` documentation](../Sources/ArgumentParser/Parsable%20Types/EnumerableFlag.swift).
258+
The flag names in this case are drawn from the raw values — for information about customizing the names and help text, see the ``EnumerableFlag`` documentation.
259259

260260
```
261261
% example --in-memory-cache --pink --silver
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Experimental Features
2+
3+
Learn about ArgumentParser's experimental features.
4+
5+
## Overview
6+
7+
Command-line programs built using `ArgumentParser` may include some built-in experimental features, available with the prefix `--experimental`. These features should not be considered stable while still prefixed, as future releases may change their behavior or remove them.
8+
9+
If you have any feedback on experimental features, please [open a GitHub issue][issue].
10+
11+
## List of Experimental Features
12+
13+
| Name | Description | related PRs | Version |
14+
| ------------- | ------------- | ------------- | ------------- |
15+
| `--experimental-dump-help` | Dumps command/argument/help information as JSON | [#310][] [#335][] | 0.5.0 or newer |
16+
17+
[#310]: https://github.com/apple/swift-argument-parser/pull/310
18+
[#335]: https://github.com/apple/swift-argument-parser/pull/335
19+
[issue]: https://github.com/apple/swift-argument-parser/issues/new/choose

Sources/ArgumentParser/Documentation.docc/Articles/ManualParsing.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Manual Parsing and Testing
22

3-
Provide your own array of command-line inputs and work with parsed results by calling alternatives to `main()`.
3+
Provide your own array of command-line inputs or work directly with parsed command-line arguments.
44

55
## Overview
66

7-
For most programs, calling the static `main()` method on the root command type is all that's necessary. That single call parses the command-line arguments to find the correct command from your tree of nested subcommands, instantiates and validates the result, and executes the chosen command. For more control, however, you can perform each of those steps manually.
7+
For most programs, denoting the root command type as `@main` is all that's necessary. As the program's entry point, that type parses the command-line arguments to find the correct command from your tree of nested subcommands, instantiates and validates the result, and executes the chosen command. For more control, however, you can perform each of those steps manually.
88

99
## Parsing Arguments
1010

1111
For simple Swift scripts, and for those who prefer a straight-down-the-left-edge-of-the-screen scripting style, you can define a single `ParsableArguments` type to parse explicitly from the command-line arguments.
1212

13-
Let's implement the `Select` command discussed in [Validation and Errors](05%20Validation%20and%20Errors.md), but using a scripty style instead of the typical command. First, we define the options as a `ParsableArguments` type:
13+
Let's implement the `Select` command discussed in <doc:Validation>, but using a scripty style instead of the typical command. First, we define the options as a `ParsableArguments` type:
1414

1515
```swift
1616
struct SelectOptions: ParsableArguments {
@@ -51,7 +51,7 @@ print(chosen.joined(separator: "\n"))
5151

5252
Manually parsing commands is a little more complex than parsing a simple `ParsableArguments` type. The result of parsing from a tree of subcommands may be of a different type than the root of the tree, so the static `parseAsRoot()` method returns a type-erased `ParsableCommand`.
5353

54-
Let's see how this works by using the `Math` command and subcommands defined in [Commands and Subcommands](03%20Commands%20and%20Subcommands.md). This time, instead of calling `Math.main()`, we'll call `Math.parseAsRoot()`, and switch over the result:
54+
Let's see how this works by using the `Math` command and subcommands defined in [Commands and Subcommands](./CommandsAndSubcommands.md). This time, instead of calling `Math.main()`, we'll call `Math.parseAsRoot()`, and switch over the result:
5555

5656
```swift
5757
do {
Loading

Sources/ArgumentParser/Parsable Properties/Argument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ extension Argument where Value: ExpressibleByArgument {
120120
/// ```
121121
///
122122
/// - Parameters:
123-
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during propery wrapper initialization.
123+
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during property wrapper initialization.
124124
/// - help: Information about how to use this argument.
125125
public init(
126126
wrappedValue: Value,
@@ -481,7 +481,7 @@ extension Argument {
481481
///
482482
/// This method is called to initialize an array `Argument` with no default value such as:
483483
/// ```swift
484-
/// @Argument(tranform: baz)
484+
/// @Argument(transform: baz)
485485
/// var foo: [String]
486486
/// ```
487487
///

Sources/ArgumentParser/Parsable Properties/ArgumentHelp.swift

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
/// Help information for a command-line argument.
1313
public struct ArgumentHelp {
14+
/// Visibility level of an argument's help.
15+
public enum Visibility {
16+
/// Show help for this argument whenever appropriate.
17+
case `default`
18+
19+
/// Only show help for this argument in the extended help screen.
20+
case hidden
21+
22+
/// Never show help for this argument.
23+
case `private`
24+
}
25+
1426
/// A short description of the argument.
1527
public var abstract: String = ""
1628

@@ -24,26 +36,57 @@ public struct ArgumentHelp {
2436
/// flags don't include a value.
2537
public var valueName: String?
2638

39+
/// A visibility level indicating whether this argument should be shown in
40+
/// the extended help display.
41+
public var visibility: Visibility = .default
42+
2743
/// A Boolean value indicating whether this argument should be shown in
2844
/// the extended help display.
29-
public var shouldDisplay: Bool = true
45+
@available(*, deprecated, message: "Use visibility level instead.")
46+
public var shouldDisplay: Bool {
47+
get {
48+
return visibility == .default
49+
}
50+
set {
51+
visibility = newValue ? .default : .hidden
52+
}
53+
}
3054

3155
/// Creates a new help instance.
56+
@available(*, deprecated, message: "Use init(_:discussion:valueName:visibility:) instead.")
3257
public init(
3358
_ abstract: String = "",
3459
discussion: String = "",
3560
valueName: String? = nil,
36-
shouldDisplay: Bool = true)
61+
shouldDisplay: Bool)
3762
{
3863
self.abstract = abstract
3964
self.discussion = discussion
4065
self.valueName = valueName
4166
self.shouldDisplay = shouldDisplay
4267
}
43-
44-
/// A `Help` instance that hides an argument from the extended help display.
68+
69+
/// Creates a new help instance.
70+
public init(
71+
_ abstract: String = "",
72+
discussion: String = "",
73+
valueName: String? = nil,
74+
visibility: Visibility = .default)
75+
{
76+
self.abstract = abstract
77+
self.discussion = discussion
78+
self.valueName = valueName
79+
self.visibility = visibility
80+
}
81+
82+
/// A `Help` instance that shows an argument only in the extended help display.
4583
public static var hidden: ArgumentHelp {
46-
ArgumentHelp(shouldDisplay: false)
84+
ArgumentHelp(visibility: .hidden)
85+
}
86+
87+
/// A `Help` instance that hides an argument from the extended help display.
88+
public static var `private`: ArgumentHelp {
89+
ArgumentHelp(visibility: .private)
4790
}
4891
}
4992

Sources/ArgumentParser/Parsable Properties/Errors.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Glibc
1515
import Darwin
1616
#elseif canImport(CRT)
1717
import CRT
18+
#elseif canImport(WASILibc)
19+
import WASILibc
1820
#endif
1921

2022
#if os(Windows)
@@ -66,6 +68,8 @@ public struct ExitCode: Error, RawRepresentable, Hashable {
6668
/// An exit code that indicates that the user provided invalid input.
6769
#if os(Windows)
6870
public static let validationFailure = ExitCode(ERROR_BAD_ARGUMENTS)
71+
#elseif os(WASI)
72+
public static let validationFailure = ExitCode(EXIT_FAILURE)
6973
#else
7074
public static let validationFailure = ExitCode(EX_USAGE)
7175
#endif

Sources/ArgumentParser/Parsable Properties/Flag.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ extension Flag where Value == Bool {
231231
/// Creates a Boolean property with default value provided by standard Swift default value syntax that reads its value from the presence of a flag.
232232
///
233233
/// - Parameters:
234-
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during propery wrapper initialization.
234+
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during property wrapper initialization.
235235
/// - name: A specification for what names are allowed for this flag.
236236
/// - help: Information about how to use this flag.
237237
public init(
@@ -273,7 +273,7 @@ extension Flag where Value == Bool {
273273
///
274274
/// - Parameters:
275275
/// - name: A specification for what names are allowed for this flag.
276-
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during propery wrapper initialization.
276+
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during property wrapper initialization.
277277
/// - inversion: The method for converting this flag's name into an on/off pair.
278278
/// - exclusivity: The behavior to use when an on/off pair of flags is specified.
279279
/// - help: Information about how to use this flag.
@@ -305,7 +305,7 @@ extension Flag where Value == Bool {
305305
///
306306
/// - Parameters:
307307
/// - name: A specification for what names are allowed for this flag.
308-
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during propery wrapper initialization.
308+
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during property wrapper initialization.
309309
/// - inversion: The method for converting this flag's name into an on/off pair.
310310
/// - exclusivity: The behavior to use when an on/off pair of flags is specified.
311311
/// - help: Information about how to use this flag.
@@ -393,7 +393,7 @@ extension Flag where Value: EnumerableFlag {
393393
/// ```
394394
///
395395
/// - Parameters:
396-
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during propery wrapper initialization.
396+
/// - wrappedValue: A default value to use for this property, provided implicitly by the compiler during property wrapper initialization.
397397
/// - exclusivity: The behavior to use when multiple flags are specified.
398398
/// - help: Information about how to use this flag.
399399
public init(

0 commit comments

Comments
 (0)