Skip to content
Command line argument parser written in Swift.
Swift
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Assets
Sources
Tests
.gitignore
.travis.yml
LICENSE
Package.swift
README.md

README.md

Bouncer
Build Status GitHub release Swift Package Manager license

Command line argument parser. Bouncer does all the heavy lifting for you. Dive into business logic in no time.

Table of Contents

Synopsis

Parse

git-mock init . -q --bare --separate-git-dir=../git_dir --shared --template ../template/git-template

and grab values.

let directory         = operands[optional: 0]
let quiet             = optionValues.have(quietOption)
let bare              = optionValues.have(bareOption)
let templateDirectory = optionValues.findOptionalArgument(for: templateOption)
let gitDirectory      = optionValues.findOptionalArgument(for: separateGitDirOption)
let shared            = optionValues.findOptionalArgument(for: sharedOption)

Installation

Add Bouncer to Package.swift.

dependencies: [
    .package(url: "https://github.com/flintbox/Bouncer", from: "0.1.2")
]

Now import!

import Bouncer

let program = Program(commands: [])

Component

Option

Configuration for option like --path, -h, --path ./temp or -p./temp. Check regular expressions for matching option in OptionNameRegex.swift.

Property Type Description
name String Name of option ([[:alnum:]\-]+).
shortName Character? Short name of option.
optional Bool If option is optional or not for the command. Used for validation.
argumentType OptionArgumentType Option argument type. None, optional, optional with default value or required.

Operand

Basically, all of arguments following command are operands. Of course, except options and option arguments.

Command

Configuration for command.

Property Type Description
name [String] Name of the command. Sub command can be easily expressed like ["container", "start"].
operandType OperandType Operand type. Define command accepts how many operands.
options [Option] Available options.
handler CommandHandler This block will be called with validated operands and option values. There are extensions for getting specific values from operand value array and option value array.

Program

Program is initialized with commands. Use program object to parse and run command.

Example

Init.swift

import Bouncer

let quietOption =          Option(name: "quiet", shortName: "q", optional: true, argumentType: .none)
let bareOption =           Option(name: "bare", optional: true, argumentType: .none)
let templateOption =       Option(name: "template", optional: true, argumentType: .required)
let separateGitDirOption = Option(name: "separate-git-dir", optional: true, argumentType: .required)
let sharedOption =         Option(name: "shared", optional: true, argumentType: .optional("group"))

let initCommand = Command(
    name: ["init"],
    operandType: .optionalEqual(1),
    options: [quietOption, bareOption, templateOption, separateGitDirOption, sharedOption]
) { program, command, operands, optionValues in
    let directory = operands[optional: 0]
    let quiet = optionValues.have(quietOption)
    let bare = optionValues.have(bareOption)
    let templateDirectory = optionValues.findOptionalArgument(for: templateOption)
    let gitDirectory = optionValues.findOptionalArgument(for: separateGitDirOption)
    let shared = optionValues.findOptionalArgument(for: sharedOption)
    print(
        """

            init command <https://git-scm.com/docs/git-init>

            directory    : \(directory ?? "nil")
            quiet        : \(quiet)
            bare         : \(bare)
            template dir : \(templateDirectory ?? "nil")
            git dir      : \(gitDirectory ?? "nil")
            shared       : \(shared ?? "nil")

        """
    )
}

main.swift

import Bouncer

let program = Program(commands: [initCommand])

let arguments = Array(CommandLine.arguments.dropFirst())
try? program.run(withArguments: arguments)

Case 1

Input

git-mock init .

Output

init command <https://git-scm.com/docs/git-init>

directory    : .
quiet        : false
bare         : false
template dir : nil
git dir      : nil
shared       : nil

Case 2

Input

git-mock init repository/dir --shared --template=../template --separate-git-dir ../.git --bare

Output

init command <https://git-scm.com/docs/git-init>

directory    : repository/dir
quiet        : false
bare         : true
template dir : ../template
git dir      : ../.git
shared       : group

Contribute

If you have good idea or suggestion? Please, don't hesitate to open a pull request or send me an email.

Hope you enjoy building command line tool with Bouncer!

You can’t perform that action at this time.