** "Looks like JavaScript, brain (contents) is Ruby, (stability is AC / DC)" ** Scripting language Kinx ). The library is the life of the language. So how to use the library.
This time it's Getopt. I implemented it internally in SpecTest and used it, but I moved it to the standard library.
It also supports the long option (~~ I haven't released it yet ... ~~ I have released it).
Getopt - System.getopt
Specify an array of options, an option string, and a long option object in the location of the conditional expression of the while statement as shown below. Long option objects can be omitted.
var opt, add, check;
while (opt = System.getopt($$, "a:df", { add: 'a', delete: 'd', help: null, "do-check": '=' })) {
switch (opt.type) {
case 'a': // '--add'But'a'Is returned.
add = opt.arg; // ':'The specification indicates that there is an argument.
System.println('-a with "%{add}"');
break;
case 'd': // '--delete'But'd'Is returned.
System.println('-d');
break;
case 'f': // '-f'Return with.
System.println('-f');
break;
case 'help': // '--help'Return with.
System.println('--help');
break;
case 'do-check': // '--do-check'Return with.
check = opt.arg; // '='The specification indicates that there is an argument.
System.println('--do-check with "%{check}"');
break;
case '-': //If it wasn't an option, come here.
list.push(opt.arg);
break;
}
}
//Display other than options
System.println("Program options: ", list);
-d -f
may be written as -df
.-a ARG
may be written as -a ARG
.-d -a ARG
can be written as either -da ARG
or -da ARG
.--long-option = argument
. In the case of the long option, an empty string argument is allowed.When the previous sample is operated, it becomes as follows.
$ ./kinx examples/option.kx -d -a arg
-d
-a with "arg"
Program options: ["examples/option.kx"]
$ ./kinx examples/option.kx -da arg
-d
-a with "arg"
Program options: ["examples/option.kx"]
$ ./kinx examples/option.kx -daarg
-d
-a with "arg"
Program options: ["examples/option.kx"]
$ ./kinx examples/option.kx --help something
--help
Program options: ["examples/option.kx", "something"]
$ ./kinx examples/option.kx --do-check=
--do-check with ""
Program options: ["examples/option.kx"]
$ ./kinx examples/option.kx --do-check=abc
--do-check with "abc"
Program options: ["examples/option.kx"]
$ ./kinx examples/option.kx -a
Uncaught exception: No one catch the exception.
ArgumentException: Needs an argument for -a
Stack Trace Information:
at <main-block>(examples/option.kx:2)
$ ./kinx examples/option.kx --unknown
Uncaught exception: No one catch the exception.
ArgumentException: Unknown option: --unknown
Stack Trace Information:
at <main-block>(examples/option.kx:2)
There are various methods of option analysis, and getopt
has a long history, but it is still active. From the perspective of ** Most fitting in C programmers **, I think getopt
is easy to use.
Boost :: program_options
is also hard to throw away in the sense that you can get help. The first is support for System.getopt
in the sense that you can do the bare minimum. More useful things may come out in the future (where?).
See you next time.