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).

Reviewed by Hound

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: 365
Help us keep the lights on

Dependencies

Releases

0.17.0 - Apr 13, 2019

This release drops support for Swift 4.2. Only Swift 5 is now supported.

  • Remove Result dependency (#146, #151). Thanks @daltonclaybrook & @giginet!

Thank you @mdiep & @giginet for reviewing pull requests!

0.16.0 - Apr 8, 2019

This release drops support for Swift 4.0/4.1 (#142) and is likely the last release to support Swift 4.2.

Added

  • Allow setting the help command's description (#136). Thanks @Pyroh!
  • Support Swift 4.2 (#137). Thanks @ikesyo!
  • Support Swift 5.0 (#142). Thanks @norio-nomura!

Thank you to @mdiep, @ikesyo, @blender and @norio-nomura for reviewing pull requests!

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!