[JAVA] Kinx Library --Getopt

Kinx Library --Getopt

Introduction

** "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

How to use

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);

Option string details

Long option details

Run the sample

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)

in conclusion

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.

Recommended Posts

Kinx Library --Getopt
Kinx Library --REPL
Kinx Library --DateTime
Kinx Library --Process
Kinx Library-JIT Compiler Library
Kinx Library-JIT Compiler Library (Extra Edition)