Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adding options for boolean flags to have --no-<arg> #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,16 @@ class CommandLineProgramParser[T](val targetClass: Class[T], val includeSpecialA
*/
private def parseArgs(args: Seq[String]): ParseResult = {
val parser: OptionParser = new OptionParser(argFilePrefix=Some("@"))
val alternateFlagNames = scala.collection.mutable.HashMap[String, String]()

try {
// Add to the option parsers
this.argumentLookup.ordered.filterNot(_.hidden).foreach {
case arg if arg.isFlag => parser.acceptFlag( arg.names: _*)
case arg if arg.isFlag =>
parser.acceptFlag( arg.names: _*)
val noName = f"no-${arg.longName}"
parser.acceptFlag(noName)
alternateFlagNames.addOne(noName -> arg.names.head)
case arg if !arg.isCollection => parser.acceptSingleValue( arg.names: _*)
case arg => parser.acceptMultipleValues(arg.names: _*)
}
Expand All @@ -181,7 +186,16 @@ class CommandLineProgramParser[T](val targetClass: Class[T], val includeSpecialA
// set the values
val parseResults: Iterable[ParseResult] = parser.map {
case (name: String, values: List[String]) =>
this.argumentLookup.forArg(name).foreach(arg => arg.setArgument(values:_*))
alternateFlagNames.get(name) match {
case Some(_name) =>
this.argumentLookup.forArg(_name).foreach { arg =>
arg.setArgument(values: _*)
arg.value = !arg.value.contains(true)
}
case None =>
this.argumentLookup.forArg(name).foreach(arg => arg.setArgument(values:_*))
}

ParseSuccess()
case _ =>
ParseFailure(ex=new IllegalStateException("Parser returned an unexpected set of values"), parser.remaining)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,12 +978,25 @@ with CommandLineParserStrings with CaptureSystemStreams with BeforeAndAfterAll {
}

it should "accept flags with a combination of arguments and no arguments" in {
val args = Seq[String]("--flag1", "T", "--flag2")
val p = parser(classOf[FlagClass])
inside (p.parseAndBuild(args=args)) { case ParseSuccess() => }
val task = p.instance.get
task.flag1 shouldBe true
task.flag2 shouldBe true
Seq("T", "F").foreach { flag1Value =>
val args = Seq[String]("--flag1", flag1Value, "--flag2")
val p = parser(classOf[FlagClass])
inside(p.parseAndBuild(args = args)) { case ParseSuccess() => }
val task = p.instance.get
task.flag1 shouldBe flag1Value == "T"
task.flag2 shouldBe true
}
}

it should "accept flags with --no-<arg>" in {
Seq("T", "F").foreach { flag1Value =>
val args = Seq[String]("--no-flag1", flag1Value, "--no-flag2")
val p = parser(classOf[FlagClass])
inside(p.parseAndBuild(args = args)) { case ParseSuccess() => }
val task = p.instance.get
task.flag1 shouldBe flag1Value == "F"
task.flag2 shouldBe false
}
}

it should "accept setting private arguments" in {
Expand Down