Swiftpack.co - Package - 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

This is not a robust full featured command line interface thing, but rather a lightweight and easy to use utility for casual making of simple command line tools. Basically, there is a Command which can execute some arguments in Cli which has its Output and any commands that you define.

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"]
            ),
            .testTarget(
                name: "MyTests",
                dependencies: ["My"]
            )
        ]
    )
    
  • Edit Sources/My/main.swift

    import AECli
    
    // MARK: - Commands
    
    struct Args: Command {
        func execute(_ arguments: [String] = [], in cli: Cli) throws {
            cli.output(arguments.description)
        }
    }
    
    struct Foo: Command {
        func execute(_ arguments: [String] = [], in cli: Cli) throws {
            cli.output("foo something")
        }
    }
    
    struct Bar: Command {
        func execute(_ arguments: [String] = [], in cli: Cli) throws {
            cli.output("bar something")
        }
    }
    
    struct Group: Command {
        var commands: [Command] {
            [Foo(), Bar()]
        }
    }
    
    struct Chain: Command {
        func execute(_ arguments: [String] = [], in cli: Cli) throws {
            try Foo().execute(in: cli)
            try Bar().execute(in: cli)
        }
    }
    
    // MARK: - Cli
    
    StandardCli(
        commands: [
            Args(), Group(), Chain()
        ]
    ).launch()
    
  • Install

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

    rm /usr/local/bin/my
    

Usage

  • Run my

    my
    

    outputs my description with all available commands

    My
    
    USAGE: 
        my command [options]
    
    COMMANDS:
        args | group | chain
    
  • Run args command with a few arguments

    my args 1 2 3
    

    outputs description of the entered arguments

    ["1", "2", "3"]
    
  • Run group command

    my group
    

    outputs group description with all available commands

    Group
    
    USAGE: 
        group command [options]
    
    COMMANDS:
        foo | bar
    
  • Run foo command

    my group foo
    

    executes foo comand

    foo something
    
  • Run chain command

    my chain
    

    executes both foo and bar comands

    foo something
    bar something
    

For the example of a more custom implementation, see AETool.

Installation

License

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

Github

link
Stars: 2

Dependencies

Used By

Total: 2