Skip to content

Commit

Permalink
Merge pull request #147 from Carthage/swiftlint
Browse files Browse the repository at this point in the history
[lint] Add .swiftlint.yml, .hound.yml and address some violations
  • Loading branch information
ikesyo authored Apr 6, 2019
2 parents 191b3f7 + b0ecd1c commit 9a2af47
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
swiftlint:
config_file: .swiftlint.yml
13 changes: 13 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
included:
- Sources
- Tests

disabled_rules:
- function_body_length
- identifier_name
- line_length
- opening_brace
- operator_whitespace

trailing_comma:
mandatory_comma: true
16 changes: 8 additions & 8 deletions Sources/Commandant/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import Result

/// Represents a subcommand that can be executed with its own set of arguments.
public protocol CommandProtocol {

/// The command's options type.
associatedtype Options: OptionsProtocol

associatedtype ClientError where ClientError == Options.ClientError

/// The action that users should specify to use this subcommand (e.g.,
/// `help`).
var verb: String { get }
Expand All @@ -33,9 +33,9 @@ public protocol CommandProtocol {
public struct CommandWrapper<ClientError: Error> {
public let verb: String
public let function: String

public let run: (ArgumentParser) -> Result<(), CommandantError<ClientError>>

public let usage: () -> CommandantError<ClientError>?

/// Creates a command that wraps another.
Expand Down Expand Up @@ -134,10 +134,10 @@ extension CommandRegistry {
/// If a matching command could not be found or a usage error occurred,
/// a helpful error message will be written to `stderr`, then the process
/// will exit with a failure error code.
public func main(defaultVerb: String, errorHandler: (ClientError) -> ()) -> Never {
public func main(defaultVerb: String, errorHandler: (ClientError) -> Void) -> Never {
main(arguments: CommandLine.arguments, defaultVerb: defaultVerb, errorHandler: errorHandler)
}

/// Hands off execution to the CommandRegistry, by parsing `arguments`
/// and then running whichever command has been identified in the argument
/// list.
Expand All @@ -155,7 +155,7 @@ extension CommandRegistry {
/// If a matching command could not be found or a usage error occurred,
/// a helpful error message will be written to `stderr`, then the process
/// will exit with a failure error code.
public func main(arguments: [String], defaultVerb: String, errorHandler: (ClientError) -> ()) -> Never {
public func main(arguments: [String], defaultVerb: String, errorHandler: (ClientError) -> Void) -> Never {
assert(arguments.count >= 1)

var arguments = arguments
Expand All @@ -170,7 +170,7 @@ extension CommandRegistry {
// Remove the command name.
arguments.remove(at: 0)
}

switch run(command: verb, arguments: arguments) {
case .success?:
exit(EXIT_SUCCESS)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Commandant/HelpCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct HelpCommand<ClientError: Error>: CommandProtocol {

public let verb = "help"
public let function: String

private let registry: CommandRegistry<ClientError>

/// Initializes the command to provide help from the given registry of
Expand Down Expand Up @@ -61,7 +61,7 @@ public struct HelpCommand<ClientError: Error>: CommandProtocol {

public struct HelpOptions<ClientError: Error>: OptionsProtocol {
fileprivate let verb: String?

private init(verb: String?) {
self.verb = verb
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Commandant/Option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public protocol OptionsProtocol {
/// An `OptionsProtocol` that has no options.
public struct NoOptions<ClientError: Error>: OptionsProtocol {
public init() {}

public static func evaluate(_ m: CommandMode) -> Result<NoOptions, CommandantError<ClientError>> {
return .success(NoOptions())
}
Expand Down Expand Up @@ -184,7 +184,7 @@ extension CommandMode {
if let value = T.from(string: stringValue) {
return .success(value)
}

let description = "Invalid value for '--\(key)': \(stringValue)"
return .failure(.usageError(description: description))
} else {
Expand Down
14 changes: 7 additions & 7 deletions Tests/CommandantTests/OptionSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,34 @@ class OptionsProtocolSpec: QuickSpec {

it("should succeed with some strings array arguments separated by comma") {
let value = tryArguments("required", "--intValue", "3", "--optionalStringValue", "baz", "fuzzbuzz", "--stringsArray", "a,b,c").value
let expected = TestOptions(intValue: 3, stringValue: "foobar", stringsArray: ["a","b","c"], optionalStringsArray: nil, optionalStringValue: "baz", optionalFilename: "fuzzbuzz", requiredName: "required", enabled: false, force: false, glob: false, arguments: [])
let expected = TestOptions(intValue: 3, stringValue: "foobar", stringsArray: ["a", "b", "c"], optionalStringsArray: nil, optionalStringValue: "baz", optionalFilename: "fuzzbuzz", requiredName: "required", enabled: false, force: false, glob: false, arguments: [])
expect(value).to(equal(expected))
}

it("should succeed with some strings array arguments separated by space") {
let value = tryArguments("required", "--intValue", "3", "--optionalStringValue", "baz", "--stringsArray", "a b c", "fuzzbuzz").value
let expected = TestOptions(intValue: 3, stringValue: "foobar", stringsArray: ["a", "b", "c"], optionalStringsArray: nil, optionalStringValue: "baz", optionalFilename: "fuzzbuzz", requiredName: "required", enabled: false, force: false, glob: false, arguments: [])
expect(value).to(equal(expected))
}

it("should succeed with some strings array arguments separated by comma and space") {
let value = tryArguments("required", "--intValue", "3", "--optionalStringValue", "baz", "--stringsArray", "a, b, c", "fuzzbuzz").value
let expected = TestOptions(intValue: 3, stringValue: "foobar", stringsArray: ["a", "b", "c"], optionalStringsArray: nil, optionalStringValue: "baz", optionalFilename: "fuzzbuzz", requiredName: "required", enabled: false, force: false, glob: false, arguments: [])
expect(value).to(equal(expected))
}

it("should succeed with some optional string arguments") {
let value = tryArguments("required", "--intValue", "3", "--optionalStringValue", "baz", "fuzzbuzz").value
let expected = TestOptions(intValue: 3, stringValue: "foobar", stringsArray: [], optionalStringsArray: nil, optionalStringValue: "baz", optionalFilename: "fuzzbuzz", requiredName: "required", enabled: false, force: false, glob: false, arguments: [])
expect(value).to(equal(expected))
}

it("should succeed without optional array arguments") {
let value = tryArguments("required").value
let expected = TestOptions(intValue: 42, stringValue: "foobar", stringsArray: [], optionalStringsArray: nil, optionalStringValue: nil, optionalFilename: "filename", requiredName: "required", enabled: false, force: false, glob: false, arguments: [])
expect(value).to(equal(expected))
}

it("should succeed with some optional array arguments") {
let value = tryArguments("required", "--intValue", "3", "--optionalStringsArray", "one, two", "fuzzbuzz").value
let expected = TestOptions(intValue: 3, stringValue: "foobar", stringsArray: [], optionalStringsArray: ["one", "two"], optionalStringValue: nil, optionalFilename: "fuzzbuzz", requiredName: "required", enabled: false, force: false, glob: false, arguments: [])
Expand Down Expand Up @@ -178,7 +178,7 @@ func ==(lhs: TestOptions, rhs: TestOptions) -> Bool {
}

func ==<T: Equatable>(lhs: [T]?, rhs: [T]?) -> Bool {
switch (lhs,rhs) {
switch (lhs, rhs) {
case let (lhs?, rhs?):
return lhs == rhs
case (nil, nil):
Expand Down
38 changes: 19 additions & 19 deletions Tests/CommandantTests/OptionsWithEnumProtocolSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class OptionsWithEnumProtocolSpec: QuickSpec {
func tryArguments(_ arguments: String...) -> Result<TestEnumOptions, CommandantError<NoError>> {
return TestEnumOptions.evaluate(.arguments(ArgumentParser(arguments)))
}

it("should fail if a required argument is missing") {
expect(tryArguments().value).to(beNil())
}
Expand All @@ -30,78 +30,78 @@ class OptionsWithEnumProtocolSpec: QuickSpec {
it("should fail if an option is missing a value") {
expect(tryArguments("required", "--strictStringValue", "drop").value).to(beNil())
}

it("should fail if an optional strict int parameter is wrong") {
expect(tryArguments("required", "256").value).to(beNil())
}

it("should succeed without optional string arguments") {
let value = tryArguments("required").value
let expected = TestEnumOptions(strictIntValue: .theAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything, strictStringValue: .foobar, strictStringsArray: [], optionalStrictStringsArray: nil, optionalStrictStringValue: nil, optionalStrictInt: .min, requiredName: "required", arguments: [])
expect(value).to(equal(expected))
}

it("should succeed without optional strict int value") {
let value = tryArguments("required", "5").value
let expected = TestEnumOptions(strictIntValue: .theAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything, strictStringValue: .foobar, strictStringsArray: [], optionalStrictStringsArray: nil, optionalStrictStringValue: nil, optionalStrictInt: .giveFive, requiredName: "required", arguments: [])
expect(value).to(equal(expected))
}

it("should succeed with some strings array arguments separated by comma") {
let value = tryArguments("required", "--strictIntValue", "3", "--optionalStrictStringValue", "baz", "255", "--strictStringsArray", "a,b,c").value
let expected = TestEnumOptions(strictIntValue: .three, strictStringValue: .foobar, strictStringsArray: [.a, .b, .c], optionalStrictStringsArray: nil, optionalStrictStringValue: .baz, optionalStrictInt: .max, requiredName: "required", arguments: [])
expect(value).to(equal(expected))
}

it("should succeed with some strings array arguments separated by space") {
let value = tryArguments("required", "--strictIntValue", "3", "--optionalStrictStringValue", "baz", "--strictStringsArray", "a b c", "255").value
let expected = TestEnumOptions(strictIntValue: .three, strictStringValue: .foobar, strictStringsArray: [.a, .b, .c], optionalStrictStringsArray: nil, optionalStrictStringValue: .baz, optionalStrictInt: .max, requiredName: "required", arguments: [])
expect(value).to(equal(expected))
}

it("should succeed with some strings array arguments separated by comma and space") {
let value = tryArguments("required", "--strictIntValue", "3", "--optionalStrictStringValue", "baz", "--strictStringsArray", "a, b, c", "255").value
let expected = TestEnumOptions(strictIntValue: .three, strictStringValue: .foobar, strictStringsArray: [.a, .b, .c], optionalStrictStringsArray: nil, optionalStrictStringValue: .baz, optionalStrictInt: .max, requiredName: "required", arguments: [])
expect(value).to(equal(expected))
}

it("should succeed with some optional string arguments") {
let value = tryArguments("required", "--strictIntValue", "3", "--optionalStrictStringValue", "baz", "255").value
let expected = TestEnumOptions(strictIntValue: .three, strictStringValue: .foobar, strictStringsArray: [], optionalStrictStringsArray: nil, optionalStrictStringValue: .baz, optionalStrictInt: .max, requiredName: "required", arguments: [])
expect(value).to(equal(expected))
}

it("should succeed without optional array arguments") {
let value = tryArguments("required").value
let expected = TestEnumOptions(strictIntValue: .theAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything, strictStringValue: .foobar, strictStringsArray: [], optionalStrictStringsArray: nil, optionalStrictStringValue: nil, optionalStrictInt: .min, requiredName: "required", arguments: [])
expect(value).to(equal(expected))
}

it("should succeed with some optional array arguments") {
let value = tryArguments("required", "--strictIntValue", "3", "--optionalStrictStringsArray", "one, two", "255").value
let expected = TestEnumOptions(strictIntValue: .three, strictStringValue: .foobar, strictStringsArray: [], optionalStrictStringsArray: [.one, .two], optionalStrictStringValue: nil, optionalStrictInt: .max, requiredName: "required", arguments: [])
expect(value).to(equal(expected))
}

it("should override previous optional arguments") {
let value = tryArguments("required", "--strictIntValue", "3", "--strictStringValue", "fuzzbuzz", "--strictIntValue", "5", "--strictStringValue", "bazbuzz").value
let expected = TestEnumOptions(strictIntValue: .giveFive, strictStringValue: .bazbuzz, strictStringsArray: [], optionalStrictStringsArray: nil, optionalStrictStringValue: nil, optionalStrictInt: .min, requiredName: "required", arguments: [])
expect(value).to(equal(expected))
}

it("should consume the rest of positional arguments") {
let value = tryArguments("required", "255", "value1", "value2").value
let expected = TestEnumOptions(strictIntValue: .theAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything, strictStringValue: .foobar, strictStringsArray: [], optionalStrictStringsArray: nil, optionalStrictStringValue: nil, optionalStrictInt: .max, requiredName: "required", arguments: [ "value1", "value2" ])
expect(value).to(equal(expected))
}

it("should treat -- as the end of valued options") {
let value = tryArguments("--", "--strictIntValue").value
let expected = TestEnumOptions(strictIntValue: .theAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything, strictStringValue: .foobar, strictStringsArray: [], optionalStrictStringsArray: nil, optionalStrictStringValue: nil, optionalStrictInt: .min, requiredName: "--strictIntValue", arguments: [])
expect(value).to(equal(expected))
}
}

describe("CommandMode.Usage") {
it("should return an error containing usage information") {
let error = TestEnumOptions.evaluate(.usage).error
Expand All @@ -123,15 +123,15 @@ struct TestEnumOptions: OptionsProtocol, Equatable {
let optionalStrictInt: StrictIntValue
let requiredName: String
let arguments: [String]

typealias ClientError = NoError

static func create(_ a: StrictIntValue) -> (StrictStringValue) -> ([StrictStringValue]) -> ([StrictStringValue]?) -> (StrictStringValue?) -> (String) -> (StrictIntValue) -> ([String]) -> TestEnumOptions {
return { b in { c in { d in { e in { f in { g in { h in
return self.init(strictIntValue: a, strictStringValue: b, strictStringsArray: c, optionalStrictStringsArray: d, optionalStrictStringValue: e, optionalStrictInt: g, requiredName: f, arguments: h)
} } } } } } }
}

static func evaluate(_ m: CommandMode) -> Result<TestEnumOptions, CommandantError<NoError>> {
return create
<*> m <| Option(key: "strictIntValue", defaultValue: .theAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything, usage: "`0` - zero, `255` - max, `3` - three, `5` - five or `42` - The Answer")
Expand All @@ -157,7 +157,7 @@ extension TestEnumOptions: CustomStringConvertible {

enum StrictStringValue: String, ArgumentProtocol {
static var name: String = "Strict string value: `foobar`, `bazbuzz`, `one`, `two`, `baz`, `a`, `b` or `c`"

case foobar
case bazbuzz
case one
Expand All @@ -170,7 +170,7 @@ enum StrictStringValue: String, ArgumentProtocol {

enum StrictIntValue: UInt8, ArgumentProtocol {
static var name: String = "Strict int value: `3`, `5`, `42`, `0`, `255`"

case min = 0
case three = 3
case giveFive = 5
Expand Down

0 comments on commit 9a2af47

Please sign in to comment.