Swiftpack.co - eneko/CommandRegistry as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by eneko.
eneko/CommandRegistry 0.4.0
⚠️DEPRECATED⚠️ Beautifully handle subcommands on your SwiftPM command line tool
⭐️ 6
🕓 3 years ago
.package(url: "https://github.com/eneko/CommandRegistry.git", from: "0.4.0")

CommandRegistry 💻

Beautifully handle subcommands in your SwiftPM command line tool.

Release Swift 5.0+ Build Status codecov Swift Package Manager Compatible Linux Compatible

$ mytool
OVERVIEW: My awesome command line tool

USAGE: mytool <subcommand> <options>

SUBCOMMANDS:
  commandA     Does something awesome
  commandB     Does something awesome in a different way

⚠️ Deprecation Notice ⚠️

This package will be no longer maintained. If you were using it in one of your projects, I recommend you switch to the official Swift Argument Parser package maintained by Apple.

Usage 🛠

Keep your main.swift nice and tidy:

import CommandRegistry

var program = CommandRegistry(usage: "<subcommand> <options>", overview: "My awesome command line tool")
program.register(command: CommandA.self)
program.run()

Define your subcommands ⌨️

import CommandRegistry

class CommandA: Command {
    let command = "commandA"
    let overview = "Does something awesome"

    let subparser: ArgumentParser
    var subcommands: [Command] = []

    required init(parser: ArgumentParser) {
        subparser = parser.add(subparser: command, overview: overview)
    }

    func run(with arguments: ArgumentParser.Result) throws {
        print("This is CommandA")
    }
}
$ swift run mytool commandA
This is CommandA

Easily define and process strongly-typed command arguments 😎

import CommandRegistry

var program = CommandRegistry(usage: "<subcommand> <options>", overview: "My awesome command line tool")
program.register(command: CommandA.self)
program.register(command: CommandB.self) // <-- New command
program.run()
import SPMUtility
import CommandRegistry

class CommandB: Command {
    let command = "commandB"
    let overview = "Does something awesome in a different way"

    let subparser: ArgumentParser
    var subcommands: [Command] = []

    // Define an optional (non-required) integer argument
    private var numberArgument: OptionArgument<Int>

    required init(parser: ArgumentParser) {
        subparser = parser.add(subparser: command, overview: overview)
        numberArgument = subparser.add(option: "--number", shortName: "-n", kind: Int.self, usage: "Number argument (optional)")
    }

    func run(with arguments: ArgumentParser.Result) throws {
        print("This is CommandB")
        if let number = arguments.get(numberArgument) {
            print("The number entered is \(number), it's square is \(number * number).")
        }
        else {
            print("You didn't enter any number. That is ok too.")
        }
    }
}
$ mytool commandB -n 10
This is commandB
The number entered is 10, it's square is 100.

Auto-generated --help 👌

By using CommandRegistry you get automatically generated help for your command line tool and all its subcommands

$ mytool --help
OVERVIEW: My awesome command line tool

USAGE: mytool <subcommand> <options>

SUBCOMMANDS:
  commandA     Does something awesome
  commandB     Does something awesome in a different way
$ mytool commandB --help
OVERVIEW: Does something awesome in a different way

OPTIONS:
  --number, -n   Number argument (optional)

Auto-generated --version 🔢

import CommandRegistry

var program = CommandRegistry(usage: "<subcommand> <options>", overview: "My awesome command line tool")
program.version = "1.0.1"
program.register(command: CommandA.self)
program.register(command: CommandB.self)
program.run()
$ mytool --version
1.0.1

Installation 🚀

  1. Add both CommandRegstry and SwiftPM depedencies to your Package.swift:
dependencies([
    .package(url: "https://github.com/eneko/CommandRegistry.git", from: "0.0.1"),
    .package(url: "https://github.com/apple/swift-package-manager.git", from: "0.1.0"),
]),
  1. Update your target dependencies in Package.swift:
.target(name: "mytool", dependencies: ["CommandRegistry", "Utility"]),
  1. Add import CommandRegistry and import SPMUtility in your source files, as needed.

Why yet another argument and command parsing library? 🤔

When I started writing command line tools in Swift using Swift Package Manager, I found several packages that provided functionality to parse command line arguments, flags and subcommands. However, I found these libraries complex and hard to use.

Then I realized Swift Package Manager already provided this and many other common functionality that most command line tools normaly need. Thus, CommandRegistry is not "yet another argument and command parsing library", but a thin layer built on top of Swift Package Manager ArgumentParser class.

I'd recommend you take a look at all the classes and types included with Swift Package Manager (the Utility module could be a good strting point), and start using those in your projects.

GitHub

link
Stars: 6
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Release 0.4.0
4 years ago
  • Dependency updates

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics