loading...

Kinx Library - Getopt

krayg profile image Kray-G ・3 min read

Hello, everybody!

The script language Kinx is published with the concept of Looks like JavaScript, Feels like Ruby, Stable like AC/DC(?).

This time it is Getopt. It is simple but useful.

You can also use a long option style.

Getopt - System.getopt

How to use

Look at an example below. You put it in the while condition part, and specify the array of argument, the option string, and the object for long options. You can omit to specify the object for long options.

var opt, add, check;
while (opt = System.getopt($$, "a:df", { add: 'a', delete: 'd', help: null, "do-check": '=' })) {
    switch (opt.type) {
    case 'a':               // Returns 'a' when a user specified '--add'.
        add = opt.arg;      // ':' means the option has an argument.
        System.println('-a with "%{add}"');
        break;
    case 'd':               // Returns 'd' when a user specified '--delete'.
        System.println('-d');
        break;
    case 'f':               // This means that a user specified '-f'.
        System.println('-f');
        break;
    case 'help':            // This means that a user specified '--help'.
        System.println('--help');
        break;
    case 'do-check':        // This means that a user specified '--do-check'
        check = opt.arg;    // '=' means the option has an argument.
        System.println('--do-check with "%{check}"');
        break;
    case '-':               // This means that the argument is not an option.
        list.push(opt.arg);
        break;
    }
}

// Displaying arguments which is not an option.
System.println("Program options: ", list);

Details for an option string.

  • If you specify the option with an argument but there is no argument to the option, ArgumentException will be raised.
  • When it is an option without an argument, the options can be combine within one option like -df instead of -d -t.
  • When it is an option with an argument, you can combine the option and the argument like -aARG. This means it is same as -a ARG.
  • By above mechanism, you can write -d -a ARG as either -da ARG or -daARG.

Details for a long option.

  • If you specified a long option with the option character of an option string, the argument style is following an option string parameter.
  • If you specify the option with an argument but there is no argument to the option, ArgumentException will be raised.
  • The argument for a long option is specified as the style of --long-option=argument. When it is a long option, the empty argument is also acceptable.

Execute an example.

Here is an example for some cases.

$ ./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)

Conclusion

There are a lot of style for analyzing arguments, and getopt has a long history. But getopt is still active. I think it is easier to use by the point of view of Most fitting in C programmers.

To display a help, boost::program_options is also useful. But getopt is minimum but very simple and useful for almost all purpose. I might support more useful method in the future.

See you next time.

Posted on by:

krayg profile

Kray-G

@krayg

Although I used to be a C++/Boost lover, I was back to C with the spirit of Zen, or the spirit of "simple is the best". Also returning to rock and roll from metal, I'm really into Rolling Stones.

Discussion

pic
Editor guide