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( "taptest" )
local argsfileinargs = require( "argsfileinargs" )
local same = require( "same" )

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

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

t()

Inspired by