Skip to content

Commit

Permalink
Generate an error against unrecognized arguments
Browse files Browse the repository at this point in the history
This is based on the implementations of #34.
  • Loading branch information
ikesyo committed Dec 14, 2015
1 parent 529a814 commit 241c851
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Commandant/ArgumentParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public final class ArgumentParser {
}
}

/// Returns the remaining arguments.
internal var remainingArguments: [String]? {
return rawArguments.isEmpty ? nil : rawArguments.map { $0.description }
}

/// Returns whether the given key was enabled or disabled, or nil if it
/// was not given at all.
///
Expand Down
5 changes: 5 additions & 0 deletions Commandant/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public struct CommandWrapper<ClientError: ErrorType> {
function = command.function
run = { (arguments: ArgumentParser) -> Result<(), CommandantError<ClientError>> in
let options = C.Options.evaluate(.Arguments(arguments))

if let remainingArguments = arguments.remainingArguments {
return .Failure(unrecognizedArgumentsError(remainingArguments))
}

if let options = options.value {
command.run(options)
return .Success()
Expand Down
5 changes: 5 additions & 0 deletions Commandant/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,8 @@ internal func combineUsageErrors<ClientError>(lhs: CommandantError<ClientError>,
return lhs
}
}

/// Constructs an error that indicates unrecognized arguments remains.
internal func unrecognizedArgumentsError<ClientError>(options: [String]) -> CommandantError<ClientError> {
return .UsageError(description: "Unrecognized arguments: " + options.joinWithSeparator(", "))
}

0 comments on commit 241c851

Please # to comment.