Dash:
[t2-trunk.git] / misc / lua / sde / getopt.lua
blob0430bf72a27b78762ec824d5658a5d595e7b8149
1 -- --- T2-COPYRIGHT-NOTE-BEGIN ---
2 -- This copyright note is auto-generated by ./scripts/Create-CopyPatch.
3 --
4 -- T2 SDE: misc/lua/sde/getopt.lua
5 -- Copyright (C) 2005 - 2006 The T2 SDE Project
6 -- Copyright (C) 2005 - 2006 Juergen "George" Sawinski
7 --
8 -- More information can be found in the files COPYING and README.
9 --
10 -- This program is free software; you can redistribute it and/or modify
11 -- it under the terms of the GNU General Public License as published by
12 -- the Free Software Foundation; version 2 of the License. A copy of the
13 -- GNU General Public License can be found in the file COPYING.
14 -- --- T2-COPYRIGHT-NOTE-END ---
16 -- TODO:
17 -- - add boolean option argument types
18 -- - maybe other types needed ?
20 -- DESCRIPTION:
21 -- opts,args,err = getopt(arg-list, option-definition)
22 -- Parses the "arg-list" according to "option-definition" and
23 -- returns the parsed options "opts", additional arguments "args" and
24 -- possibly an error.
25 --
26 -- Option definition format:
27 -- key Option key (e.g. key = "help")
28 -- option String or list of options (e.g. option = {"-h", "--help"})
29 -- default Default value (e.g. default = false)
30 -- argument This option has no argument (ARG.none), a required
31 -- argument (ARG.required) or an optional argument (ARG.optional)
32 -- flag The option is flagged:
33 -- number (OPT.number)
34 -- incremental (OPT.incremental)
35 -- Example: see below
37 ARG = {
38 none = 0,
39 required = 1,
40 optional = 2
43 OPT = {
44 none = 0,
45 string = 0,
46 number = 1,
47 --boolean = 2,
48 incremental = 10,
51 function getopt(args,def)
52 local opt = {}
53 local arg = {}
54 local nargs = table.getn(args)
56 -- first pass: defaults
57 for _,d in pairs(def) do
58 if d.default ~= nil then
59 opt[d.key] = d.default
60 end
61 end
63 -- Second pass: parse arguments
64 local i=1
65 repeat
66 for _,d in pairs(def) do
67 local found = false
69 -- FIXME is there some shorter version for this:
70 if type(d.option) == "string" then
71 if d.option == args[i] then found=true end
72 else
73 for k=1,table.getn(d.option) do
74 if d.option[k] == args[i] then
75 found = true
76 end
77 end
78 end
80 -- process option and arguments
81 if found then
82 local val = true
83 local nextisopt = false
84 local havearg = false
86 -- check if next is an argument (not option)
87 if args[i+1] and string.sub(args[i+1],1,1) == "-" then
88 nextisopt = true
89 end
91 -- option argument ?
92 if nextisopt and d.argument == ARG.required then
93 return nil, nil, "Missing option argument for `" .. args[i] .. "'"
94 end
96 if not nextisopt and
97 (d.argument == ARG.required
98 or d.argument == ARG.optional) then
99 args[i] = nil
100 i = i+1
102 val = args[i]
104 havearg = true
107 -- flags ?
108 if d.flag then
109 if d.flag == OPT.number then
110 val = tonumber(val)
111 elseif d.flag == OPT.incremental then
112 val = opt[d.key] + 1
116 -- finally assign value
117 opt[d.key] = val
119 -- remove key from list (see third pass)
120 args[i] = nil
123 i=i+1
124 until i>nargs
126 -- Third pass: check option, sort arguments
127 for i=1,nargs do
128 if args[i] then
129 if string.sub(args[i],1,1) == "-" then
130 return nil, nil, "Unrecognized option `" .. args[i] .. "'"
131 else
132 table.insert(arg, args[i])
137 -- done
138 return opt,arg
142 -- EXAMPLE
143 if false then
144 opt,arg,err = getopt(arg, {
146 key = "help",
147 option = { "-h", "-help", "--help" },
150 key = "verbose",
151 option = { "-v", "-verbose", "--verbose" },
152 default = 0,
153 flag = OPT.incremental
157 if not opt then
158 print "ERR:"
159 print(err)
160 os.exit(-1)
163 print "OPTS:"
164 for k,v in pairs(opt) do
165 print("",k,v)
168 print "ARGS:"
169 for k,v in pairs(arg) do
170 print("",k,v)