apple / apple/swift-argument-parser

Short name option '-h' (conflicting with built-in help) is allowed, with strange results.

Open
#778 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
3.8k
Forks
411
Avg merge
7d 13h
Merged PRs (30d)
17

Description

### Checklist
- [ x] If possible, I've reproduced the issue using the `main` branch of this package
- [ x] I've searched for [existing GitHub issues](https://github.com/apple/swift-argument-parser/issues)

### Steps to Reproduce

Add the `horse` option to the Repeat example provided with swift-argument-parser:

```swift
@main
struct Repeat: ParsableCommand {
@Option(name: .shortAndLong, help: "The number of times to repeat 'phrase'.")
var count: Int? = nil

@Option(name: .shortAndLong, help: "The name of the talking horse.")
var horse: String

@Flag(name: .shortAndLong, help: "Include a counter with each repetition.")
var includeCounter = false

@Argument(help: "The phrase to repeat.")
var phrase: String

mutating func run() throws {

let repeatCount = count ?? 2

for i in 1...repeatCount {
if includeCounter {
print("\(i): \(horse) says: \"\(phrase)\"")
} else {
print(phrase)
}
}
}
}
```
### Actual behavior

Help shows conflicting descriptions for "-h":

```
USAGE: repeat [--count ] [--include-counter] --horse

ARGUMENTS:
The phrase to repeat.

OPTIONS:
-c, --count The number of times to repeat 'phrase'.
-i, --include-counter Include a counter with each repetition.
-h, --horse The name of the speaking horse.
-h, --help Show help information.
```
Works sometimes:
```
release> ./repeat --horse Secretariat "I trained hard and won." -i
1: Secretariat says: "I trained hard and won."
2: Secretariat says: "I trained hard and won."
```
What does "-h" mean?
```
release> ./repeat -h
Error: Missing value for '-h '
Help: -h The name of the speaking horse.
Usage: repeat [--count ] --horse [--include-counter]
See 'repeat --help' for more information.
```
Seems to mean the same as "--horse", so add the value:
```
release> ./repeat -h Secretariat
USAGE: repeat [--count ] --horse [--include-counter]

ARGUMENTS:
The phrase to repeat.

OPTIONS:
-c, --count The number of times to repeat 'phrase'.
-h, --horse The name of the speaking horse.
-i, --include-counter Include a counter with each repetition.
-h, --help Show help information.
```
Hm - now it seems to means "--help"

### Expected behavior

I expected the program to stop immediately with a fatal error. (I assume that
the conflict cannot be detected at compile time.)

### By the way.

One might consider modifying the current version of Repeat to avoid this:
```
release> ./repeat -c 0 hello
fish: Job 1, './repeat -c 0 hello' terminated by signal SIGTRAP (Trace or breakpoint trap)
```
No big deal.

Contributor guide

Open the contributing guide

Research direction

Run the Repeat example with the conflicting `horse` option and inspect the argument and help behavior shown in the report. Trace where short names and the built-in `-h` help option are handled; done means the conflict is rejected with a fatal error instead of producing duplicate help entries or ambiguous parsing.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.