diff options
author | Paul Phillips <paulp@improving.org> | 2010-04-11 00:32:00 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-04-11 00:32:00 +0000 |
commit | e93c1a93a2c8a40265b34bb9f1dd61b9470c908d (patch) | |
tree | b9f13ffbe1d1ef59380e32f74380f9797b7fd2cb /src/compiler/scala/tools/cmd/FromString.scala | |
parent | 71b6aca681ab697304590a96b13847b9bba141dc (diff) | |
download | scala-e93c1a93a2c8a40265b34bb9f1dd61b9470c908d.tar.gz scala-e93c1a93a2c8a40265b34bb9f1dd61b9470c908d.tar.bz2 scala-e93c1a93a2c8a40265b34bb9f1dd61b9470c908d.zip |
Introduces scala.tools.cmd providing command li...
Introduces scala.tools.cmd providing command line tool infrastructure.
For a quick look at what can be done, see
scala.tools.cmd.Demo
For a more involved, potentially eye-straining look, see
scala.tools.partest.PartestSpec
To experience it through the eyes of Joe Partest User, run
test/partest
Review by community.
Diffstat (limited to 'src/compiler/scala/tools/cmd/FromString.scala')
-rw-r--r-- | src/compiler/scala/tools/cmd/FromString.scala | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/cmd/FromString.scala b/src/compiler/scala/tools/cmd/FromString.scala new file mode 100644 index 0000000000..81454e7a30 --- /dev/null +++ b/src/compiler/scala/tools/cmd/FromString.scala @@ -0,0 +1,72 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2010 LAMP/EPFL + * @author Paul Phillips + */ + +package scala.tools +package cmd + +import nsc.io.{ Path, File, Directory } +import reflect.OptManifest + +/** A general mechanism for defining how a command line argument + * (always a String) is transformed into an arbitrary type. A few + * example instances are in the companion object, but in general + * either IntFromString will suffice or you'll want custom transformers. + */ +abstract class FromString[+T](implicit m: OptManifest[T]) extends PartialFunction[String, T] { + def apply(s: String): T + def isDefinedAt(s: String): Boolean = true + def zero: T = apply("") + + def targetString: String = m.toString +} + +object FromString { + // We need these because we clash with the String => Path implicits. + private def toFile(s: String) = new File(new java.io.File(s)) + private def toDir(s: String) = new Directory(new java.io.File(s)) + + /** Path related stringifiers. + */ + val ExistingFile: FromString[File] = new FromString[File] { + override def isDefinedAt(s: String) = toFile(s).isFile + def apply(s: String): File = + if (isDefinedAt(s)) toFile(s) + else cmd.runAndExit(println("'%s' is not an existing file." format s)) + } + val ExistingDir: FromString[Directory] = new FromString[Directory] { + override def isDefinedAt(s: String) = toDir(s).isDirectory + def apply(s: String): Directory = + if (isDefinedAt(s)) toDir(s) + else cmd.runAndExit(println("'%s' is not an existing directory." format s)) + } + def ExistingDirRelativeTo(root: Directory) = new FromString[Directory] { + private def resolve(s: String) = toDir(s) toAbsoluteWithRoot root toDirectory + override def isDefinedAt(s: String) = resolve(s).isDirectory + def apply(s: String): Directory = + if (isDefinedAt(s)) resolve(s) + else cmd.runAndExit(println("'%s' is not an existing directory." format resolve(s))) + } + + /** Argument expander, i.e. turns single argument "foo bar baz" into argument + * list "foo", "bar", "baz". + */ + val ArgumentsFromString: FromString[List[String]] = new FromString[List[String]] { + def apply(s: String) = toArgs(s) + } + + /** Identity. + */ + implicit val StringFromString: FromString[String] = new FromString[String] { + def apply(s: String): String = s + } + + /** Implicit as the most likely to be useful as-is. + */ + implicit val IntFromString: FromString[Int] = new FromString[Int] { + override def isDefinedAt(s: String) = safeToInt(s).isDefined + def apply(s: String) = safeToInt(s).get + def safeToInt(s: String): Option[Int] = try Some(java.lang.Integer.parseInt(s)) catch { case _: NumberFormatException => None } + } +} |