argsfileinargs

function argsfileinargs( args ) --> filepath, idx, rest

Description

Scans an aruments table for the --args value and returns the filepath that belongs to it.

Parameters

args

The arguments that should be scaned.

Return Values

filepath

Filepath to the args file, otherwise nil.

idx

The index in args where the arguments should be inserted in rest, otherwise nil.

rest

The remaining entries from args.

Code

--ZFUNC-argsfileinargs-v1
local function argsfileinargs( args ) --> filepath, idx, rest
   local idx = nil
   for k, v in pairs( args ) do
      if v == "--args" then
         idx = k
      end
   end

   if not idx then return nil, idx, args end

   -- we have a args parameter that should be used
   local filepath = nil
   local rest = {}
   for i = 1, #args do
      if i == idx then
         -- nothing happens --args
      elseif i == idx+1 then
         filepath = args[ i ]
      else
         table.insert( rest, args[ i ] )
      end
   end

   return filepath, idx, rest
end

return argsfileinargs

Example

local t = require( "tapered" )
local argsfileinargs = require( "argsfileinargs" )

filepath, idx, args = argsfileinargs{ "opt1", "--args", "xxx.args", "opt2" }
t.is( filepath, "xxx.args" )
t.is( idx, 2 )
t.same( args, { "opt1", "opt2" } )

filepath, idx, args = argsfileinargs( args )
t.is( filepath, nil )
t.is( idx, nil )
t.same( args, { "opt1", "opt2" } )

t.done()

Inspired by