Skip to content

Commit d28f725

Browse files
Use CaseIterable to auto-generate CLI --help entries (#8045)
The swift-argument-parser library will automatically show the available values for options that implement the CaseIterable protocol. There are options in SwiftPM that do either list the available values statically in the help message, which can be stale, or don't provide a list at all, which leaves the user to guess the value that they want. Sweep these options, removing the static list, sometimes adding the help message based on the swift documentation. Since the CaseIterable doesn't provide a description of each value, leave the options alone that provide detail for each of their available values. The result of these changes is that the online help (and content assist) is able to automatically display all of the available values for certain options. Also, some of the options have a help description for the first time. --------- Co-authored-by: Max Desiatov <[email protected]>
1 parent 7f2a2fc commit d28f725

File tree

6 files changed

+22
-22
lines changed

6 files changed

+22
-22
lines changed

Sources/Commands/PackageCommands/AddProduct.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension SwiftPackageCommand {
2525
struct AddProduct: SwiftCommand {
2626
/// The package product type used for the command-line. This is a
2727
/// subset of `ProductType` that expands out the library types.
28-
enum CommandProductType: String, Codable, ExpressibleByArgument {
28+
enum CommandProductType: String, Codable, ExpressibleByArgument, CaseIterable {
2929
case executable
3030
case library
3131
case staticLibrary = "static-library"
@@ -42,7 +42,7 @@ extension SwiftPackageCommand {
4242
@Argument(help: "The name of the new product")
4343
var name: String
4444

45-
@Option(help: "The type of target to add, which can be one of 'executable', 'library', 'static-library', 'dynamic-library', or 'plugin'")
45+
@Option(help: "The type of target to add")
4646
var type: CommandProductType = .library
4747

4848
@Option(

Sources/Commands/PackageCommands/AddTarget.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension AddTarget.TestHarness: ExpressibleByArgument { }
2626
extension SwiftPackageCommand {
2727
struct AddTarget: SwiftCommand {
2828
/// The type of target that can be specified on the command line.
29-
enum TargetType: String, Codable, ExpressibleByArgument {
29+
enum TargetType: String, Codable, ExpressibleByArgument, CaseIterable {
3030
case library
3131
case executable
3232
case test
@@ -42,7 +42,7 @@ extension SwiftPackageCommand {
4242
@Argument(help: "The name of the new target")
4343
var name: String
4444

45-
@Option(help: "The type of target to add, which can be one of 'library', 'executable', 'test', or 'macro'")
45+
@Option(help: "The type of target to add")
4646
var type: TargetType = .library
4747

4848
@Option(

Sources/Commands/PackageCommands/Describe.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension SwiftPackageCommand {
2626
@OptionGroup(visibility: .hidden)
2727
var globalOptions: GlobalOptions
2828

29-
@Option(help: "json | text | mermaid")
29+
@Option(help: "Set the output format")
3030
var type: DescribeMode = .text
3131

3232
func run(_ swiftCommandState: SwiftCommandState) async throws {
@@ -63,7 +63,7 @@ extension SwiftPackageCommand {
6363
print(String(decoding: data, as: UTF8.self))
6464
}
6565

66-
enum DescribeMode: String, ExpressibleByArgument {
66+
enum DescribeMode: String, ExpressibleByArgument, CaseIterable {
6767
/// JSON format (guaranteed to be parsable and stable across time).
6868
case json
6969
/// Human readable format (not guaranteed to be parsable).

Sources/Commands/PackageCommands/ShowDependencies.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension SwiftPackageCommand {
2828
@OptionGroup(visibility: .hidden)
2929
var globalOptions: GlobalOptions
3030

31-
@Option(help: "text | dot | json | flatlist")
31+
@Option(help: "Set the output format")
3232
var format: ShowDependenciesMode = .text
3333

3434
@Option(name: [.long, .customShort("o") ],
@@ -69,7 +69,7 @@ extension SwiftPackageCommand {
6969
stream.flush()
7070
}
7171

72-
enum ShowDependenciesMode: String, RawRepresentable, CustomStringConvertible, ExpressibleByArgument {
72+
enum ShowDependenciesMode: String, RawRepresentable, CustomStringConvertible, ExpressibleByArgument, CaseIterable {
7373
case text, dot, json, flatlist
7474

7575
public init?(rawValue: String) {

Sources/CoreCommands/Options.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public struct CachingOptions: ParsableArguments {
162162
/// Disables manifest caching.
163163
@Option(
164164
name: .customLong("manifest-cache"),
165-
help: "Caching mode of Package.swift manifests (shared: shared cache, local: package's build directory, none: disabled"
165+
help: "Caching mode of Package.swift manifests (shared: shared cache, local: package's build directory, none: disabled)"
166166
)
167167
public var manifestCachingMode: ManifestCachingMode = .shared
168168

@@ -470,7 +470,7 @@ public struct BuildOptions: ParsableArguments {
470470

471471
/// A flag that indicates this build should check whether targets only import
472472
/// their explicitly-declared dependencies
473-
@Option()
473+
@Option(help: "A flag that indicates this build should check whether targets only import their explicitly-declared dependencies")
474474
public var explicitTargetDependencyImportCheck: TargetDependencyImportCheckingMode = .none
475475

476476
/// Whether to use the explicit module build flow (with the integrated driver)
@@ -482,7 +482,7 @@ public struct BuildOptions: ParsableArguments {
482482
var _buildSystem: BuildSystemProvider.Kind = .native
483483

484484
/// The Debug Information Format to use.
485-
@Option(name: .customLong("debug-info-format", withSingleDash: true))
485+
@Option(name: .customLong("debug-info-format", withSingleDash: true), help: "The Debug Information Format to use")
486486
public var debugInfoFormat: DebugInfoFormat = .dwarf
487487

488488
public var buildSystem: BuildSystemProvider.Kind {
@@ -527,7 +527,7 @@ public struct BuildOptions: ParsableArguments {
527527
case disableIndexStore
528528
}
529529

530-
public enum TargetDependencyImportCheckingMode: String, Codable, ExpressibleByArgument {
530+
public enum TargetDependencyImportCheckingMode: String, Codable, ExpressibleByArgument, CaseIterable {
531531
case none
532532
case warn
533533
case error
@@ -542,7 +542,7 @@ public struct BuildOptions: ParsableArguments {
542542
}
543543

544544
/// See `BuildParameters.DebugInfoFormat` for details.
545-
public enum DebugInfoFormat: String, Codable, ExpressibleByArgument {
545+
public enum DebugInfoFormat: String, Codable, ExpressibleByArgument, CaseIterable {
546546
/// See `BuildParameters.DebugInfoFormat.dwarf` for details.
547547
case dwarf
548548
/// See `BuildParameters.DebugInfoFormat.codeview` for details.
@@ -747,20 +747,20 @@ extension URL {
747747
}
748748

749749
#if compiler(<6.0)
750-
extension BuildConfiguration: ExpressibleByArgument {}
750+
extension BuildConfiguration: ExpressibleByArgument, CaseIterable {}
751751
extension AbsolutePath: ExpressibleByArgument {}
752752
extension WorkspaceConfiguration.CheckingMode: ExpressibleByArgument {}
753753
extension Sanitizer: ExpressibleByArgument {}
754-
extension BuildSystemProvider.Kind: ExpressibleByArgument {}
754+
extension BuildSystemProvider.Kind: ExpressibleByArgument, CaseIterable {}
755755
extension Version: ExpressibleByArgument {}
756756
extension PackageIdentity: ExpressibleByArgument {}
757757
extension URL: ExpressibleByArgument {}
758758
#else
759-
extension BuildConfiguration: @retroactive ExpressibleByArgument {}
759+
extension BuildConfiguration: @retroactive ExpressibleByArgument, CaseIterable {}
760760
extension AbsolutePath: @retroactive ExpressibleByArgument {}
761761
extension WorkspaceConfiguration.CheckingMode: @retroactive ExpressibleByArgument {}
762762
extension Sanitizer: @retroactive ExpressibleByArgument {}
763-
extension BuildSystemProvider.Kind: @retroactive ExpressibleByArgument {}
763+
extension BuildSystemProvider.Kind: @retroactive ExpressibleByArgument, CaseIterable {}
764764
extension Version: @retroactive ExpressibleByArgument {}
765765
extension PackageIdentity: @retroactive ExpressibleByArgument {}
766766
extension URL: @retroactive ExpressibleByArgument {}

Sources/swift-bootstrap/main.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ struct SwiftBootstrapBuildTool: AsyncParsableCommand {
116116
@Flag()
117117
public var useIntegratedSwiftDriver: Bool = false
118118

119-
/// A flag that indicates this build should check whether targets only import
119+
/// An option that indicates this build should check whether targets only import
120120
/// their explicitly-declared dependencies
121-
@Option()
121+
@Option(help: "Check that targets only import their explicitly-declared dependencies")
122122
public var explicitTargetDependencyImportCheck: TargetDependencyImportCheckingMode = .none
123123

124-
enum TargetDependencyImportCheckingMode: String, Codable, ExpressibleByArgument {
124+
enum TargetDependencyImportCheckingMode: String, Codable, ExpressibleByArgument, CaseIterable {
125125
case none
126126
case error
127127
}
@@ -473,10 +473,10 @@ extension BuildConfiguration {
473473

474474
#if compiler(<6.0)
475475
extension AbsolutePath: ExpressibleByArgument {}
476-
extension BuildConfiguration: ExpressibleByArgument {}
476+
extension BuildConfiguration: ExpressibleByArgument, CaseIterable {}
477477
#else
478478
extension AbsolutePath: @retroactive ExpressibleByArgument {}
479-
extension BuildConfiguration: @retroactive ExpressibleByArgument {}
479+
extension BuildConfiguration: @retroactive ExpressibleByArgument, CaseIterable {}
480480
#endif
481481

482482
public func topologicalSort<T: Hashable>(

0 commit comments

Comments
 (0)