Blame examples/getopt.lua

Packit 437b5e
local P = require 'posix'
Packit 437b5e
Packit 437b5e
local short = "ha:s:"
Packit 437b5e
Packit 437b5e
local long = {
Packit 437b5e
  {"help",  "none", 'h'},
Packit 437b5e
  {"aleph", "required", 'a'},
Packit 437b5e
  {"start", "required", 's'}
Packit 437b5e
}
Packit 437b5e
Packit 437b5e
local last_index = 1
Packit 437b5e
for r, optarg, optind, li in P.getopt (arg, short, long) do
Packit 437b5e
  if r == '?' then
Packit 437b5e
    return print  'unrecognized option'
Packit 437b5e
  end
Packit 437b5e
  last_index = optind
Packit 437b5e
  if r == 'h' then
Packit 437b5e
    print 'help'
Packit 437b5e
  elseif r == 'a' or r == 's' then
Packit 437b5e
    print ('we were passed', r, optarg)
Packit 437b5e
  end
Packit 437b5e
end
Packit 437b5e
Packit 437b5e
for i = last_index, #arg do
Packit 437b5e
  print (i, arg[i])
Packit 437b5e
end