mirror of
https://github.com/apple/swift-argument-parser.git
synced 2026-06-06 20:18:23 +00:00
1.4 KiB
1.4 KiB
ArgumentParser/ParsableCommand
ParsableCommand types are the basic building blocks for command-line tools built using ArgumentParser. To create a command, declare properties using the @Argument, @Option, and @Flag property wrappers, or include groups of options with @OptionGroup. Finally, implement your command's functionality in the run()-7p2fr method.
@main
struct Repeat: ParsableCommand {
@Argument(help: "The phrase to repeat.")
var phrase: String
@Option(help: "The number of times to repeat 'phrase'.")
var count: Int? = nil
mutating func run() throws {
let repeatCount = count ?? 2
for _ in 0..<repeatCount {
print(phrase)
}
}
}
Note: The Swift compiler uses either the type marked with
@mainor amain.swiftfile as the entry point for an executable program. You can use either one, but not both — rename yourmain.swiftfile to the name of the command when you add@main.
Topics
Essentials
Implementing a Command's Behavior
run()-7p2frParsableArguments/validate()-5r0ge
Customizing a Command
configuration-35km1CommandConfiguration
Generating Help Text
helpMessage(for:includeHidden:columns:)
Starting the Program
main()main(_:)
Manually Parsing Input
parseAsRoot(_:)