Implemented parameter passing into script, added a minimal getop-parser, added an example script which takes parameters

This commit is contained in:
martin.holst@gmail.com 2013-09-19 19:21:12 +00:00
parent b13fa4448f
commit 30a5d35529
3 changed files with 155 additions and 3 deletions

View file

@ -228,8 +228,8 @@ int CmdRun(const char *Cmd)
// memset(cmd_name, 0, 32);
// sscanf(Cmd, "%31s%n", cmd_name, &len);
char script_name[128];
char arguments[256];
char script_name[128] = {0};
char arguments[256] = {0};
int name_len = 0;
int arg_len = 0;
@ -244,7 +244,7 @@ int CmdRun(const char *Cmd)
char buf[256];
snprintf(buf, sizeof buf, "./scripts/%s%s", script_name, suffix);
printf("---Executing: %s with arguments '%s'\n",buf,arguments);
printf("--- Executing: %s, args'%s'\n",buf,arguments);

107
client/getopt.lua Normal file
View file

@ -0,0 +1,107 @@
--[[This file is an adaptation from the following source:
https://github.com/attractivechaos/klib/blob/master/lua/klib.lua
]]
--[[
The MIT License
Copyright (c) 2011, Attractive Chaos <attractor@live.co.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]--
local function split(txt)
local retval = {};
for i in string.gmatch(txt, "%S+") do
table.insert(retval,i)
end
return retval
end
-- Description: getopt() translated from the BSD getopt(); compatible with the default Unix getopt()
--[[ Example:
for o, a in os.getopt(arg, 'a:b') do
print(o, a)
end
]]--
local function getopt(args, ostr)
-- Modification to handle strings instead of tables:
if type(args) == 'string' then
args = split(args)
end
local arg, place = nil, 0;
return function ()
if place == 0 then -- update scanning pointer
place = 1
if #args == 0
or args[1]:sub(1, 1) ~= '-' then
place = 0; return nil end
if #args[1] >= 2 then
place = place + 1
if args[1]:sub(2, 2) == '-' then -- found "--"
place = 0
table.remove(args, 1);
return nil;
end
end
end
local optopt = args[1]:sub(place, place);
place = place + 1;
local oli = ostr:find(optopt);
if optopt == ':' or oli == nil then -- unknown option
if optopt == '-' then return nil end
if place > #args[1] then
table.remove(args, 1);
place = 0;
end
return '?';
end
oli = oli + 1;
if ostr:sub(oli, oli) ~= ':' then -- do not need argument
arg = nil;
if place > #args[1] then
table.remove(args, 1);
place = 0;
end
else -- need an argument
if place <= #args[1] then -- no white space
arg = args[1]:sub(place);
else
table.remove(args, 1);
if #args == 0 then -- an option requiring argument is the last one
place = 0;
if ostr:sub(1, 1) == ':' then return ':' end
return '?';
else arg = args[1] end
end
table.remove(args, 1);
place = 0;
end
return optopt, arg;
end
end
return { getopt = getopt }

View file

@ -0,0 +1,45 @@
-- The getopt-functionality is loaded from pm3/getopt.lua
-- Have a look there for further details
getopt = require('getopt')
usage = "script run parameters.lua -a 1 -blala -c -de"
author = "Martin Holst Swende"
desc =[[
This is an example script to demonstrate handle parameters in scripts.
For more info, check the comments in the code
]]
local function main(args)
print(desc)
print("These parameters were passed")
--[[
When passing parameters,
x: means that a value should follow x
y means that 'y' is a flag, either on or off
So, the string a:b:def means that we support up to
5 parameters; two with values and three flags. The following
should be valid:
script run parameters.lua -a 1 -blala -c -de
Notice two things:
1. 'blala' works just like 'b lala', both set 'b' to 'lala'
2. Flags can be put together, '-de' is the same as '-d -e'
3. The format -b=lala is *not* supported
4. The format b lala (without -) is *not* supported
--]]
for o, a in getopt.getopt(args, 'a:b:ced') do
print(o, a)
end
end
--[[
In the future, we may implement so that scripts are invoked directly
into a 'main' function, instead of being executed blindly. For future
compatibility, I have done so, but I invoke my main from here.
--]]
main(args)