Repository: apple/swift-argument-parser
Pull request #474 (2022-08-25 08:27:02)

Fixup help options for built-in flags
- 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.

rauhul: @swift-ci please test
rauhul: @swift-ci please test

Patch:
diff --git a/Sources/ArgumentParser/Usage/HelpGenerator.swift b/Sources/ArgumentParser/Usage/HelpGenerator.swift
index b216aa2..3f09f13 100644
--- a/Sources/ArgumentParser/Usage/HelpGenerator.swift
+++ b/Sources/ArgumentParser/Usage/HelpGenerator.swift
@@ -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 })
     )
@@ -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 })
     )
@@ -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,


Test patch:
diff --git a/Tests/ArgumentParserGenerateManualTests/CountLinesGenerateManualTests.swift b/Tests/ArgumentParserGenerateManualTests/CountLinesGenerateManualTests.swift
index 6b0b3ba..ab045db 100644
--- a/Tests/ArgumentParserGenerateManualTests/CountLinesGenerateManualTests.swift
+++ b/Tests/ArgumentParserGenerateManualTests/CountLinesGenerateManualTests.swift
@@ -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
@@ -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
diff --git a/Tests/ArgumentParserGenerateManualTests/MathGenerateManualTests.swift b/Tests/ArgumentParserGenerateManualTests/MathGenerateManualTests.swift
index 95de81c..23c3322 100644
--- a/Tests/ArgumentParserGenerateManualTests/MathGenerateManualTests.swift
+++ b/Tests/ArgumentParserGenerateManualTests/MathGenerateManualTests.swift
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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...
@@ -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
diff --git a/Tests/ArgumentParserGenerateManualTests/RepeatGenerateManualTests.swift b/Tests/ArgumentParserGenerateManualTests/RepeatGenerateManualTests.swift
index a0e98fb..c7b8c24 100644
--- a/Tests/ArgumentParserGenerateManualTests/RepeatGenerateManualTests.swift
+++ b/Tests/ArgumentParserGenerateManualTests/RepeatGenerateManualTests.swift
@@ -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
@@ -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
diff --git a/Tests/ArgumentParserGenerateManualTests/RollDiceGenerateManualTests.swift b/Tests/ArgumentParserGenerateManualTests/RollDiceGenerateManualTests.swift
index 2b93562..5c0d8a5 100644
--- a/Tests/ArgumentParserGenerateManualTests/RollDiceGenerateManualTests.swift
+++ b/Tests/ArgumentParserGenerateManualTests/RollDiceGenerateManualTests.swift
@@ -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
@@ -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
diff --git a/Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift b/Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift
index b05e826..f207ac9 100644
--- a/Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift
+++ b/Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift
@@ -180,7 +180,7 @@ extension DumpHelpGenerationTests {
       },
       {
         "abstract" : "Show help information.",
-        "isOptional" : false,
+        "isOptional" : true,
         "isRepeating" : false,
         "kind" : "flag",
         "names" : [
@@ -214,7 +214,7 @@ extension DumpHelpGenerationTests {
     "arguments" : [
       {
         "abstract" : "Show the version.",
-        "isOptional" : false,
+        "isOptional" : true,
         "isRepeating" : false,
         "kind" : "flag",
         "names" : [
@@ -232,7 +232,7 @@ extension DumpHelpGenerationTests {
       },
       {
         "abstract" : "Show help information.",
-        "isOptional" : false,
+        "isOptional" : true,
         "isRepeating" : false,
         "kind" : "flag",
         "names" : [
@@ -290,7 +290,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show the version.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -308,7 +308,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show help information.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -369,7 +369,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show the version.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -387,7 +387,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show help information.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -418,7 +418,7 @@ extension DumpHelpGenerationTests {
         "arguments" : [
           {
             "abstract" : "Show the version.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -436,7 +436,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show help information.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -496,7 +496,7 @@ extension DumpHelpGenerationTests {
               },
               {
                 "abstract" : "Show the version.",
-                "isOptional" : false,
+                "isOptional" : true,
                 "isRepeating" : false,
                 "kind" : "flag",
                 "names" : [
@@ -514,7 +514,7 @@ extension DumpHelpGenerationTests {
               },
               {
                 "abstract" : "Show help information.",
-                "isOptional" : false,
+                "isOptional" : true,
                 "isRepeating" : false,
                 "kind" : "flag",
                 "names" : [
@@ -554,7 +554,7 @@ extension DumpHelpGenerationTests {
               },
               {
                 "abstract" : "Show the version.",
-                "isOptional" : false,
+                "isOptional" : true,
                 "isRepeating" : false,
                 "kind" : "flag",
                 "names" : [
@@ -572,7 +572,7 @@ extension DumpHelpGenerationTests {
               },
               {
                 "abstract" : "Show help information.",
-                "isOptional" : false,
+                "isOptional" : true,
                 "isRepeating" : false,
                 "kind" : "flag",
                 "names" : [
@@ -762,7 +762,7 @@ extension DumpHelpGenerationTests {
               },
               {
                 "abstract" : "Show the version.",
-                "isOptional" : false,
+                "isOptional" : true,
                 "isRepeating" : false,
                 "kind" : "flag",
                 "names" : [
@@ -780,7 +780,7 @@ extension DumpHelpGenerationTests {
               },
               {
                 "abstract" : "Show help information.",
-                "isOptional" : false,
+                "isOptional" : true,
                 "isRepeating" : false,
                 "kind" : "flag",
                 "names" : [
@@ -855,7 +855,7 @@ extension DumpHelpGenerationTests {
       },
       {
         "abstract" : "Show the version.",
-        "isOptional" : false,
+        "isOptional" : true,
         "isRepeating" : false,
         "kind" : "flag",
         "names" : [
@@ -873,7 +873,7 @@ extension DumpHelpGenerationTests {
       },
       {
         "abstract" : "Show help information.",
-        "isOptional" : false,
+        "isOptional" : true,
         "isRepeating" : false,
         "kind" : "flag",
         "names" : [
@@ -940,7 +940,7 @@ extension DumpHelpGenerationTests {
       },
       {
         "abstract" : "Show the version.",
-        "isOptional" : false,
+        "isOptional" : true,
         "isRepeating" : false,
         "kind" : "flag",
         "names" : [
@@ -958,7 +958,7 @@ extension DumpHelpGenerationTests {
       },
       {
         "abstract" : "Show help information.",
-        "isOptional" : false,
+        "isOptional" : true,
         "isRepeating" : false,
         "kind" : "flag",
         "names" : [
@@ -995,7 +995,7 @@ extension DumpHelpGenerationTests {
     "arguments" : [
       {
         "abstract" : "Show the version.",
-        "isOptional" : false,
+        "isOptional" : true,
         "isRepeating" : false,
         "kind" : "flag",
         "names" : [
@@ -1013,7 +1013,7 @@ extension DumpHelpGenerationTests {
       },
       {
         "abstract" : "Show help information.",
-        "isOptional" : false,
+        "isOptional" : true,
         "isRepeating" : false,
         "kind" : "flag",
         "names" : [
@@ -1073,7 +1073,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show the version.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -1091,7 +1091,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show help information.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -1131,7 +1131,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show the version.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -1149,7 +1149,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show help information.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -1339,7 +1339,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show the version.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [
@@ -1357,7 +1357,7 @@ extension DumpHelpGenerationTests {
           },
           {
             "abstract" : "Show help information.",
-            "isOptional" : false,
+            "isOptional" : true,
             "isRepeating" : false,
             "kind" : "flag",
             "names" : [