Skip to content

Commit

Permalink
Fixup help options for built-in flags (#474)
Browse files Browse the repository at this point in the history
- Fixes a bug where built-in flags such as --help and --version were not
  properly marked with isOptional which resulted in them appearing in
  generated content (such as completion scripts and manuals) as required
  arguments.
  • Loading branch information
rauhul committed Aug 26, 2022
1 parent b1b0595 commit f1c6afd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 51 deletions.
11 changes: 9 additions & 2 deletions Sources/ArgumentParser/Usage/HelpGenerator.swift
Expand Up @@ -307,7 +307,10 @@ internal extension BidirectionalCollection where Element == ParsableCommand.Type
else { return nil }
return ArgumentDefinition(
kind: .named([.long("version")]),
help: .init(help: "Show the version.", key: InputKey(rawValue: "")),
help: .init(
options: [.isOptional],
help: "Show the version.",
key: InputKey(rawValue: "")),
completion: .default,
update: .nullary({ _, _, _ in })
)
Expand All @@ -318,7 +321,10 @@ internal extension BidirectionalCollection where Element == ParsableCommand.Type
guard !names.isEmpty else { return nil }
return ArgumentDefinition(
kind: .named(names),
help: .init(help: "Show help information.", key: InputKey(rawValue: "")),
help: .init(
options: [.isOptional],
help: "Show help information.",
key: InputKey(rawValue: "")),
completion: .default,
update: .nullary({ _, _, _ in })
)
Expand All @@ -328,6 +334,7 @@ internal extension BidirectionalCollection where Element == ParsableCommand.Type
return ArgumentDefinition(
kind: .named([.long("experimental-dump-help")]),
help: .init(
options: [.isOptional],
help: ArgumentHelp("Dump help information as JSON."),
key: InputKey(rawValue: "")),
completion: .default,
Expand Down
Expand Up @@ -29,7 +29,7 @@ final class CountLinesGenerateManualTests: XCTestCase {
.Ar input-file
.Op Fl -prefix Ar prefix
.Op Fl -verbose
.Fl -help
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Ar input-file
Expand Down Expand Up @@ -71,7 +71,7 @@ final class CountLinesGenerateManualTests: XCTestCase {
.Ar input-file
.Op Fl -prefix Ar prefix
.Op Fl -verbose
.Fl -help
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Ar input-file
Expand Down
Expand Up @@ -26,8 +26,8 @@ final class MathGenerateManualTests: XCTestCase {
.Sh SYNOPSIS
.Nm
.Ar subcommand
.Fl -version
.Fl -help
.Op Fl -version
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl -version
Expand Down Expand Up @@ -131,8 +131,8 @@ final class MathGenerateManualTests: XCTestCase {
.Sh SYNOPSIS
.Nm
.Ar subcommand
.Fl -version
.Fl -help
.Op Fl -version
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl -version
Expand Down Expand Up @@ -168,8 +168,8 @@ final class MathGenerateManualTests: XCTestCase {
.Nm
.Op Fl -hex-output
.Op Ar values...
.Fl -version
.Fl -help
.Op Fl -version
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl x , -hex-output
Expand Down Expand Up @@ -205,8 +205,8 @@ final class MathGenerateManualTests: XCTestCase {
.Nm
.Op Fl -hex-output
.Op Ar values...
.Fl -version
.Fl -help
.Op Fl -version
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl x , -hex-output
Expand Down Expand Up @@ -241,8 +241,8 @@ final class MathGenerateManualTests: XCTestCase {
.Sh SYNOPSIS
.Nm
.Ar subcommand
.Fl -version
.Fl -help
.Op Fl -version
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl -version
Expand Down Expand Up @@ -278,8 +278,8 @@ final class MathGenerateManualTests: XCTestCase {
.Nm
.Op Fl -kind Ar kind
.Op Ar values...
.Fl -version
.Fl -help
.Op Fl -version
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl -kind Ar kind
Expand Down Expand Up @@ -314,8 +314,8 @@ final class MathGenerateManualTests: XCTestCase {
.Sh SYNOPSIS
.Nm
.Op Ar values...
.Fl -version
.Fl -help
.Op Fl -version
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Ar values...
Expand Down Expand Up @@ -358,8 +358,8 @@ final class MathGenerateManualTests: XCTestCase {
.Op Fl -directory Ar directory
.Op Fl -shell Ar shell
.Op Fl -custom Ar custom
.Fl -version
.Fl -help
.Op Fl -version
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Ar one-of-four
Expand Down
Expand Up @@ -26,7 +26,7 @@ final class RepeatGenerateManualTests: XCTestCase {
.Op Fl -count Ar count
.Op Fl -include-counter
.Ar phrase
.Fl -help
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl -count Ar count
Expand Down Expand Up @@ -67,7 +67,7 @@ final class RepeatGenerateManualTests: XCTestCase {
.Op Fl -count Ar count
.Op Fl -include-counter
.Ar phrase
.Fl -help
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl -count Ar count
Expand Down
Expand Up @@ -27,7 +27,7 @@ final class RollDiceGenerateManualTests: XCTestCase {
.Op Fl -sides Ar m
.Op Fl -seed Ar seed
.Op Fl -verbose
.Fl -help
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl -times Ar n
Expand Down Expand Up @@ -73,7 +73,7 @@ final class RollDiceGenerateManualTests: XCTestCase {
.Op Fl -sides Ar m
.Op Fl -seed Ar seed
.Op Fl -verbose
.Fl -help
.Op Fl -help
.Sh DESCRIPTION
.Bl -tag -width 6n
.It Fl -times Ar n
Expand Down

0 comments on commit f1c6afd

Please sign in to comment.