Skip to content

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

@psummerland

Description

@psummerland

Checklist

  • [ x] If possible, I've reproduced the issue using the main branch of this package
  • [ x] I've searched for existing GitHub issues

Steps to Reproduce

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

 @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 <count>] [--include-counter] <phrase> --horse <horse>

ARGUMENTS:
  <phrase>                The phrase to repeat.

OPTIONS:
  -c, --count <count>     The number of times to repeat 'phrase'.
  -i, --include-counter   Include a counter with each repetition.
  -h, --horse <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 <horse>'
Help:  -h <horse>  The name of the speaking horse.
Usage: repeat [--count <count>] --horse <horse> [--include-counter] <phrase>
  See 'repeat --help' for more information.

Seems to mean the same as "--horse", so add the value:

release> ./repeat -h Secretariat
USAGE: repeat [--count <count>] --horse <horse> [--include-counter] <phrase>

ARGUMENTS:
  <phrase>                The phrase to repeat.

OPTIONS:
  -c, --count <count>     The number of times to repeat 'phrase'.
  -h, --horse <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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions