mirror of
https://github.com/apple/swift-argument-parser.git
synced 2026-06-06 20:18:23 +00:00
Adopts the common swift-mmio and swift-argument-parser format and enables CI checking. Fixes: #702
59 lines
1.4 KiB
Swift
59 lines
1.4 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2024 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import ArgumentParser
|
|
|
|
@main
|
|
struct Color: ParsableCommand {
|
|
@Option(help: "Your favorite color.")
|
|
var fav: ColorOptions
|
|
|
|
@Option(
|
|
help: .init("Your second favorite color.", discussion: "This is optional."))
|
|
var second: ColorOptions?
|
|
|
|
func run() {
|
|
print("My favorite color is \(fav.rawValue)")
|
|
if let second {
|
|
print("...And my second favorite is \(second.rawValue)!")
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum ColorOptions: String, CaseIterable, ExpressibleByArgument {
|
|
case red
|
|
case blue
|
|
case yellow
|
|
|
|
public var defaultValueDescription: String {
|
|
switch self {
|
|
case .red:
|
|
return "A red color."
|
|
case .blue:
|
|
return "A blue color."
|
|
case .yellow:
|
|
return "A yellow color."
|
|
}
|
|
}
|
|
|
|
public var description: String {
|
|
switch self {
|
|
case .red:
|
|
return "A red color."
|
|
case .blue:
|
|
return "A blue color."
|
|
case .yellow:
|
|
return "A yellow color."
|
|
}
|
|
}
|
|
}
|