Skip to content

Commit f1c6afd

Browse files
authored
Fixup help options for built-in flags (#474)
- 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.
1 parent b1b0595 commit f1c6afd

File tree

6 files changed

+58
-51
lines changed

6 files changed

+58
-51
lines changed

Sources/ArgumentParser/Usage/HelpGenerator.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,10 @@ internal extension BidirectionalCollection where Element == ParsableCommand.Type
307307
else { return nil }
308308
return ArgumentDefinition(
309309
kind: .named([.long("version")]),
310-
help: .init(help: "Show the version.", key: InputKey(rawValue: "")),
310+
help: .init(
311+
options: [.isOptional],
312+
help: "Show the version.",
313+
key: InputKey(rawValue: "")),
311314
completion: .default,
312315
update: .nullary({ _, _, _ in })
313316
)
@@ -318,7 +321,10 @@ internal extension BidirectionalCollection where Element == ParsableCommand.Type
318321
guard !names.isEmpty else { return nil }
319322
return ArgumentDefinition(
320323
kind: .named(names),
321-
help: .init(help: "Show help information.", key: InputKey(rawValue: "")),
324+
help: .init(
325+
options: [.isOptional],
326+
help: "Show help information.",
327+
key: InputKey(rawValue: "")),
322328
completion: .default,
323329
update: .nullary({ _, _, _ in })
324330
)
@@ -328,6 +334,7 @@ internal extension BidirectionalCollection where Element == ParsableCommand.Type
328334
return ArgumentDefinition(
329335
kind: .named([.long("experimental-dump-help")]),
330336
help: .init(
337+
options: [.isOptional],
331338
help: ArgumentHelp("Dump help information as JSON."),
332339
key: InputKey(rawValue: "")),
333340
completion: .default,

Tests/ArgumentParserGenerateManualTests/CountLinesGenerateManualTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class CountLinesGenerateManualTests: XCTestCase {
2929
.Ar input-file
3030
.Op Fl -prefix Ar prefix
3131
.Op Fl -verbose
32-
.Fl -help
32+
.Op Fl -help
3333
.Sh DESCRIPTION
3434
.Bl -tag -width 6n
3535
.It Ar input-file
@@ -71,7 +71,7 @@ final class CountLinesGenerateManualTests: XCTestCase {
7171
.Ar input-file
7272
.Op Fl -prefix Ar prefix
7373
.Op Fl -verbose
74-
.Fl -help
74+
.Op Fl -help
7575
.Sh DESCRIPTION
7676
.Bl -tag -width 6n
7777
.It Ar input-file

Tests/ArgumentParserGenerateManualTests/MathGenerateManualTests.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ final class MathGenerateManualTests: XCTestCase {
2626
.Sh SYNOPSIS
2727
.Nm
2828
.Ar subcommand
29-
.Fl -version
30-
.Fl -help
29+
.Op Fl -version
30+
.Op Fl -help
3131
.Sh DESCRIPTION
3232
.Bl -tag -width 6n
3333
.It Fl -version
@@ -131,8 +131,8 @@ final class MathGenerateManualTests: XCTestCase {
131131
.Sh SYNOPSIS
132132
.Nm
133133
.Ar subcommand
134-
.Fl -version
135-
.Fl -help
134+
.Op Fl -version
135+
.Op Fl -help
136136
.Sh DESCRIPTION
137137
.Bl -tag -width 6n
138138
.It Fl -version
@@ -168,8 +168,8 @@ final class MathGenerateManualTests: XCTestCase {
168168
.Nm
169169
.Op Fl -hex-output
170170
.Op Ar values...
171-
.Fl -version
172-
.Fl -help
171+
.Op Fl -version
172+
.Op Fl -help
173173
.Sh DESCRIPTION
174174
.Bl -tag -width 6n
175175
.It Fl x , -hex-output
@@ -205,8 +205,8 @@ final class MathGenerateManualTests: XCTestCase {
205205
.Nm
206206
.Op Fl -hex-output
207207
.Op Ar values...
208-
.Fl -version
209-
.Fl -help
208+
.Op Fl -version
209+
.Op Fl -help
210210
.Sh DESCRIPTION
211211
.Bl -tag -width 6n
212212
.It Fl x , -hex-output
@@ -241,8 +241,8 @@ final class MathGenerateManualTests: XCTestCase {
241241
.Sh SYNOPSIS
242242
.Nm
243243
.Ar subcommand
244-
.Fl -version
245-
.Fl -help
244+
.Op Fl -version
245+
.Op Fl -help
246246
.Sh DESCRIPTION
247247
.Bl -tag -width 6n
248248
.It Fl -version
@@ -278,8 +278,8 @@ final class MathGenerateManualTests: XCTestCase {
278278
.Nm
279279
.Op Fl -kind Ar kind
280280
.Op Ar values...
281-
.Fl -version
282-
.Fl -help
281+
.Op Fl -version
282+
.Op Fl -help
283283
.Sh DESCRIPTION
284284
.Bl -tag -width 6n
285285
.It Fl -kind Ar kind
@@ -314,8 +314,8 @@ final class MathGenerateManualTests: XCTestCase {
314314
.Sh SYNOPSIS
315315
.Nm
316316
.Op Ar values...
317-
.Fl -version
318-
.Fl -help
317+
.Op Fl -version
318+
.Op Fl -help
319319
.Sh DESCRIPTION
320320
.Bl -tag -width 6n
321321
.It Ar values...
@@ -358,8 +358,8 @@ final class MathGenerateManualTests: XCTestCase {
358358
.Op Fl -directory Ar directory
359359
.Op Fl -shell Ar shell
360360
.Op Fl -custom Ar custom
361-
.Fl -version
362-
.Fl -help
361+
.Op Fl -version
362+
.Op Fl -help
363363
.Sh DESCRIPTION
364364
.Bl -tag -width 6n
365365
.It Ar one-of-four

Tests/ArgumentParserGenerateManualTests/RepeatGenerateManualTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class RepeatGenerateManualTests: XCTestCase {
2626
.Op Fl -count Ar count
2727
.Op Fl -include-counter
2828
.Ar phrase
29-
.Fl -help
29+
.Op Fl -help
3030
.Sh DESCRIPTION
3131
.Bl -tag -width 6n
3232
.It Fl -count Ar count
@@ -67,7 +67,7 @@ final class RepeatGenerateManualTests: XCTestCase {
6767
.Op Fl -count Ar count
6868
.Op Fl -include-counter
6969
.Ar phrase
70-
.Fl -help
70+
.Op Fl -help
7171
.Sh DESCRIPTION
7272
.Bl -tag -width 6n
7373
.It Fl -count Ar count

Tests/ArgumentParserGenerateManualTests/RollDiceGenerateManualTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class RollDiceGenerateManualTests: XCTestCase {
2727
.Op Fl -sides Ar m
2828
.Op Fl -seed Ar seed
2929
.Op Fl -verbose
30-
.Fl -help
30+
.Op Fl -help
3131
.Sh DESCRIPTION
3232
.Bl -tag -width 6n
3333
.It Fl -times Ar n
@@ -73,7 +73,7 @@ final class RollDiceGenerateManualTests: XCTestCase {
7373
.Op Fl -sides Ar m
7474
.Op Fl -seed Ar seed
7575
.Op Fl -verbose
76-
.Fl -help
76+
.Op Fl -help
7777
.Sh DESCRIPTION
7878
.Bl -tag -width 6n
7979
.It Fl -times Ar n

0 commit comments

Comments
 (0)