diff options
| author | Schuyler Eldridge | 2018-11-07 13:48:06 -0500 |
|---|---|---|
| committer | GitHub | 2018-11-07 13:48:06 -0500 |
| commit | 17b4e9835bd95dcf91c5ea5a4d7c52280031ea93 (patch) | |
| tree | e54301b8019d17cce4448ce9d09589815a7315d5 /src/main/scala/firrtl/options/OptionParser.scala | |
| parent | d04af59c233cec994087df3d0d3fff14e20ac04c (diff) | |
| parent | 27c1b366ce58e93434e77e964365474f5e7aa8d7 (diff) | |
Merge pull request #879 from seldridge/issue-764-refactor-pr-pointer-optionsPackage
- Adds firrtl.options package and tests
- Does not use firrtl.options in any way
Diffstat (limited to 'src/main/scala/firrtl/options/OptionParser.scala')
| -rw-r--r-- | src/main/scala/firrtl/options/OptionParser.scala | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/main/scala/firrtl/options/OptionParser.scala b/src/main/scala/firrtl/options/OptionParser.scala new file mode 100644 index 00000000..6d8095c0 --- /dev/null +++ b/src/main/scala/firrtl/options/OptionParser.scala @@ -0,0 +1,39 @@ +// See LICENSE for license details. + +package firrtl.options + +import firrtl.{FIRRTLException, AnnotationSeq} + +import scopt.OptionParser + +/** Causes an OptionParser to not call exit (call `sys.exit`) if the `--help` option is passed + */ +trait DoNotTerminateOnExit { this: OptionParser[_] => + override def terminate(exitState: Either[String, Unit]): Unit = Unit +} + +/** A modified OptionParser with mutable termination and additional checks + */ +trait DuplicateHandling extends OptionParser[AnnotationSeq] { + + override def parse(args: Seq[String], init: AnnotationSeq): Option[AnnotationSeq] = { + + /** Message for found duplicate options */ + def msg(x: String, y: String) = s"""Duplicate $x "$y" (did your custom Transform or OptionsManager add this?)""" + + val longDups = options.map(_.name).groupBy(identity).collect{ case (k, v) if v.size > 1 && k != "" => k } + val shortDups = options.map(_.shortOpt).flatten.groupBy(identity).collect{ case (k, v) if v.size > 1 => k } + + + if (longDups.nonEmpty) { + throw new OptionsException(msg("long option", longDups.map("--" + _).mkString(",")), new IllegalArgumentException) + } + + if (shortDups.nonEmpty) { + throw new OptionsException(msg("short option", shortDups.map("-" + _).mkString(",")), new IllegalArgumentException) + } + + super.parse(args, init) + } + +} |
