Parse command-line arguments: Difference between revisions

Added zkl
(cut off first element to really get name *after* -f argument (alternatively: increase index))
(Added zkl)
Line 605:
Searching with -regexp allows to specify longer mnemonic names, so it still succeeds on longer flags, e.g.
$ myscript.tcl -separator '\t' ...
 
=={{header|zkl}}==
The Argh class provides command line parsing, it can do actions during parsing, leave it for you to do after parsing, print errors, the option list, short or long options, with or without option args, etc.
 
File myprogram.zkl:
<lang zkl>var ip;
argh := Utils.Argh(
T("v","v","print version",fcn{println("Version stub")}),
T("n","n","ignored"),
T("z","z","zazzle"),
T("+ip","","get IP address",fcn(arg){ip=arg}),
);
parsedArgs := argh.parse(vm.arglist);
 
println("Unparsed stuff: ",argh.loners);
println("The IP address is ",ip);
foreach option,arg in (parsedArgs){
switch(option) {
case("z") { println("zazzle") }
}
}</lang>
<pre>zkl myprogram nc -v -n -z --ip 192.168.1.2 1-1000</pre>
{{out}}
<pre>Version stub
Unparsed stuff: L("nc","1-1000")
The IP address is 192.168.1.2
zazzle
</pre>
<pre>zkl myprogram nc -v -n -z --ip</pre>
{{out}}
<pre>
Option "ip" is missing an arg
Options:
--ip <arg>: get IP address
--n (-n) : ignored
--v (-v) : print version
--z (-z) : zazzle
</pre>
Anonymous user