Skip to content

Commit

Permalink
Leave single '-' in user arguments
Browse files Browse the repository at this point in the history
These can mean stdin for some apps for example.
  • Loading branch information
alexarchambault committed Jun 17, 2021
1 parent d4709f1 commit bf5a112
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/shared/src/main/scala/caseapp/core/parser/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ abstract class Parser[T] {
}
val reverseSteps0 = Step.DoubleDash(index) :: reverseSteps.reverse
(res, reverseSteps0.reverse)
case opt :: rem if opt.startsWith("-") =>
case opt :: rem if opt.startsWith("-") && opt != "-" =>
if (ignoreUnrecognized)
helper(current, rem, opt :: extraArgsReverse, Step.IgnoredUnrecognized(index) :: reverseSteps, index + 1)
else if (stopAtFirstUnrecognized) {
Expand Down Expand Up @@ -282,6 +282,8 @@ abstract class Parser[T] {
completer.optionName(value, stateOpt)
case Step.Unrecognized(_, _) =>
completer.optionName(value, stateOpt)
case Step.StandardArgument(idx) if args0(idx) == "-" =>
completer.optionName(value, stateOpt)
case Step.MatchedOption(_, consumed, arg) if shift == 0 =>
completer.optionName(value, stateOpt)
case Step.MatchedOption(_, consumed, arg) if consumed == 2 && shift == 1 =>
Expand Down
17 changes: 17 additions & 0 deletions tests/shared/src/test/scala/caseapp/ParserTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,22 @@ object ParserTests extends TestSuite {
assert(res.isLeft)
}
}
test("Keep single dashes in user arguments") {
case class Helper(n: Int, value: String)
val parser =
Argument[Int](Arg("n")) ::
Argument[String](Arg("value")).withDefault(() => Some("default")) ::
NilParser
test("single") {
val res = parser.to[Helper].parse(Seq("-n", "2", "-"))
val expected = Right((Helper(2, "default"), Seq("-")))
assert(res == expected)
}
test("several") {
val res = parser.to[Helper].parse(Seq("-", "a", "-", "-n", "2", "-"))
val expected = Right((Helper(2, "default"), Seq("-", "a", "-", "-")))
assert(res == expected)
}
}
}
}

0 comments on commit bf5a112

Please sign in to comment.