Swiftpack.co - 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: 335
Help us keep the lights on

Used By

Total:

Releases

0.15.0 - Jul 20, 2018

This release drops support for Swift 3.x (#135).

Added

  • Add CocoaPods support (#132, #134). Thanks @phatblat!

Changed

  • Update Result to 4.0 (#130). Thanks @ikesyo!

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

0.14.0 - Jun 16, 2018

Added

  • Add Argument.usageParameter to improve help messages (#129) Thanks @benasher44!

Fixed

  • Improve Xcode 9.3 and Swift 4.1 support (#123, #125, #126, #127) Thanks @ikesyo!

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

0.13.0 - Feb 1, 2018

Added

  • Return self from register(_:) for method chaining (#111) Thanks @ikesyo!
  • Support strict argument values (#117) Thanks @nekrich!

Removed

  • Removed deprecated APIs (#106) Thanks @ikesyo!

Fixed

  • Use default command if options are present (#101) Thanks @xinsight!
  • Improve support for Xcode 8.3 and later & Swift 3.1 and later (#103, #109, #112, #113, #115) Thanks @ikesyo & @iosdevzone!
  • Fix scrambled commands (#118) Thanks @ikesyo!

Thank you to @mdiep for reviewing pull requests!

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!