[/] [trunk/] [src/] [commands.sml] - Blame information for rev 4

Line No. Rev Author Line
1 4 tbourke
(* $Id$
2
 
3
   20070815 T. Bourke
4
     Original code. Process options from the command line.
5
 *)
6
 
7
structure Commands : COMMANDS
8
=
9
let structure GO  = GetOpt
10
          and O   = Option
11
          and TIO = TextIO
12
in
13
struct
14
  type options = {inputfile: string option, outputfile: string option}
15
  val defaults = {inputfile=NONE, outputfile=NONE}
16
 
17
  datatype command = ScriptFile of string
18
                   | ScriptText of string
19
                   | ScriptTerminal
20
                   | ShowConfig
21
                   | ConfigFile of string
22
                   | ConfigText of string
23
                   | TestFlip
24
 
25
  datatype args = ShowUsage
26
                | ShowVersion
27
                | InputFile of string
28
                | OutputFile of string
29
                | Command of command
30
                | Ignore
31
 
32
  fun showError msg = TIO.output (TIO.stdErr,
33
        String.concat [Settings.progName, ": ", msg, "\n"])
34
 
35
  fun filterCommand (Command cmd) = SOME cmd
36
    | filterCommand _             = NONE
37
 
38
  val optionList = [
39
      {short="h", long=["help"], desc=GO.NoArg (fn ()=>ShowUsage),
40
       help="show this summary of command line options"},
41
 
42
      {short="v", long=["version"],
43
       desc=GO.NoArg (fn ()=>ShowVersion),
44
       help="show the version number"},
45
 
46
      {short="i", long=["input"],
47
       desc=GO.ReqArg (InputFile, "path"),
48
       help="Uppaal xml file to use for input"},
49
 
50
      {short="o", long=["output"],
51
       desc=GO.ReqArg (OutputFile, "path"),
52
       help="Path to file which should be overwritten with results"},
53
 
54
      {short="e", long=["eval"],
55
       desc=GO.ReqArg (fn t=>Command (ScriptText t), "script"),
56
       help="Evaluate script expressions directly."},
57
 
58
      {short="f", long=["scriptfile"],
59
       desc=GO.ReqArg (fn f=>Command (ScriptFile f), "path"),
60
       help="Evaluate script commands from a file."},
61
 
62
      {short="t", long=["terminal"],
63
       desc=GO.NoArg (fn ()=>Command (ScriptTerminal)),
64
       help="Invoke interactive terminal."},
65
 
66
      {short="c", long=["config"],
67
       desc=GO.ReqArg (fn f=>Command (ConfigFile f), "path"),
68
       help="Path to a configuration file."},
69
 
70
      {short="s", long=["set"],
71
       desc=GO.ReqArg (fn t=>Command (ConfigText t), "configtext"),
72
       help="Specify configuration settings directly."},
73
 
74
      {short="", long=["showconfig"],
75
       desc=GO.NoArg (fn ()=> Command ShowConfig),
76
       help="Write all settings to standard output"},
77
 
78
      {short="", long=["testflip"],
79
       desc=GO.NoArg (fn ()=>Command TestFlip),
80
       help="Test template flipping."}
81
    ]
82
 
83
  fun warningIfUsed NONE     = ()
84
    | warningIfUsed (SOME f) = showError ("file ignored '"^f^"'")
85
 
86
  fun processOptions (arg, opt as {inputfile=inf, outputfile=outf}) =
87
      case arg of
88
        ShowUsage        => opt before (
89
                              TextIO.print (GO.usageInfo {
90
                                header=(Settings.progName^" "^
91
                                        Settings.version),
92
                                options=optionList});
93
                              TextIO.print "\n")
94
      | ShowVersion      => opt before TextIO.print (Settings.version ^ "\n")
95
      | InputFile f      => {inputfile=SOME f,outputfile=outf}
96
                            before warningIfUsed inf
97
      | OutputFile f     => {outputfile=SOME f, inputfile=inf}
98
                            before warningIfUsed outf
99
      | Command com      => opt
100
      | Ignore           => opt
101
 
102
  fun wrapFiles []       = []
103
    | wrapFiles (f::fs)  = InputFile f::wrapFiles' fs
104
  and wrapFiles' []      = []
105
    | wrapFiles' (f::fs) = OutputFile f::wrapFiles fs
106
 
107
  fun showRequest ShowUsage   = true
108
    | showRequest ShowVersion = true
109
    | showRequest _           = false
110
 
111
  fun processCommands args = let
112
      val (ops, files) = GO.getOpt {argOrder=GO.Permute,
113
                                    options=optionList,
114
                                    errFn=showError} args
115
      val options = foldl processOptions defaults (ops @ wrapFiles files)
116
      val commands = List.mapPartial filterCommand ops
117
    in
118
      if null commands andalso not (List.exists showRequest ops)
119
      then ([ScriptTerminal], options) else (commands, options)
120
    end
121
 
122
end
123
end
124