CLI
public enum CLI
Namespace for all commandline related functions and objects.
-
Prompt handler for print output and read input for command line. Initial
CLI.promptvalue is set to print to stdout and stderr, and read from stdin.Note
CustomPromptHandlercan be useful in tests.Declaration
Swift
public static var prompt: PromptHandler -
Flag for globally enable or disable the
StringStyleformatting. If set tofalse, string styles not be evaluated inside strings. Default istrue.Declaration
Swift
public static var enableStringStyles: Bool -
Access to environment variables of current process.
Declaration
Swift
public static var env: CLI.Environment -
Access to parsed command line arguments for current process.
Declaration
Swift
public static let args: CLI.Arguments -
Holds global instance of
ProcessBuilderthat is used for createProcessinstances forCommandexecution. Default isCLI.Shell.zshon macOS andCLI.Shell.bashon Linux.See also
CLI.ShellDeclaration
Swift
public static var processBuilder: ProcessBuilder
-
Available string styles.
This styles can be used in string iterpolation:
print("Result is \(result.exitCode, styled: .fgRed, .italic)")or can be used with any instances of
StringProtocol:print("Init...".styled(.bgMagenta, .bold, .fgRed))See moreNote
Uses SGR parameters from ANSI escape codesDeclaration
Swift
struct StringStyle
-
Print string to defined prompt (default is
stdout). Printed string is not terminated by newline.Declaration
Swift
@inlinable static func print(_ string: String)Parameters
stringThe text to print.
-
Print string with a new line terminator to defined prompt (default is
stdout).Declaration
Swift
@inlinable static func println(_ string: String)Parameters
stringThe text to print.
-
Print string to defined error prompt (default is
stderr). Printed string is not terminated by newline.Declaration
Swift
@inlinable static func print(error string: String)Parameters
stringThe error text to print.
-
Print string with a new line terminator to defined error prompt (default is
stderr).Declaration
Swift
@inlinable static func println(error string: String)Parameters
stringThe error text to print.
-
Displays prompt to the user.
Declaration
Swift
static func ask(_ prompt: String, options: AskOption<String>...) -> StringParameters
promptThe message to display.
optionsThe prompt customization options.
Return Value
The string enters from the user.
-
Displays prompt to the user.
If the user enters a string wich cannot be converted to the expected type,
askwill keep prompting until a corect value has been entered.Declaration
Swift
static func ask<T>(_ prompt: String, type: T.Type = T.self, options: AskOption<T>...) -> T where T : ExpressibleByStringArgumentParameters
promptThe message to display.
typeThe value type to be expected from the user.
optionsThe prompt customization options.
Return Value
The converted string enters from the user.
-
Options for
CLI.ask(_:options:)functions which can customize value handling and custom prompt validations.This options are passed to
CLI.ask(_:options:)functions, for example:
See more// Define a default value and require the entered value confirmation. CLI.ask("Choose directory [/tmp]: ", options: .default("/tmp"), .confirm()) // Prints: // $ Choose directory [/tmp]: <<user input>> // $ Are you sure? <<y>> // The result is "<<user input>>" or "/tmp" if the user only press Enter key.Declaration
Swift
enum AskOption<T> where T : ExpressibleByStringArgument
-
Displays a menu of items to the user to choose from.
Precondition
The choices must be non empty array.Declaration
Swift
static func choose(_ prompt: String, choices: [String]) -> StringParameters
promptThe message to display.
choicesThe items to choose from.
Return Value
An one of the choices that the user choose.
-
Displays a menu of items to the user to choose from.
Precondition
The choices must be non empty dictionary.Declaration
Swift
static func choose<T>(_ prompt: String, choices: [String : T]) -> TParameters
promptThe message to display.
choicesThe items to choose from. The choice key will be shown to the user (sorted) and dictionary value for that key will be returned from this function.
Return Value
A value of the one of choices that the user choose.
-
Run external command with defined executor.
Precondition
The command arguments is not empty.See also
Command.execute()Declaration
Swift
@discardableResult @inlinable static func run(_ args: String..., executor: CommandExecutor = .default) throws -> StringParameters
argsThe command with arguments to be executed.
executorThe command executor for execute defined command.
Return Value
A command result with exit status code and parsed stdout and stderr outputs.
-
The command which to be executed.
This struct holds all settings for command execution and can be created througt
CLI.run(_:_:executor:)functions or directly.CLI.run("echo -n", "Hi!") // is equivalent to Command(["echo", "-n", "Hi!"]).execute()See moreNote
Commandis more complex than simplerunfunction. You can specify command working directory or environment variables.Declaration
Swift
struct Command : CustomStringConvertible -
Types of supported command executors.
See moreDeclaration
Swift
enum CommandExecutor -
Basic implementation of
See moreProcessBuilder, used to execute common shells.Declaration
Swift
struct Shell : ProcessBuilder -
The error type that are used if command not executed correctly.
See moreDeclaration
Swift
struct CommandExecutionError : Error
-
A type for handle environment variables.
See moreDeclaration
Swift
struct Environment
-
A type that handles the command line arguments passed to the script.
See moreNote
The arguments will be parsed only if you access to any attribute that require arguments parsing, which is the attributescommand,flagsandparameters. The arguments are parsed only once.Declaration
Swift
class Arguments
View on GitHub
CLI Enumeration Reference