liquid_feedback_frontend

diff utils/optparse.lua @ 107:eeb167cf9dc4

add comand line admin tool

started a command line admin tool to help development.
currently supports setting login passwords and list users
author Daniel Poelzleithner <poelzi@poelzi.org>
date Sun Sep 19 01:33:23 2010 +0200 (2010-09-19)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/utils/optparse.lua	Sun Sep 19 01:33:23 2010 +0200
     1.3 @@ -0,0 +1,139 @@
     1.4 +-- Lua command line option parser.
     1.5 +-- Interface based on Pythons optparse.
     1.6 +-- http://docs.python.org/lib/module-optparse.html
     1.7 +-- (c) 2008 David Manura, Licensed under the same terms as Lua (MIT license)
     1.8 +--
     1.9 +-- To be used like this:                                                                  
    1.10 +-- t={usage="<some usage message>", version="<version string>"}                           
    1.11 +-- op=OptionParser(t)                                                                     
    1.12 +-- op=add_option{"<opt>", action=<action>, dest=<dest>, help="<help message for this option>"}
    1.13 +--
    1.14 +-- with :
    1.15 +--   <opt> the option string to be used (can be anything, if one letter opt, then should be -x val, more letters: -xy=val )
    1.16 +--   <action> one of
    1.17 +--   - store: store in options as key, val                                                  
    1.18 +--   - store_true: stores key, true                                                         
    1.19 +--   - store_false: stores key, false
    1.20 +--   <dest> is the key under which the option is saved
    1.21 +--                                      
    1.22 +-- options,args = op.parse_args()
    1.23 +--
    1.24 +-- now options is the table of options (key, val) and args is the table with non-option arguments.
    1.25 +-- You can use op.fail(message) for failing and op.print_help() for printing the usage as you like.
    1.26 +
    1.27 +--module('utils.optparse')
    1.28 +
    1.29 +local function OptionParser(t)
    1.30 +  local usage = t.usage
    1.31 +  local version = t.version
    1.32 +  local commands = t.commands
    1.33 +
    1.34 +  local o = {}
    1.35 +  local option_descriptions = {}
    1.36 +  local option_of = {}
    1.37 +
    1.38 +  function o.fail(s) -- extension
    1.39 +    io.stderr:write(s .. '\n')
    1.40 +    os.exit(1)
    1.41 +  end
    1.42 +
    1.43 +  function o.add_option(optdesc)
    1.44 +    option_descriptions[#option_descriptions+1] = optdesc
    1.45 +    for _,v in pairs(optdesc) do
    1.46 +      option_of[v] = optdesc
    1.47 +    end
    1.48 +  end
    1.49 +  function o.parse_args()
    1.50 +    -- expand options (e.g. "--input=file" -> "--input", "file")
    1.51 +    local arg = {unpack(arg)}
    1.52 +    for i=#arg,1,-1 do local v = arg[i]
    1.53 +      local flag, val = v:match('^(%-%-%w+)=(.*)')
    1.54 +      if flag then
    1.55 +        arg[i] = flag
    1.56 +        table.insert(arg, i+1, val)
    1.57 +      end
    1.58 +    end
    1.59 +
    1.60 +    local options = {}
    1.61 +    for _,optdesc in ipairs(option_descriptions) do
    1.62 +      options[optdesc["dest"]] = optdesc.default
    1.63 +    end
    1.64 +    local args = {}
    1.65 +    local i = 1
    1.66 +    while i <= #arg do local v = arg[i]
    1.67 +      local optdesc = option_of[v]
    1.68 +      if optdesc then
    1.69 +        local action = optdesc.action
    1.70 +        local val
    1.71 +        if action == 'store' or action == nil then
    1.72 +          i = i + 1
    1.73 +          val = arg[i]
    1.74 +          if not val then o.fail('option requires an argument ' .. v) end
    1.75 +        elseif action == 'store_true' then
    1.76 +          val = true
    1.77 +        elseif action == 'store_false' then
    1.78 +          val = false
    1.79 +        end
    1.80 +        options[optdesc.dest] = val
    1.81 +      else
    1.82 +        if v:match('^%-') then o.fail('invalid option ' .. v) end
    1.83 +        args[#args+1] = v
    1.84 +      end
    1.85 +      i = i + 1
    1.86 +    end
    1.87 +    if options.help then
    1.88 +      o.print_help()
    1.89 +      os.exit()
    1.90 +    end
    1.91 +    if options.version then
    1.92 +      io.stdout:write(t.version .. "\n")
    1.93 +      os.exit()
    1.94 +    end
    1.95 +    return options, args
    1.96 +  end
    1.97 +
    1.98 +  local function flags_str(optdesc)
    1.99 +    local sflags = {}
   1.100 +    local action = optdesc.action
   1.101 +    for _,flag in ipairs(optdesc) do
   1.102 +      local sflagend
   1.103 +      if action == nil or action == 'store' then
   1.104 +        local metavar = optdesc.metavar or optdesc.dest:upper()
   1.105 +        sflagend = #flag == 2 and ' ' .. metavar
   1.106 +                              or  '=' .. metavar
   1.107 +      else
   1.108 +        sflagend = ''
   1.109 +      end
   1.110 +      sflags[#sflags+1] = flag .. sflagend
   1.111 +    end
   1.112 +    return table.concat(sflags, ', ')
   1.113 +  end
   1.114 +
   1.115 +  function o.print_help()
   1.116 +    io.stdout:write("Usage: " .. usage:gsub('%%prog', arg[0]) .. "\n")
   1.117 +    io.stdout:write("\n")
   1.118 +    io.stdout:write("Options:\n")
   1.119 +    for _,optdesc in ipairs(option_descriptions) do
   1.120 +      io.stdout:write("  " .. flags_str(optdesc) ..
   1.121 +                      "  " .. optdesc.help .. "\n")
   1.122 +    end
   1.123 +    if commands then
   1.124 +        io.stdout:write("\nCommands:\n")
   1.125 +        for _,command in ipairs(commands) do
   1.126 +              io.stdout:write("  " .. command[1] .. 
   1.127 +                              string.rep(" ", 30-#command[1]) ..
   1.128 +                              command[2] .. "\n")
   1.129 +        end
   1.130 +    end
   1.131 +
   1.132 +  end
   1.133 +  o.add_option{"--help", action="store_true", dest="help",
   1.134 +               help="show this help message and exit"}
   1.135 +  if t.version then
   1.136 +    o.add_option{"--version", action="store_true", dest="version",
   1.137 +                 help="output version info."}
   1.138 +  end
   1.139 +  return o
   1.140 +end
   1.141 +
   1.142 +return OptionParser
   1.143 \ No newline at end of file

Impressum / About Us