File: getopt.lua

package info (click to toggle)
lua-posix 36.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,720 kB
  • sloc: ansic: 5,462; makefile: 21; sh: 6
file content (23 lines) | stat: -rw-r--r-- 566 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#! /usr/bin/env lua

local getopt = require 'posix.unistd'.getopt


local last_index = 1
for r, optarg, optind in getopt(arg, 'ha:s:') do
   if r == '?' then
      return print('unrecognized option', arg[optind -1])
   end
   last_index = optind
   if r == 'h' then
      print '-h      print this help text'
      print '-a ARG  a flag with required argument'
      print '-s ARG  a different flag with a required argument'
   elseif r == 'a' or r == 's' then
      print('we were passed', r, optarg)
   end
end

for i = last_index, #arg do
   print(i, arg[i])
end