Giter Site home page Giter Site logo

commandy's Introduction

Commandy

Commandy is Cli framework.

Usage

Implement Command

non option

enum Stash: Command {
    static func run() throws {
        print("git stash")
    }
}

option

enum Commit: String, Command {

    case message
    case allowEmpty
    
    var shortOption: String? {
        switch self {
        case .message: return "m"
        default:       return nil
        }
    }

    static func run() throws {
        let matchOptions = Commit.matchOptions
        switch matchOptions {
        case _ where matchOptions[.message]:
            print("git commit -m")
        case _ where matchOptions[.allowEmpty, .message]:
            print("git commit --allow-empty -m")
        default: break
        }
    }
}

Analysis method of command options is as follows:

definition prefix separator example word analyzed
single-word -- message --message
multiple-word -- - allowEmpty --allow-empty

Implement Cli

enum Git: String, Commandy.Cli {

    case commit
    case stash

    func run() throws {
        switch self {
        case .commit: try Commit.run()
        case .stash:  try Stash.run()
        }
    }
}

Running

try YourCli().run()

Arguments

 git commit
let command = Arguments.cached.command // Optional("commit")

 git commit -m hello_world
let nonOptionArguments = Arguments.cached.nonOptionArguments.first // Optional("hello_world")

 git commit --allow-empty -m hello_world
let options = Arguments.cached.options // ["--allow-empty", "-m"]

Installation via Swift Package Manager

.package(url: "https://github.com/atsushi130/Commandy", from: "1.0.0"),

License

Commandy is available under the MIT license. See the LICENSE file.

commandy's People

Stargazers

 avatar

Watchers

 avatar  avatar

commandy's Issues

modify Cli initialize interface

Before

public init?()

After

public init() throws
public init() throws {

    guard let command = Arguments.cached.command else {
        throw CommandyError.commandNotFound
    }
        
    let matches = Self.allCases.filter { `case` in
        return `case`.command == command || `case`.shortCommand  == command
    }
        
    guard let matchCase = matches.first else { throw CommandyError.commandNotFound }
    self = matchCase
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.