ArgumentParser
Straightforward, type-safe argument parsing for Swift.
ArgumentParser.mdimport ArgumentParser
Interface Breakdown
Declarations
Symbols
Doc Coverage
Declarations
Symbols
Overview
By using ArgumentParser
, you can create a command-line interface tool by declaring simple Swift types. Begin by declaring a type that defines the information that you need to collect from the command line. Decorate each stored property with one of ArgumentParser
‘s property wrappers, declare conformance to ParsableCommand
, and implement your command’s logic in its run()
method.
import ArgumentParser
@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)
}
}
}
When a user executes your command, the ArgumentParser
library parses the command-line arguments, instantiates your command type, and then either calls your run()
method or exits with a useful message.
Additional Resources
See Also
Essentials
Getting Started with ArgumentParser
Learn to set up and customize a simple command-line tool.
Read Moreprotocol ParsableCommand
A type that can be executed as part of a nested tree of commands.
protocol AsyncParsableCommand
A type that can be executed asynchronously, as part of a nested tree of commands.
Defining Commands and Subcommands
Break complex command-line tools into a tree of subcommands.
Read MoreCustomizing Help for Commands
Define your command’s abstract, extended discussion, or usage string, and set the flags used to invoke the help display.
Read More
Arguments, Options, and Flags
Declaring Arguments, Options, and Flags
Use the
Read More@Argument
,@Option
and@Flag
property wrappers to declare the command-line interface for your command.struct Argument
A property wrapper that represents a positional command-line argument.
struct Option
A property wrapper that represents a command-line option.
struct Flag
A property wrapper that represents a command-line flag.
struct OptionGroup
A wrapper that transparently includes a parsable type.
protocol ParsableArguments
A type that can be parsed from a program’s command-line arguments.
Property Customization
Customizing Help
Support your users (and yourself) by providing rich help for arguments, options, and flags.
Read Morestruct ArgumentHelp
Help information for a command-line argument.
struct ArgumentVisibility
Visibility level of an argument’s help.
struct NameSpecification
A specification for how to represent a property as a command-line argument label.
Custom Types
protocol ExpressibleByArgument
A type that can be expressed as a command-line argument.
protocol EnumerableFlag
A type that represents the different possible flags to be used by a
@Flag
property.
Validation and Errors
Providing Custom Validation
Provide helpful feedback to users when things go wrong.
Read Morestruct ValidationError
An error type that is presented to the user as an error with parsing their command-line input.
struct CleanExit
An error type that represents a clean (i.e. non-error state) exit of the utility.
struct ExitCode
An error type that only includes an exit code.
Shell Completion Scripts
Generating and Installing Completion Scripts
Install shell completion scripts generated by your command-line tool.
Read MoreCustomizing Completions
Provide custom shell completions for your command-line tool’s arguments and options.
Read Morestruct CompletionKind
The type of completion to use for an argument or option.
Advanced Topics
Manual Parsing and Testing
Provide your own array of command-line inputs or work directly with parsed command-line arguments.
Read MoreExperimental Features
Learn about ArgumentParser’s experimental features.
Read More