-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathDefaultAsFlag.swift
More file actions
54 lines (45 loc) · 1.74 KB
/
DefaultAsFlag.swift
File metadata and controls
54 lines (45 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2025 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
//
//===----------------------------------------------------------------------===//
import ArgumentParser
@main
struct DefaultAsFlag: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "A utility demonstrating defaultAsFlag options.",
discussion: """
This command shows how defaultAsFlag options can work both as flags
and as options with values.
"""
)
@Option(defaultAsFlag: "default", help: "A string option with defaultAsFlag.")
var stringFlag: String?
@Option(defaultAsFlag: 42, help: "An integer option with defaultAsFlag.")
var numberFlag: Int?
@Option(defaultAsFlag: true, help: "A boolean option with defaultAsFlag.")
var boolFlag: Bool?
@Option(
defaultAsFlag: "transformed",
help: "A string option with transform and defaultAsFlag.",
transform: { $0.uppercased() }
)
var transformFlag: String?
@Option(name: .shortAndLong, help: "A regular option for comparison.")
var regular: String?
@Argument
var additionalArgs: [String] = []
func run() {
print("String flag: \(stringFlag?.description ?? "nil")")
print("Number flag: \(numberFlag?.description ?? "nil")")
print("Bool flag: \(boolFlag?.description ?? "nil")")
print("Transform flag: \(transformFlag?.description ?? "nil")")
print("Regular option: \(regular?.description ?? "nil")")
print("Additional args: \(additionalArgs)")
}
}