-
Notifications
You must be signed in to change notification settings - Fork 350
doc: add detailed doc for enum descriptions. #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
louisunlimited
wants to merge
3
commits into
apple:main
Choose a base branch
from
louisunlimited:louis/additional-help-doc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+116
−9
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,6 +150,150 @@ OPTIONS: | |
|
||
For an ``ExpressibleByArgument`` and `CaseIterable` type with many cases, you may still want to implement ``ExpressibleByArgument/allValueStrings`` to avoid an overly long list of values appearing in the help screen. For these types it is recommended to include the most common possible values. | ||
|
||
## Providing Descriptions for Individual Enum Values | ||
|
||
When your argument or option uses an enum type, you can provide detailed descriptions for each enum value that will appear in the help screen. This is especially useful when the enum cases represent complex concepts that benefit from explanation. | ||
|
||
### Basic Enum Value Descriptions | ||
|
||
To provide descriptions for individual enum values, implement a custom `defaultValueDescription` property for each case. The ArgumentParser will automatically detect when descriptions differ from the enum's string representation and display them in an enumerated format. | ||
|
||
```swift | ||
enum LogLevel: String, CaseIterable, ExpressibleByArgument { | ||
case error | ||
case warning | ||
case info | ||
case debug | ||
|
||
var defaultValueDescription: String { | ||
switch self { | ||
case .error: | ||
return "Show only error messages" | ||
case .warning: | ||
return "Show warnings and errors" | ||
case .info: | ||
return "Show informational messages and above" | ||
case .debug: | ||
return "Show all messages including debug output" | ||
} | ||
} | ||
} | ||
|
||
struct Logger: ParsableCommand { | ||
@Option(help: "Set the logging level") | ||
var level: LogLevel = .info | ||
} | ||
``` | ||
|
||
This produces help output with detailed descriptions for each enum value: | ||
|
||
``` | ||
USAGE: logger [--level <level>] | ||
|
||
OPTIONS: | ||
--level <level> Set the logging level (default: info) | ||
error - Show only error messages | ||
warning - Show warnings and errors | ||
info - Show informational messages and above | ||
debug - Show all messages including debug output | ||
-h, --help Show help information. | ||
``` | ||
|
||
### Array Options with Enum Descriptions | ||
|
||
The same enum value descriptions work seamlessly with array options, allowing users to understand each possible value when multiple selections are allowed: | ||
|
||
```swift | ||
enum OutputFormat: String, CaseIterable, ExpressibleByArgument { | ||
case json | ||
case yaml | ||
case xml | ||
case csv | ||
|
||
var defaultValueDescription: String { | ||
switch self { | ||
case .json: | ||
return "JavaScript Object Notation format" | ||
case .yaml: | ||
return "YAML Ain't Markup Language format" | ||
case .xml: | ||
return "eXtensible Markup Language format" | ||
case .csv: | ||
return "Comma-Separated Values format" | ||
} | ||
} | ||
} | ||
|
||
struct DataExporter: ParsableCommand { | ||
@Option(help: "Output formats to generate") | ||
var formats: [OutputFormat] = [.json] | ||
} | ||
``` | ||
|
||
The help screen displays descriptions for each format option: | ||
|
||
``` | ||
USAGE: data-exporter [--formats <formats> ...] | ||
|
||
OPTIONS: | ||
--formats <formats> Output formats to generate (default: json) | ||
json - JavaScript Object Notation format | ||
yaml - YAML Ain't Markup Language format | ||
xml - eXtensible Markup Language format | ||
csv - Comma-Separated Values format | ||
-h, --help Show help information. | ||
``` | ||
|
||
### Working with Raw Values | ||
|
||
For enums with custom raw values, the descriptions work with the raw value representation: | ||
|
||
```swift | ||
enum CompressionAlgorithm: String, CaseIterable, ExpressibleByArgument { | ||
case gzip = "gzip" | ||
case bzip2 = "bzip2" | ||
case lzma = "lzma" | ||
case none = "none" | ||
|
||
var defaultValueDescription: String { | ||
switch self { | ||
case .gzip: | ||
return "GNU zip compression (good speed, decent compression)" | ||
case .bzip2: | ||
return "Burrows-Wheeler compression (slower, better compression)" | ||
case .lzma: | ||
return "Lempel-Ziv-Markov compression (slowest, best compression)" | ||
case .none: | ||
return "No compression applied" | ||
} | ||
} | ||
} | ||
|
||
struct Archive: ParsableCommand { | ||
@Option(help: "Compression algorithm to use") | ||
var compression: CompressionAlgorithm = .gzip | ||
} | ||
``` | ||
|
||
### Tips for Effective Enum Descriptions | ||
|
||
|
||
1. **Keep descriptions concise but informative** - Aim for one line that clearly explains the option's purpose or behavior. | ||
|
||
2. **Use consistent formatting** - Maintain a consistent style across all your descriptions (e.g., all sentences or all phrases). | ||
|
||
3. **Explain the impact** - Help users understand what choosing each option will do, not just what it is. | ||
|
||
4. **Order logically** - The descriptions appear in the same order as your enum cases, so consider ordering them by frequency of use or logical progression. | ||
|
||
5. **Only provide descriptions when helpful** - If your enum values are self-explanatory, you don't need custom descriptions. The system will only show the detailed format when descriptions differ from the raw values. | ||
|
||
The enum value descriptions feature works automatically with: | ||
- Single enum options (`@Option var value: MyEnum`) | ||
- Optional enum options (`@Option var value: MyEnum?`) | ||
- Array enum options (`@Option var values: [MyEnum]`) | ||
- Enum arguments (`@Argument var value: MyEnum`) | ||
- Default values and value display formatting | ||
|
||
### Controlling Argument Visibility | ||
|
||
You can specify the visibility of any argument, option, or flag. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use just one enum throughout this documentation, I think output format is a good example