62431
|
1 |
/* Title: Pure/System/getopts.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Support for command-line options as in GNU bash.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
71601
|
10 |
import scala.annotation.tailrec
|
|
11 |
|
|
12 |
|
62431
|
13 |
object Getopts
|
|
14 |
{
|
62454
|
15 |
def apply(usage_text: String, option_specs: (String, String => Unit)*): Getopts =
|
62431
|
16 |
{
|
|
17 |
val options =
|
73359
|
18 |
option_specs.foldLeft(Map.empty[Char, (Boolean, String => Unit)]) {
|
62431
|
19 |
case (m, (s, f)) =>
|
|
20 |
val (a, entry) =
|
74037
|
21 |
if (s.length == 1) (s(0), (false, f))
|
|
22 |
else if (s.length == 2 && s.endsWith(":")) (s(0), (true, f))
|
62431
|
23 |
else error("Bad option specification: " + quote(s))
|
|
24 |
if (m.isDefinedAt(a)) error("Duplicate option specification: " + quote(a.toString))
|
|
25 |
else m + (a -> entry)
|
|
26 |
}
|
|
27 |
new Getopts(usage_text, options)
|
|
28 |
}
|
|
29 |
}
|
|
30 |
|
62454
|
31 |
class Getopts private(usage_text: String, options: Map[Char, (Boolean, String => Unit)])
|
62431
|
32 |
{
|
|
33 |
def usage(): Nothing =
|
|
34 |
{
|
67178
|
35 |
Output.writeln(usage_text, stdout = true)
|
62431
|
36 |
sys.exit(1)
|
|
37 |
}
|
|
38 |
|
|
39 |
private def is_option(opt: Char): Boolean = options.isDefinedAt(opt)
|
|
40 |
private def is_option0(opt: Char): Boolean = is_option(opt) && !options(opt)._1
|
|
41 |
private def is_option1(opt: Char): Boolean = is_option(opt) && options(opt)._1
|
62432
|
42 |
private def print_option(opt: Char): String = quote("-" + opt.toString)
|
62434
|
43 |
private def option(opt: Char, opt_arg: List[Char]): Unit =
|
|
44 |
try { options(opt)._2.apply(opt_arg.mkString) }
|
|
45 |
catch {
|
|
46 |
case ERROR(msg) =>
|
|
47 |
cat_error(msg, "The error(s) above occurred in command-line option " + print_option(opt))
|
|
48 |
}
|
62431
|
49 |
|
71601
|
50 |
@tailrec private def getopts(args: List[List[Char]]): List[List[Char]] =
|
62431
|
51 |
args match {
|
|
52 |
case List('-', '-') :: rest_args => rest_args
|
71383
|
53 |
case ('-' :: opt :: rest_opts) :: rest_args if is_option0(opt) && rest_opts.nonEmpty =>
|
62431
|
54 |
option(opt, Nil); getopts(('-' :: rest_opts) :: rest_args)
|
|
55 |
case List('-', opt) :: rest_args if is_option0(opt) =>
|
|
56 |
option(opt, Nil); getopts(rest_args)
|
71383
|
57 |
case ('-' :: opt :: opt_arg) :: rest_args if is_option1(opt) && opt_arg.nonEmpty =>
|
62431
|
58 |
option(opt, opt_arg); getopts(rest_args)
|
|
59 |
case List('-', opt) :: opt_arg :: rest_args if is_option1(opt) =>
|
|
60 |
option(opt, opt_arg); getopts(rest_args)
|
|
61 |
case List(List('-', opt)) if is_option1(opt) =>
|
62432
|
62 |
Output.error_message("Command-line option " + print_option(opt) + " requires an argument")
|
62431
|
63 |
usage()
|
|
64 |
case ('-' :: opt :: _) :: _ if !is_option(opt) =>
|
62432
|
65 |
if (opt != '?') Output.error_message("Illegal command-line option " + print_option(opt))
|
62431
|
66 |
usage()
|
|
67 |
case _ => args
|
|
68 |
}
|
|
69 |
|
|
70 |
def apply(args: List[String]): List[String] = getopts(args.map(_.toList)).map(_.mkString)
|
|
71 |
def apply(args: Array[String]): List[String] = apply(args.toList)
|
|
72 |
}
|