diff --git a/GNUmakefile b/GNUmakefile index bcdc6e7..25122be 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -22,7 +22,8 @@ specl_verbose_1 = --verbose --formatter=report include Makefile -MKROCKSPECS = $(ROCKSPEC_ENV) $(LUA) $(srcdir)/mkrockspecs.lua +ROCKSPEC_ENV = $(LUA_ENV) +MKROCKSPECS = $(ROCKSPEC_ENV) $(LUA) $(srcdir)/mkrockspecs.lua ROCKSPEC_TEMPLATE = $(srcdir)/$(PACKAGE)-rockspec.lua luarocks-config.lua: diff --git a/configure.ac b/configure.ac index 966b5d6..86de659 100644 --- a/configure.ac +++ b/configure.ac @@ -1,7 +1,7 @@ dnl Process this file with autoconf to produce a configure script dnl Initialise autoconf and automake -AC_INIT(stdlib, 34, rrt@sc3d.org) +AC_INIT(stdlib, 34.1, rrt@sc3d.org) AC_CONFIG_AUX_DIR([build-aux]) AM_INIT_AUTOMAKE([foreign]) AM_SILENT_RULES([yes]) diff --git a/specs/getopt_spec.yaml b/specs/getopt_spec.yaml index 006d9cd..94bfe5c 100644 --- a/specs/getopt_spec.yaml +++ b/specs/getopt_spec.yaml @@ -12,8 +12,8 @@ specify getopt: } } - function test (cmdLine, undefined_opts) - local nonOpts, opts, errors = getopt.getOpt (cmdLine, prog.options, undefined_opts) + function test (cmdLine, stop_at_nonopt) + local nonOpts, opts, errors = getopt.getOpt (cmdLine, prog.options, stop_at_nonopt) if #errors == 0 then return { options = opts, args = nonOpts } else @@ -35,9 +35,9 @@ specify getopt: args = {} }) - it diagnoses unrecognized options: expect (test {"-foo"}).should_contain "unrecognized option `-foo'" - - it allows unrecognized options if told to: - expect (test ({"-foo"}, true)).should_equal ( - { options = { foo = {1}}, args = {} }) + - it stops option parsing at the first non-option if told to: + expect (test ({"foo", "-bar"}, true)).should_equal ( + { options = {}, args = {"foo", "-bar"} }) - "describe getopt.usageInfo": - context when specifying options: diff --git a/src/getopt.lua b/src/getopt.lua index 2042f15..d67c8a2 100644 --- a/src/getopt.lua +++ b/src/getopt.lua @@ -44,11 +44,11 @@ local M = { --- Perform argument processing -- @param argIn list of command-line args -- @param options options table --- @param undefined_options if true, allow and collect undefined options +-- @param stop_at_nonopt if true, stop option processing at first non-option -- @return table of remaining non-options -- @return table of option key-value list pairs -- @return table of error messages -local function getOpt (argIn, options, undefined_options) +local function getOpt (argIn, options, stop_at_nonopt) local noProcess = nil local argOut, optOut, errors = {[0] = argIn[0]}, {}, {} -- get an argument for option opt @@ -74,7 +74,7 @@ local function getOpt (argIn, options, undefined_options) local function parseOpt (opt, arg) local o = options.name[opt] - if undefined_options or o ~= nil then + if o ~= nil then o = o or {name = {opt}} optOut[o.name[1]] = optOut[o.name[1]] or {} table.insert (optOut[o.name[1]], getArg (o, opt, arg, optOut[o.name[1]])) @@ -87,9 +87,12 @@ local function getOpt (argIn, options, undefined_options) table.remove (argIn, 1) local _, _, dash, opt = string.find (v, "^(%-%-?)([^=-][^=]*)") local _, _, arg = string.find (v, "=(.*)$") + if not dash and stop_at_nonopt then + noProcess = true + end if v == "--" then - noProcess = 1 - elseif dash == nil or noProcess then -- non-option + noProcess = true + elseif not dash or noProcess then -- non-option table.insert (argOut, v) else -- option parseOpt (opt, arg) @@ -242,12 +245,12 @@ end -- stops program if there was an error, or if --help or -- --version was used. -- @param prog table of named parameters --- @param undefined_opts if true, allow and collect undefined options -local function processArgs (prog, undefined_opts) - local totArgs = #arg +-- @param ... extra arguments for getOpt +local function processArgs (prog, ...) + local totArgs = #_G.arg local errors prog.options = makeOptions (prog.options) - _G.arg, M.opt, errors = getOpt (arg, prog.options, undefined_opts) + _G.arg, M.opt, errors = getOpt (_G.arg, prog.options, ...) local opt = M.opt if (opt.version or opt.help) and prog.banner then io.writelines (prog.banner)