apple / apple/swift-argument-parser
Support option in children and parent with the same name
- Dominant language
- Swift
- Stars
- 3.8k
- Forks
- 411
- Avg merge
- 7d 14h
- Merged PRs (30d)
- 15
Description
My use case is an example from `git`.
Potentially linked discussion: https://forums.swift.org/t/supporting-subcommands-with-same-option-name-as-reused-parent-command
Let’s consider `git commit` specifically which has the `-C` option to specify a commit from which to reuse the log message and other.
`git` by itself also has a `-C` option, to change the repository path.
Thus, we can legally call git with:
```
git -C my_path commit -C my_commit
```
If I’m not mistaken, this setup is currently impossible to describe using `ArgumentParser`.
Ideally, I think this:
```swift
struct GlobalOptions : ParsableArguments {
@Option(name: .customShort("C"))
var path: String
}
struct Git : ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "…",
subcommands: [
GitCommit.self
]
)
@OptionGroup()
var globalOptions: GlobalOptions
}
struct GitCommit : ParsableCommand {
static var configuration = CommandConfiguration(abstract: "…")
@OptionGroup()
var globalOptions: GlobalOptions
@Option(name: .customShort("C"))
var commitFrom: String
func run() throws {
print(globalOptions.path)
print(commitFrom)
}
}
```
When called like this:
```
git -C path commit -C commit
```
Should print:
```
path
commit
```
Currently it says there is a missing `-C` argument.
Contributor guide
Research direction
Start by reproducing the nested ParsableCommand example with GlobalOptions, Git, and GitCommit, using the repeated custom short option `-C`. Trace how OptionGroup values are parsed across the parent and child commands; done means `git -C path commit -C commit` accepts both values and prints them separately.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100