kinx

Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.

This project is maintained by Kray-G

Getopt - System.getopt

Overview

This is Getopt. It is simple but useful. There are a lot of styles for analyzing arguments, and getopt is one of those and has a long history. Now getopt is still active and surviving.

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, list = [];
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);

Option String

Long Option

Examples

Example 1. Option Pattern (1)

Code [args -> -d -a arg]

var opt, add, check, list = [];
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);

Result

-d
-a with "arg"
Program options: ["test.kx"]

Example 2. Option Pattern (2)

Code [args -> -da arg]

var opt, add, check, list = [];
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);

Result

-d
-a with "arg"
Program options: ["test.kx"]

Example 3. Option Pattern (3)

Code [args -> -daarg]

var opt, add, check, list = [];
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);

Result

-d
-a with "arg"
Program options: ["test.kx"]

Example 4. Option Pattern (4)

Code [args -> –help something]

var opt, add, check, list = [];
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);

Result

--help
Program options: ["test.kx", "something"]

Example 5. Option Pattern (5)

Code [args -> –do-check=]

var opt, add, check, list = [];
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);

Result

--do-check with ""
Program options: ["test.kx"]

Example 6. Option Pattern (6)

Code [args -> –do-check=abc]

var opt, add, check, list = [];
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);

Result

--do-check with "abc"
Program options: ["test.kx"]

Example 7. Option Pattern (7)

Code [args -> -a]

var opt, add, check, list = [];
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);

Result

Uncaught exception: No one catch the exception.
ArgumentException: Needs an argument for -a
Stack Trace Information:
        at <main-block>(test.kx:2)

Example 8. Option Pattern (8)

Code [args -> –unknown]

var opt, add, check, list = [];
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);

Result

Uncaught exception: No one catch the exception.
ArgumentException: Unknown option: --unknown
Stack Trace Information:
        at <main-block>(test.kx:2)