1 -- The getopt-functionality is loaded from pm3/getopt.lua
2 -- Have a look there for further details
3 local getopt
= require('getopt')
4 local ansicolors
= require('ansicolors')
7 usage
= 'script run example_parameters.lua -a 1 -blala -c -de'
8 author
= 'Martin Holst Swende'
11 This is an example script to demonstrate handle parameters in scripts.
12 For more info, check the comments in the code
15 1. script run example_parameters -a mytestparam_input -c
18 script run example_parameters [-h] [-a <txt>] [-b <txt>] [-c] [-d] [-e]
24 -c test param w/o input
25 -d test param w/o input
26 -e test param w/o input
36 print(ansicolors
.cyan
..'Usage'..ansicolors
.reset
)
38 print(ansicolors
.cyan
..'Arguments'..ansicolors
.reset
)
40 print(ansicolors
.cyan
..'Example usage'..ansicolors
.reset
)
44 local function main(args
)
46 print('These parameters were passed')
48 When passing parameters,
49 x: means that a value should follow x
50 y means that 'y' is a flag, either on or off
51 So, the string a:b:def means that we support up to
52 5 parameters; two with values and three flags. The following
55 script run example_parameters.lua -a 1 -blala -c -de
58 1. 'blala' works just like 'b lala', both set 'b' to 'lala'
59 2. Flags can be put together, '-de' is the same as '-d -e'
60 3. The format -b=lala is *not* supported
61 4. The format b lala (without -) is *not* supported
64 for o
, a
in getopt
.getopt(args
, 'ha:b:ced') do
65 if o
== 'h' then return help() end
72 In the future, we may implement so that scripts are invoked directly
73 into a 'main' function, instead of being executed blindly. For future
74 compatibility, I have done so, but I invoke my main from here.