]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | --[[This file is an adaptation from the following source: | |
3 | ||
4 | https://github.com/attractivechaos/klib/blob/master/lua/klib.lua | |
5 | ||
6 | ]] | |
7 | ||
8 | --[[ | |
9 | The MIT License | |
10 | ||
11 | Copyright (c) 2011, Attractive Chaos <attractor@live.co.uk> | |
12 | ||
13 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
14 | of this software and associated documentation files (the "Software"), to deal | |
15 | in the Software without restriction, including without limitation the rights | |
16 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
17 | copies of the Software, and to permit persons to whom the Software is | |
18 | furnished to do so, subject to the following conditions: | |
19 | ||
20 | The above copyright notice and this permission notice shall be included in | |
21 | all copies or substantial portions of the Software. | |
22 | ||
23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
24 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
25 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
26 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
27 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
28 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
29 | SOFTWARE. | |
30 | ]]-- | |
31 | ||
32 | ||
33 | local function split(txt) | |
34 | local retval = {}; | |
35 | for i in string.gmatch(txt, "%S+") do | |
36 | table.insert(retval,i) | |
37 | end | |
38 | return retval | |
39 | end | |
40 | ||
41 | ||
42 | -- Description: getopt() translated from the BSD getopt(); compatible with the default Unix getopt() | |
43 | --[[ Example: | |
44 | for o, a in os.getopt(arg, 'a:b') do | |
45 | print(o, a) | |
46 | end | |
47 | ]]-- | |
48 | ||
49 | local function getopt(args, ostr) | |
50 | -- Modification to handle strings instead of tables: | |
51 | if type(args) == 'string' then | |
52 | args = split(args) | |
53 | end | |
54 | ||
55 | local arg, place = nil, 0; | |
56 | return function () | |
57 | if place == 0 then -- update scanning pointer | |
58 | place = 1 | |
59 | if #args == 0 | |
60 | or args[1]:sub(1, 1) ~= '-' then | |
61 | place = 0; return nil end | |
62 | if #args[1] >= 2 then | |
63 | place = place + 1 | |
64 | if args[1]:sub(2, 2) == '-' then -- found "--" | |
65 | place = 0 | |
66 | table.remove(args, 1); | |
67 | return nil; | |
68 | end | |
69 | end | |
70 | end | |
71 | local optopt = args[1]:sub(place, place); | |
72 | place = place + 1; | |
73 | local oli = ostr:find(optopt); | |
74 | if optopt == ':' or oli == nil then -- unknown option | |
75 | if optopt == '-' then return nil end | |
76 | if place > #args[1] then | |
77 | table.remove(args, 1); | |
78 | place = 0; | |
79 | end | |
80 | return '?'; | |
81 | end | |
82 | oli = oli + 1; | |
83 | if ostr:sub(oli, oli) ~= ':' then -- do not need argument | |
84 | arg = nil; | |
85 | if place > #args[1] then | |
86 | table.remove(args, 1); | |
87 | place = 0; | |
88 | end | |
89 | else -- need an argument | |
90 | if place <= #args[1] then -- no white space | |
91 | arg = args[1]:sub(place); | |
92 | else | |
93 | table.remove(args, 1); | |
94 | if #args == 0 then -- an option requiring argument is the last one | |
95 | place = 0; | |
96 | if ostr:sub(1, 1) == ':' then return ':' end | |
97 | return '?'; | |
98 | else arg = args[1] end | |
99 | end | |
100 | table.remove(args, 1); | |
101 | place = 0; | |
102 | end | |
103 | return optopt, arg; | |
104 | end | |
105 | end | |
106 | ||
107 | return { getopt = getopt } |