Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.
This project is maintained by Kray-G
System.getopt
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.
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);
-df
instead of -d -t
.-aARG
. This means it is same as -a ARG
.-d -a ARG
as either -da ARG
or -daARG
.--long-option=argument
. When it is a long option, the empty argument is also acceptable.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);
-d
-a with "arg"
Program options: ["test.kx"]
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);
-d
-a with "arg"
Program options: ["test.kx"]
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);
-d
-a with "arg"
Program options: ["test.kx"]
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);
--help
Program options: ["test.kx", "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);
--do-check with ""
Program options: ["test.kx"]
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);
--do-check with "abc"
Program options: ["test.kx"]
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);
Uncaught exception: No one catch the exception.
ArgumentException: Needs an argument for -a
Stack Trace Information:
at <main-block>(test.kx:2)
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);
Uncaught exception: No one catch the exception.
ArgumentException: Unknown option: --unknown
Stack Trace Information:
at <main-block>(test.kx:2)