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

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