diff options
author | Rocky Madden <git@rockymadden.com> | 2013-12-31 12:15:43 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2013-12-31 12:15:43 -0700 |
commit | b7309df68ff37c79428c103cc224d81f40d8a37f (patch) | |
tree | 58facc06d07772ca0d2e3bd2b815740814e99fe8 /cli/source | |
parent | e88f7a8f8e77632cce4afef1c9f50c05b826328b (diff) | |
download | stringmetric-b7309df68ff37c79428c103cc224d81f40d8a37f.tar.gz stringmetric-b7309df68ff37c79428c103cc224d81f40d8a37f.tar.bz2 stringmetric-b7309df68ff37c79428c103cc224d81f40d8a37f.zip |
Moved Command into package object.
Diffstat (limited to 'cli/source')
-rwxr-xr-x | cli/source/main/scala/com/rockymadden/stringmetric/cli/Command.scala | 32 | ||||
-rwxr-xr-x | cli/source/main/scala/com/rockymadden/stringmetric/cli/package.scala | 35 |
2 files changed, 34 insertions, 33 deletions
diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/Command.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/Command.scala deleted file mode 100755 index 7451c35..0000000 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/Command.scala +++ /dev/null @@ -1,32 +0,0 @@ -package com.rockymadden.stringmetric.cli - -abstract class Command( - protected val help: (OptionMap => String), - protected val predicate: (OptionMap => Boolean), - protected val execute: (OptionMap => String) -) { - private def error(error: Throwable, opts: OptionMap): Unit = - if (!isUnitTest(opts)) { - println(error.getMessage) - sys.exit(1) - } else throw error - - private def exit(opts: OptionMap): Unit = if (!isUnitTest(opts)) sys.exit(0) - - private def isUnitTest(opts: OptionMap) = - opts.contains('ut) || (opts.contains('unitTest) && opts.get('unitTest) != "false") - - final def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - println(help(opts)) - exit(opts) - } else if (predicate(opts)) { - println(execute(opts)) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } -} diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/package.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/package.scala index c4d8873..617f4a5 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/package.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/package.scala @@ -9,7 +9,8 @@ package object cli { implicit def optionStringToArray(os: OptionString): Array[String] = - if (os.get.length == 0) Array.empty[String] else os.get.split(" ") + if (os.get.length == 0) Array.empty[String] + else os.get.split(" ") implicit def optionStringToBigDecimal(os: OptionString): BigDecimal = BigDecimal(os.get) implicit def optionStringToBigInt(os: OptionString): BigInt = BigInt(os.get) implicit def optionStringToDouble(os: OptionString): Double = os.get.toDouble @@ -69,4 +70,36 @@ package object cli { next(Map.empty[Symbol, OptionString], varargs.toList) } } + + + abstract class Command( + protected val help: (OptionMap => String), + protected val predicate: (OptionMap => Boolean), + protected val execute: (OptionMap => String) + ) { + final def main(args: Array[String]): Unit = { + val opts: OptionMap = args + + try + if (opts.contains('h) || opts.contains('help)) { + println(help(opts)) + exit(opts) + } else if (predicate(opts)) { + println(execute(opts)) + exit(opts) + } else throw new IllegalArgumentException("Expected valid syntax. See --help.") + catch { case e: Throwable => error(e, opts) } + } + + private def error(error: Throwable, opts: OptionMap): Unit = + if (!isUnitTest(opts)) { + println(error.getMessage) + sys.exit(1) + } else throw error + + private def exit(opts: OptionMap): Unit = if (!isUnitTest(opts)) sys.exit(0) + + private def isUnitTest(opts: OptionMap) = + opts.contains('ut) || (opts.contains('unitTest) && opts.get('unitTest) != "false") + } } |