diff options
author | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-07-01 11:29:34 -0700 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2013-07-01 11:29:34 -0700 |
commit | 7e833bcc75b382cecc4b6e8f33b8def68037af82 (patch) | |
tree | ea00f9602770f40e3efc7d53dd129e94249c993c /src/compiler | |
parent | e2fbbb28fa0200d4799ed81a4c93dcb224f6564b (diff) | |
parent | 609070953cfce003fbfcf232ffbfb7fb284cda54 (diff) | |
download | scala-7e833bcc75b382cecc4b6e8f33b8def68037af82.tar.gz scala-7e833bcc75b382cecc4b6e8f33b8def68037af82.tar.bz2 scala-7e833bcc75b382cecc4b6e8f33b8def68037af82.zip |
Merge pull request #2692 from soc/SI-7591
SI-7591 Migrate command-line parsing to s.t.cmd
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/cmd/Demo.scala | 85 | ||||
-rw-r--r-- | src/compiler/scala/tools/cmd/Spec.scala | 2 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/util/CommandLine.scala | 98 |
3 files changed, 1 insertions, 184 deletions
diff --git a/src/compiler/scala/tools/cmd/Demo.scala b/src/compiler/scala/tools/cmd/Demo.scala deleted file mode 100644 index fc90140f8f..0000000000 --- a/src/compiler/scala/tools/cmd/Demo.scala +++ /dev/null @@ -1,85 +0,0 @@ -/* NEST (New Scala Test) - * Copyright 2007-2013 LAMP/EPFL - * @author Paul Phillips - */ - -package scala -package tools -package cmd - -/** A sample command specification for illustrative purposes. - * First take advantage of the meta-options: - * - * // this command creates an executable runner script "demo" - * % scala scala.tools.cmd.Demo --self-update demo - * - * // this one creates and sources a completion file - note backticks - * % `./demo --bash` - * - * // and now you have a runner with working completion - * % ./demo --<tab> - * --action --defint --int - * --bash --defstr --str - * --defenv --self-update --unary - * - * The normal option configuration is plausibly self-explanatory. - */ -trait DemoSpec extends Spec with Meta.StdOpts with Interpolation { - lazy val referenceSpec = DemoSpec - lazy val programInfo = Spec.Info("demo", "Usage: demo [<options>]", "scala.tools.cmd.Demo") - - help("""Usage: demo [<options>]""") - heading("Unary options:") - - val optIsUnary = "unary" / "a unary option" --? ; - ("action" / "a body which may be run") --> println("Hello, I am the --action body.") - - heading("Binary options:") - val optopt = "str" / "an optional String" --| - val optoptInt = ("int" / "an optional Int") . --^[Int] - val optEnv = "defenv" / "an optional String" defaultToEnv "PATH" - val optDefault = "defstr" / "an optional String" defaultTo "default" - val optDefaultInt = "defint" / "an optional Int" defaultTo -1 - val optExpand = "alias" / "an option which expands" expandTo ("--int", "15") -} - -object DemoSpec extends DemoSpec with Property { - lazy val propMapper = new PropertyMapper(DemoSpec) - - type ThisCommandLine = SpecCommandLine - def creator(args: List[String]) = - new SpecCommandLine(args) { - override def errorFn(msg: String) = { println("Error: " + msg) ; sys.exit(0) } - } -} - -class Demo(args: List[String]) extends { - val parsed = DemoSpec(args: _*) -} with DemoSpec with Instance { - import java.lang.reflect._ - - def helpMsg = DemoSpec.helpMsg - def demoSpecMethods = this.getClass.getMethods.toList - private def isDemo(m: Method) = (m.getName startsWith "opt") && !(m.getName contains "$") && (m.getParameterTypes.isEmpty) - - def demoString(ms: List[Method]) = { - val longest = ms map (_.getName.length) max - val formatStr = " %-" + longest + "s: %s" - val xs = ms map (m => formatStr.format(m.getName, m.invoke(this))) - - xs mkString ("Demo(\n ", "\n ", "\n)\n") - } - - override def toString = demoString(demoSpecMethods filter isDemo) -} - -object Demo { - def main(args: Array[String]): Unit = { - val runner = new Demo(args.toList) - - if (args.isEmpty) - println(runner.helpMsg) - - println(runner) - } -} diff --git a/src/compiler/scala/tools/cmd/Spec.scala b/src/compiler/scala/tools/cmd/Spec.scala index b761601167..a1cb31f911 100644 --- a/src/compiler/scala/tools/cmd/Spec.scala +++ b/src/compiler/scala/tools/cmd/Spec.scala @@ -15,7 +15,7 @@ trait Spec { def programInfo: Spec.Info protected def help(str: => String): Unit - protected def heading(str: => String): Unit = help("\n " + str) + protected def heading(str: => String): Unit = help(s"\n $str") type OptionMagic <: Opt.Implicit protected implicit def optionMagicAdditions(s: String): OptionMagic diff --git a/src/compiler/scala/tools/nsc/util/CommandLine.scala b/src/compiler/scala/tools/nsc/util/CommandLine.scala deleted file mode 100644 index ef28f6dc53..0000000000 --- a/src/compiler/scala/tools/nsc/util/CommandLine.scala +++ /dev/null @@ -1,98 +0,0 @@ -/* NEST (New Scala Test) - * Copyright 2007-2013 LAMP/EPFL - * @author Paul Phillips - */ - -package scala.tools -package nsc.util - -import scala.collection.mutable.ListBuffer - -/** - * XXX Note this has been completely obsolesced by scala.tools.cmd. - * I checked it back in as part of rolling partest back a month - * rather than go down the rabbit hole of unravelling dependencies. - */ -case class CommandLine( - args: List[String], - unaryArguments: List[String], - binaryArguments: List[String] -) { - def this(args: List[String]) = this(args, Nil, Nil) - def this(args: Array[String]) = this(args.toList, Nil, Nil) - def this(line: String) = this(cmd.CommandLineParser tokenize line, Nil, Nil) - - def withUnaryArgs(xs: List[String]) = copy(unaryArguments = xs) - def withBinaryArgs(xs: List[String]) = copy(binaryArguments = xs) - - def assumeBinary = true - def enforceArity = true - def onlyKnownOptions = false - - val Terminator = "--" - val ValueForUnaryOption = "true" // so if --opt is given, x(--opt) = true - - def mapForUnary(opt: String) = Map(opt -> ValueForUnaryOption) - def errorFn(msg: String) = println(msg) - - /** argMap is option -> argument (or "" if it is a unary argument) - * residualArgs are what is left after removing the options and their args. - */ - lazy val (argMap, residualArgs) = { - val residualBuffer = new ListBuffer[String] - - def stripQuotes(s: String) = { - def isQuotedBy(c: Char) = s.length > 0 && s.head == c && s.last == c - if (List('"', '\'') exists isQuotedBy) s.tail.init else s - } - - def isValidOption(s: String) = !onlyKnownOptions || (unaryArguments contains s) || (binaryArguments contains s) - def isOption(s: String) = (s startsWith "-") && (isValidOption(s) || { unknownOption(s) ; false }) - def isUnary(s: String) = isOption(s) && (unaryArguments contains s) - def isBinary(s: String) = isOption(s) && !isUnary(s) && (assumeBinary || (binaryArguments contains s)) - - def unknownOption(opt: String) = - errorFn("Option '%s' not recognized.".format(opt)) - def missingArg(opt: String, what: String) = - errorFn("Option '%s' requires argument, found %s instead.".format(opt, what)) - - def loop(args: List[String]): Map[String, String] = { - def residual(xs: List[String]) = { residualBuffer ++= xs ; Map[String, String]() } - if (args.isEmpty) return Map() - val hd :: rest = args - if (rest.isEmpty) { - if (isBinary(hd) && enforceArity) - missingArg(hd, "EOF") - - if (isOption(hd)) mapForUnary(hd) else residual(args) - } - else - if (hd == Terminator) residual(rest) - else { - val hd1 :: hd2 :: rest = args - - if (hd2 == Terminator) mapForUnary(hd1) ++ residual(rest) - else if (isUnary(hd1)) mapForUnary(hd1) ++ loop(hd2 :: rest) - else if (isBinary(hd1)) { - // Disabling this check so - // --scalacopts "-verbose" works. We can't tell if it's quoted, - // the shell does us in. - // - // if (isOption(hd2) && enforceArity) - // missingArg(hd1, hd2) - - Map(hd1 -> hd2) ++ loop(rest) - } - else { residual(List(hd1)) ++ loop(hd2 :: rest) } - } - } - - (loop(args), residualBuffer map stripQuotes toList) - } - - def isSet(arg: String) = args contains arg - def get(arg: String) = argMap get arg - def apply(arg: String) = argMap(arg) - - override def toString() = "CommandLine(\n%s)\n" format (args map (" " + _ + "\n") mkString) -} |