** "Sieht aus wie JavaScript, Gehirn (Inhalt) ist Ruby, (Stabilität ist AC / DC)" ** Skriptsprache Kinx ). Die Sprache ist die Bibliothek. Also, wie man die Bibliothek benutzt.
Diesmal ist es Getopt. Ich habe es intern in SpecTest implementiert und verwendet, aber ich habe es in die Standardbibliothek verschoben.
Es unterstützt auch die lange Option (~~ Ich habe es noch nicht veröffentlicht ... ~~ Ich habe es veröffentlicht).
Getopt - System.getopt
Geben Sie das Optionsarray, die Optionszeichenfolge und das lange Optionsobjekt an der Position des bedingten Ausdrucks der while-Anweisung an, wie unten gezeigt. Langes Optionsobjekt ist optional.
var opt, add, check;
while (opt = System.getopt($$, "a:df", { add: 'a', delete: 'd', help: null, "do-check": '=' })) {
switch (opt.type) {
case 'a': // '--add'Aber'a'Ist zurück gekommen.
add = opt.arg; // ':'Die Spezifikation gibt an, dass ein Argument vorliegt.
System.println('-a with "%{add}"');
break;
case 'd': // '--delete'Aber'd'Ist zurück gekommen.
System.println('-d');
break;
case 'f': // '-f'Rückkehr mit.
System.println('-f');
break;
case 'help': // '--help'Rückkehr mit.
System.println('--help');
break;
case 'do-check': // '--do-check'Rückkehr mit.
check = opt.arg; // '='Die Spezifikation gibt an, dass ein Argument vorliegt.
System.println('--do-check with "%{check}"');
break;
case '-': //Wenn es keine Option war, kommen Sie hierher.
list.push(opt.arg);
break;
}
}
//Andere als Optionen anzeigen
System.println("Program options: ", list);
Wenn die vorherige Probe betrieben wird, wird sie wie folgt.
$ ./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)
Es gibt verschiedene Methoden zur Optionsanalyse, und getopt hat eine lange Geschichte, ist aber immer noch aktiv. Aus der Sicht von ** Am besten für C-Programmierer geeignet ** finde ich, dass getopt einfach zu bedienen ist.
Boost :: program_options
ist auch in dem Sinne schwer wegzuwerfen, dass man Hilfe bekommen kann. Das erste ist die Unterstützung von "System.getopt" in dem Sinne, dass Sie das Nötigste tun können. Weitere nützliche Dinge könnten in Zukunft herauskommen (wo?).
Bis zum nächsten Mal.