Skip to content

Commit

Permalink
fix: store flags as ordered
Browse files Browse the repository at this point in the history
Fixes #78
  • Loading branch information
kylef committed Sep 23, 2019
1 parent e725aee commit f2ac63b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Commander Changelog

## Master

### Bug Fixes

- Fixed ordering of flags in the "Unknown Arguments" help output of a command.
Flags have previously been stored in an un orderered set and thus upon
printing them back out their order was not persisted and a flag part such as
`-user` can be printed as `-usre`.
[#78](https://github.com/kylef/Commander/issues/78)

## 0.9.0 (2019-06-12)

### Breaking
Expand Down
7 changes: 3 additions & 4 deletions Sources/Commander/ArgumentParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ private enum Arg : CustomStringConvertible, Equatable {
case option(String)

/// A flag
case flag(Set<Character>)
case flag([Character])

var description:String {
switch self {
Expand Down Expand Up @@ -78,8 +78,7 @@ public final class ArgumentParser : ArgumentConvertible, CustomStringConvertible
let option = flags[flags.index(after: flags.startIndex)..<flags.endIndex]
return .option(String(option))
}

return .flag(Set(flags))
return .flag(Array(String(flags)))
}

return .argument(argument)
Expand Down Expand Up @@ -194,7 +193,7 @@ public final class ArgumentParser : ArgumentConvertible, CustomStringConvertible
case .flag(let option):
var options = option
if options.contains(flag) {
options.remove(flag)
options.removeAll(where: { $0 == flag })
arguments.remove(at: index)

if !options.isEmpty {
Expand Down
8 changes: 8 additions & 0 deletions Tests/CommanderTests/ArgumentParserSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,12 @@ let testArgumentParser: ((ContextType) -> Void) = {
try expect(value2) == "value2"
}
}

$0.describe("#description") {
$0.it("should maintain flag ordering") {
let parser = ArgumentParser(arguments: ["-user", "-one"])

try expect(parser.description) == "-user -one"
}
}
}

0 comments on commit f2ac63b

Please sign in to comment.