Skip to content

atsushi130/Commandy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.