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