2 --[[This file is an adaptation from the following source:
4 https://github.com/attractivechaos/klib/blob/master/lua/klib.lua
11 Copyright (c) 2011, Attractive Chaos <attractor@live.co.uk>
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:
20 The above copyright notice and this permission notice shall be included in
21 all copies or substantial portions of the Software.
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
33 local function split(txt)
35 for i in string.gmatch(txt, "%S+") do
36 table.insert(retval,i)
42 -- Description: getopt() translated from the BSD getopt(); compatible with the default Unix getopt()
44 for o, a in os.getopt(arg, 'a:b') do
49 local function getopt(args, ostr)
50 -- Modification to handle strings instead of tables:
51 if type(args) == 'string' then
55 local arg, place = nil, 0;
57 if place == 0 then -- update scanning pointer
60 or args[1]:sub(1, 1) ~= '-' then
61 place = 0; return nil end
64 if args[1]:sub(2, 2) == '-' then -- found "--"
66 table.remove(args, 1);
71 local optopt = args[1]:sub(place, place);
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);
83 if ostr:sub(oli, oli) ~= ':' then -- do not need argument
85 if place > #args[1] then
86 table.remove(args, 1);
89 else -- need an argument
90 if place <= #args[1] then -- no white space
91 arg = args[1]:sub(place);
93 table.remove(args, 1);
94 if #args == 0 then -- an option requiring argument is the last one
96 if ostr:sub(1, 1) == ':' then return ':' end
98 else arg = args[1] end
100 table.remove(args, 1);
107 return { getopt = getopt }