apple / apple/swift-argument-parser
Ability for an input to work as both a @Flag and an @Option.
- Dominant language
- Swift
- Stars
- 3.8k
- Forks
- 411
- Avg merge
- 7d 14h
- Merged PRs (30d)
- 15
Description
I'd like the ability for an input to be able to work as both a Flag and an Option in my CLI. For example, I would want both of the following examples to be possible:
```bash
$ example --build
# and
$ example --build Foo Bar
```
The first case would parse to `build: []` and the second case to `build: ["Foo", "Bar"]`. I tried making this work in two ways. First I tried making an `@Option` with a type of `[String]? = nil` and using a custom transform closure. My assumption was that passing `--build` without a value would pass an empty string to the transform closure. Instead, the CLI emits the following error when running:
```
Error: Missing value for '--build '
```
The second strategy I tried was creating two separate fields, making one a `@Flag` and one an `@Option`, and giving them the same name. This time, I got an assertion failure:
```
ArgumentParser/ParsableArguments.swift:243: Fatal error: Validation failed for `Example`:
- Multiple (2) `Option` or `Flag` arguments are named "--build".
```
The first strategy—the one that uses only a single `@Option` field—seems the most ergonomic to me. I could also picture using a new initializer parameter to `@Option` e.g. `allowsMissingValue: Bool`, etc. My apologies if this is already possible and I just didn't dig hard enough.
Contributor guide
Research direction
Start by reproducing the two `example` command invocations and inspect the validation reported at `ArgumentParser/ParsableArguments.swift:243`. The behavior is done when `--build` parses as an empty list and `--build Foo Bar` parses as the two supplied values, without the current missing-value or duplicate-name failures.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100