Commander

0.4.4

🚀The framework to write type-safe and structured command line program easily in Swift.
devedbox/Commander

What's New

v0.4.4

2018-11-06T11:45:10Z

Feats:

  • Added built-in command list to list the subcommands and options of given command.
  • Added built-in command complete to generate and execute the completion scripts for bash and zsh both.

Fixes:

  • Fixed and make the help options with extra available options an std error

logo Commander

testcodecovlicenselangMaintainabilityTest Coverage

Commander is a Swift framework for decoding command-line arguments by integrating with Swift Standard Library Protocols e.g. Encodable & Decodable. With Commander, you just need to focus on writing options model of commands, the rest works will be handled by Commander.

Test Coverage Graph

coverage graph

Example

With Commander, a command and its associated options could be defined as follows:

import Commander

public struct SampleCommand: CommandRepresentable {
  public struct Options: OptionsRepresentable {
    public typealias ArgumentsResolver = AnyArgumentsResolver<String>
    public enum CodingKeys: String, CodingKeysRepresentable {
      case verbose = "verbose"
      case stringValue = "string-value"
    }

    public static let keys: [Options.CodingKeys : Character] = [
      .verbose: "v",
      .stringValue: "s"
    ]

    public static let descriptions: [Options.CodingKeys : OptionDescription] = [
      .verbose: .usage("Prints the logs of the command"),
      .stringValue: .usage("Pass a value of String to the command")
    ]

    public var verbose: Bool = false
    public var stringValue: String = ""
  }

  public static let symbol: String = "sample"
  public static let usage: String = "Show sample usage of commander"

  public static func main(_ options: Options) throws {
    print(options)
    print("arguments: \(options.arguments)")
    print("\n\n\(Options.CodingKeys.stringValue.stringValue)")
  }
}

Then, configuring the available commands would like this:

import Commander

Commander.commands = [
  SampleCommand.self,
  NoArgsCommand.self
]
Commander.usage = "The sample usage command of 'Commander'"
Commander().dispatch() // Call this to dispatch and run the command

After which, arguments can be resolved by declaration of ArgumentsResolver:

public typealias ArgumentsResolver = AnyArgumentsResolver<T> // T must be Decodable

And you can fetch the arguments by:

public static func main(_ options: Options) throws {
  print("arguments: \(options.arguments)") // 'arguments' is being declared in OptionsRepresentable 
}

It's easy and fun!!!

License

Commander is released under the MIT license.

Description

  • Swift Tools 4.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Oct 20 2024 07:29:50 GMT-0900 (Hawaii-Aleutian Daylight Time)