All Projects → apple → Swift Argument Parser

apple / Swift Argument Parser

Licence: apache-2.0
Straightforward, type-safe argument parsing for Swift

Programming Languages

swift
15916 projects
CMake
9771 projects
shell
77523 projects

Projects that are alternatives of or similar to Swift Argument Parser

tiinvo
Functions for tacit programming and functional types for TypeScript and JavaScript.
Stars: ✭ 36 (-98.52%)
Mutual labels:  option
mango
mango is a man-page generator for the Go flag, pflag, cobra, coral, and kong packages
Stars: ✭ 191 (-92.14%)
Mutual labels:  flag
set-config-resolver
[READ-ONLY] Loads configs to you with CLI --config, -c, --set, -s or sets parameter
Stars: ✭ 50 (-97.94%)
Mutual labels:  option
Country
Country gives a list of countries with all their flags.
Stars: ✭ 26 (-98.93%)
Mutual labels:  flag
django-flag-app
A pluggable django application that adds the ability for users to flag(or report) your models.
Stars: ✭ 13 (-99.47%)
Mutual labels:  flag
akshare
AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库
Stars: ✭ 5,155 (+112.14%)
Mutual labels:  option
common
A minimal library that defines primitive building blocks of PHP code.
Stars: ✭ 28 (-98.85%)
Mutual labels:  option
Akshare
AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库
Stars: ✭ 4,334 (+78.35%)
Mutual labels:  option
dart maybe
No more null check with an dart equivalent of Maybe (Haskel, Elm) / Option (F#).
Stars: ✭ 20 (-99.18%)
Mutual labels:  option
nginx cookie flag module
Module for Nginx which allows to set the flags "HttpOnly", "secure" and "SameSite" for cookies.
Stars: ✭ 101 (-95.84%)
Mutual labels:  flag
flag
专为命令行爱好者设计,方便写出类似于git或者coreutils中的命令接口(ls, mv, cat),新用户建议使用https://github.com/guonaihong/clop
Stars: ✭ 48 (-98.02%)
Mutual labels:  flag
country-flag-emoji-json
Country flag emojis in JSON format.
Stars: ✭ 92 (-96.21%)
Mutual labels:  flag
ts-belt
🔧 Fast, modern, and practical utility library for FP in TypeScript.
Stars: ✭ 439 (-81.93%)
Mutual labels:  option
either option
A small typed and safe library for error handling with functionnal programming concept in Dart and flutter project
Stars: ✭ 34 (-98.6%)
Mutual labels:  option
goconf
Configuration loader in Go
Stars: ✭ 23 (-99.05%)
Mutual labels:  flag
country-emoji
Converts between country names, ISO 3166-1 codes and flag emojis.
Stars: ✭ 81 (-96.67%)
Mutual labels:  flag
monas
🦋 Scala monads for javascript
Stars: ✭ 21 (-99.14%)
Mutual labels:  option
Co
Art of C++. Flag, logging, unit-test, json, go-style coroutine and more.
Stars: ✭ 2,264 (-6.83%)
Mutual labels:  flag
rust-error-handle
detail rust error handle
Stars: ✭ 47 (-98.07%)
Mutual labels:  option
go-getoptions
Fully featured Go (golang) command line option parser with built-in auto-completion support.
Stars: ✭ 41 (-98.31%)
Mutual labels:  flag

Swift Argument Parser

Usage

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, and then declare conformance to ParsableCommand and add the @main attribute. Finally, implement your command's logic in the run() method.

import ArgumentParser

@main
struct Repeat: ParsableCommand {
    @Flag(help: "Include a counter with each repetition.")
    var includeCounter = false

    @Option(name: .shortAndLong, help: "The number of times to repeat 'phrase'.")
    var count: Int?

    @Argument(help: "The phrase to repeat.")
    var phrase: String

    mutating func run() throws {
        let repeatCount = count ?? .max

        for i in 1...repeatCount {
            if includeCounter {
                print("\(i): \(phrase)")
            } else {
                print(phrase)
            }
        }
    }
}

The ArgumentParser library parses the command-line arguments, instantiates your command type, and then either executes your run() method or exits with a useful message.

ArgumentParser uses your properties' names and type information, along with the details you provide using property wrappers, to supply useful error messages and detailed help:

$ repeat hello --count 3
hello
hello
hello
$ repeat --count 3
Error: Missing expected argument 'phrase'.
Help:  <phrase>  The phrase to repeat.
Usage: repeat [--count <count>] [--include-counter] <phrase>
  See 'repeat --help' for more information.
$ repeat --help
USAGE: repeat [--count <count>] [--include-counter] <phrase>

ARGUMENTS:
  <phrase>                The phrase to repeat.

OPTIONS:
  --include-counter       Include a counter with each repetition.
  -c, --count <count>     The number of times to repeat 'phrase'.
  -h, --help              Show help for this command.

For more information and documentation about all supported options, see the library's documentation in Xcode.

Examples

This repository includes a few examples of using the library:

  • repeat is the example shown above.
  • roll is a simple utility implemented as a straight-line script.
  • math is an annotated example of using nested commands and subcommands.

You can also see examples of ArgumentParser adoption among Swift project tools:

  • indexstore-db is a simple utility with two commands.
  • swift-format uses some advanced features, like custom option values and hidden flags.

Project Status

The Swift Argument Parser package is source stable; version numbers follow semantic versioning. Source breaking changes to public API can only land in a new major version.

The public API of version 1.0 of the swift-argument-parser package consists of non-underscored declarations that are marked public in the ArgumentParser module. Interfaces that aren't part of the public API may continue to change in any release, including the exact wording and formatting of the autogenerated help and error messages, as well as the package’s examples, tests, utilities, and documentation.

Future minor versions of the package may introduce changes to these rules as needed.

We'd like this package to quickly embrace Swift language and toolchain improvements that are relevant to its mandate. Accordingly, from time to time, we expect that new versions of this package will require clients to upgrade to a more recent Swift toolchain release. Requiring a new Swift release will only require a minor version bump.

Adding ArgumentParser as a Dependency

To use the ArgumentParser library in a SwiftPM project, add it to the dependencies for your package and your command-line executable target:

let package = Package(
    // name, platforms, products, etc.
    dependencies: [
        // other dependencies
        .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
    ],
    targets: [
        .executableTarget(name: "<command-line-tool>", dependencies: [
            // other dependencies
            .product(name: "ArgumentParser", package: "swift-argument-parser"),
        ]),
        // other targets
    ]
)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].