/zoeplat

To get this branch, use:
bzr branch http://9ix.org/bzr/zoeplat

« back to all changes in this revision

Viewing changes to getopt_alt.lua

  • Committer: Josh C
  • Date: 2013-04-22 20:42:06 UTC
  • Revision ID: josh@9ix.org-20130422204206-of4wo7sulpkp0pft
stop build from dropping backup files EVERYWHERE

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
-- getopt_alt.lua
 
2
-- http://lua-users.org/wiki/AlternativeGetOpt
 
3
 
 
4
-- getopt, POSIX style command line argument parser
 
5
-- param arg contains the command line arguments in a standard table.
 
6
-- param options is a string with the letters that expect string values.
 
7
-- returns a table where associated keys are true, nil, or a string value.
 
8
-- The following example styles are supported
 
9
--   -a one  ==> opts["a"]=="one"
 
10
--   -bone   ==> opts["b"]=="one"
 
11
--   -c      ==> opts["c"]==true
 
12
--   --c=one ==> opts["c"]=="one"
 
13
--   -cdaone ==> opts["c"]==true opts["d"]==true opts["a"]=="one"
 
14
-- note POSIX demands the parser ends at the first non option
 
15
--      this behavior isn't implemented.
 
16
 
 
17
function getopt( arg, options )
 
18
  local tab = {}
 
19
  for k, v in ipairs(arg) do
 
20
    if string.sub( v, 1, 2) == "--" then
 
21
      local x = string.find( v, "=", 1, true )
 
22
      if x then tab[ string.sub( v, 3, x-1 ) ] = string.sub( v, x+1 )
 
23
      else      tab[ string.sub( v, 3 ) ] = true
 
24
      end
 
25
    elseif string.sub( v, 1, 1 ) == "-" then
 
26
      local y = 2
 
27
      local l = string.len(v)
 
28
      local jopt
 
29
      while ( y <= l ) do
 
30
        jopt = string.sub( v, y, y )
 
31
        if string.find( options, jopt, 1, true ) then
 
32
          if y < l then
 
33
            tab[ jopt ] = string.sub( v, y+1 )
 
34
            y = l
 
35
          else
 
36
            tab[ jopt ] = arg[ k + 1 ]
 
37
          end
 
38
        else
 
39
          tab[ jopt ] = true
 
40
        end
 
41
        y = y + 1
 
42
      end
 
43
    end
 
44
  end
 
45
  return tab
 
46
end
 
 
b'\\ No newline at end of file'