AECli

master

Swift package for making simple command line tools
tadija/AECli

AECli

Swift package for making simple command line tools

I made this for personal use, but feel free to use it or contribute.

Intro

There is a Command which can run some arguments in Cli which has its Output and contains any defined commands. For the example of a more custom implementation, see AETool.

Example

  • Create executable Swift package and open it with Xcode

    mkdir My && cd My && swift package init --type executable && xed .
  • Edit Package.swift

    // swift-tools-version:5.1
    
    import PackageDescription
    
    let package = Package(
        name: "My",
        products: [
            .executable(name: "my", targets: ["My"])
        ],
        dependencies: [
            .package(url: "https://github.com/tadija/AECli.git", from: "0.1.0")
        ],
        targets: [
            .target(name: "My", dependencies: ["AECli"]),
        ]
    )
  • Edit Sources/My/main.swift

    import AECli
    
    // MARK: - Commands
    
    struct Thing: Command {
        var overview: String {
            "does something"
        }
        
        var commands: [Command] {
            [Foo(), Bar()]
        }
    }
    
    struct Foo: Command {
        var overview: String {
            "foo something"
        }
    
        func run(_ arguments: [String] = [], in cli: Cli) throws {
            cli.output("foo")
        }
    }
    
    struct Bar: Command {
        var overview: String {
            "bar something"
        }
    
        func run(_ arguments: [String] = [], in cli: Cli) throws {
            cli.output("bar")
        }
    }
    
    // MARK: - Cli
    
    SimpleCli(
        overview: "my cli",
        commands: [Thing()],
        help: "USAGE: command [options]"
    ).launch()
  • Install

    swift build -c release && install .build/release/my /usr/local/bin/my
  • Uninstall

    rm /usr/local/bin/my

Usage

  • Run my

    my
    

    describes cli with all available commands

    my cli
    
    • thing > does something
    
    USAGE: command [options]
  • Run thing command

    my thing

    describes command with all its subcommands

    does something
    
    • foo > foo something
    • bar > bar something
  • Run foo command

    my thing foo

    runs foo comand

    foo

Installation

License

This code is released under the MIT license. See LICENSE for details.

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Mar 17 2024 12:00:15 GMT-0900 (Hawaii-Aleutian Daylight Time)