Package - Carthage/Commandant

Commandant

Commandant is a Swift framework for parsing command-line arguments, inspired by Argo (which is, in turn, inspired by the Haskell library Aeson).

Example

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

struct LogCommand: CommandProtocol {
	typealias Options = LogOptions

	let verb = "log"
	let function = "Reads the log"

	func run(_ options: Options) -> Result<(), YourErrorType> {
		// Use the parsed options to do something interesting here.
		return ()
	}
}

struct LogOptions: OptionsProtocol {
	let lines: Int
	let verbose: Bool
	let logName: String

	static func create(_ lines: Int) -> (Bool) -> (String) -> LogOptions {
		return { verbose in { logName in LogOptions(lines: lines, verbose: verbose, logName: logName) } }
	}

	static func evaluate(_ m: CommandMode) -> Result<LogOptions, CommandantError<YourErrorType>> {
		return create
			<*> m <| Option(key: "lines", defaultValue: 0, usage: "the number of lines to read from the logs")
			<*> m <| Option(key: "verbose", defaultValue: false, usage: "show verbose output")
			<*> m <| Argument(usage: "the log to read")
	}
}

Then, each available command should be added to a registry:

let commands = CommandRegistry<YourErrorType>()
commands.register(LogCommand())
commands.register(VersionCommand())

After which, arguments can be parsed by simply invoking the registry:

var arguments = CommandLine.arguments

// Remove the executable name.
assert(!arguments.isEmpty)
arguments.remove(at: 0)

if let verb = arguments.first {
	// Remove the command name.
	arguments.remove(at: 0)

	if let result = commands.run(command: verb, arguments: arguments) {
		// Handle success or failure.
	} else {
		// Unrecognized command.
	}
} else {
	// No command given.
}

For real-world examples, see the implementation of the Carthage command-line tool.

License

Commandant is released under the MIT license.

Github

link
Stars: 294

Advertisement

Releases

0.12.0 - Mar 8, 2017

Added

  • Support for arguments array in Option (#86) Thanks @Antondomashnev!

Fixed

  • Swift 3.1 compatibility, especially on Linux (#97, #99) Thanks @ikesyo!
  • Fix switch keys being ignored when a Switch contains a flag (#95, #96) Thanks @natanrolnik!

Thank you to @mdiep for reviewing pull requests!

0.11.3 - Nov 7, 2016

Fixed

  • [CI] Fix Travis deploy (#81). Thanks @jpsim!
  • [SwiftPM] Remove test dependencies from Package.swift on Linux (#83). Thanks @ikesyo!
  • [SwiftPM] Loosen Result version constraint (#84). Thanks @ikesyo!

Thank you to @mdiep for reviewing pull requests!

0.11.2 - Oct 14, 2016

  • Add missing @availables to enums to help consumers upgrade to new Swift 3 APIs.
  • Fix build issue on Linux using SPM.

Thanks @ikesyo, @NachoSoto, @norio-nomura and @mdiep for making this release possible 🎉.

0.11.1 - Sep 22, 2016

This release drops support for Swift 2.2 and 2.3.

Fixed

  • Semantic Versioning (and Swift Package Manager) compatibility (#75, 4e056dd).

Thank you to @mitsuse for filing the issue!

0.11 - Sep 21, 2016

This release drops support for Swift 2.2 and 2.3.

Added

  • Support for Swift 3.0 (#69). Thanks @norio-nomura!

Thank you to @ikesyo and @mdiep for reviewing pull requests!