Permalink
Browse files

Put getopt into its own namespace

  • Loading branch information...
1 parent 3b383bd commit 4c2b430f1cc28a3991023c30471e7567e5ca0013 @wezm committed Aug 27, 2009
Showing with 72 additions and 101 deletions.
  1. +64 −58 getopt.j
  2. +8 −43 main.j
View
122 getopt.j
@@ -10,75 +10,81 @@
// Wesley Moore, http://wezm.net/, Public Domain
// External variables:
-// optind -- index in ARGV of first nonoption argument
-// optarg -- string value of argument to current option
-// opterr -- if nonzero, print our own diagnostic
-// optopt -- current option letter
+// optind = 0; // -- index in ARGV of first nonoption argument
+// optarg; // -- string value of argument to current option
+// opterr = 1; // -- if nonzero, print our own diagnostic
+// optopt; // -- current option letter
// Returns:
-// null at end of options
+// null at end of options
// ? for unrecognized option
// <c> a character representing the current option
// Private Data:
// _opti -- index in multi-flag option, e.g., -abc
-function getopt(args, options, thisopt, i)
-{
- if (options.length == 0) // no options given
- return null;
- if (args[optind] == "--") { // all done
- optind++;
- _opti = 0;
- return null;
- } else if (argv[optind] !~ /^-[^: \t\n\f\r\v\b]/) {
- _opti = 0;
- return null;
- }
- if (_opti == 0)
- _opti = 2;
- thisopt = args[optind].charAt(_opti);
- optopt = thisopt;
- i = options.indexOf(thisopt);
- if (i == -1) {
- if (opterr)
- print(thisopt + " -- invalid option\n";
- if (_opti >= args[optind].length {
+GetOpt = {
+ optind: 0,
+ optarg: null,
+ opterr: 1,
+ optopt: null,
+ _opti: null,
+ getopt: function(args, options, thisopt, i) {
+ if (options.length == 0) // no options given
+ return null;
+
+ if (args[optind] == "--") { // all done
+ optind++;
+ _opti = 0;
+ return null;
+ } else if (argv[optind].match(/^-[^: \t\n\f\r\v\b]/)) {
+ _opti = 0;
+ return null;
+ }
+ if (_opti == 0)
+ _opti = 2;
+ thisopt = args[optind].charAt(_opti);
+ optopt = thisopt;
+ i = options.indexOf(thisopt);
+ if (i == -1) {
+ if (opterr)
+ print(thisopt + " -- invalid option");
+ if (_opti >= args[optind].length {
+ optind++;
+ _opti = 0;
+ } else
+ _opti++;
+ return "?";
+ }
+ if ( options.charAt(i + 1) == ":") {
+ // get option argument
+ if (args[optind].charAt(_opti + 1) > 0) //length(substr(args[optind], _opti + 1)) > 0)
+ optarg = args[optind].charAt(_opti + 1); //substr(args[optind], _opti + 1)
+ else
+ optarg = args[++optind];
+ _opti = 0;
+ } else
+ optarg = "";
+ if (_opti == 0 || _opti >= args[optind].length) {
optind++;
_opti = 0;
} else
_opti++;
- return "?"
+ return thisopt;
}
- if ( options.charAt(i + 1) == ":") {
- // get option argument
- if (args[optind].charAt(_opti + 1) > 0) //length(substr(args[optind], _opti + 1)) > 0)
- optarg = args[optind].charAt(_opti + 1); //substr(args[optind], _opti + 1)
- else
- optarg = args[++optind];
- _opti = 0;
- } else
- optarg = "";
- if (_opti == 0 || _opti >= args[optind].length) {
- optind++;
- _opti = 0;
- } else
- _opti++;
- return thisopt;
-}
-
-BEGIN {
- opterr = 1 # default is to diagnose
- optind = 1 # skip ARGV[0]
+};
- # test program
- if (_getopt_test) {
- while ((_go_c = getopt(ARGC, ARGV, "ab:cd")) != -1)
- printf("c = <%c>, optarg = <%s>\n",
- _go_c, optarg)
- printf("non-option arguments:\n")
- for (; optind < ARGC; optind++)
- printf("\tARGV[%d] = <%s>\n",
- optind, ARGV[optind])
- }
-}
+// function getopt_init()
+// {
+//
+// // test program
+// if (_getopt_test) {
+// while ((_go_c = getopt(ARGC, ARGV, "ab:cd")) != -1)
+// printf("c = <%c>, optarg = <%s>\n",
+// _go_c, optarg)
+// printf("non-option arguments:\n")
+// for (; optind < ARGC; optind++)
+// printf("\tARGV[%d] = <%s>\n",
+// optind, ARGV[optind])
+// }
+// }
View
51 main.j
@@ -1,5 +1,6 @@
@import <Foundation/Foundation.j>
+@import "getopt.j"
@import "OJTestCase.j"
@import "OJTestSuite.j"
@import "OJTestResult.j"
@@ -98,51 +99,15 @@ CPLogRegister(CPLogPrint, "warn");
@end
-function printUsage()
-{
- print("Usage: ojtest ");
- quit();
-}
+var _go_c;
+while ((_go_c = getopt(ARGC, ARGV, "ab:cd")) != null)
+ print("c = <" + _go_c + ">, optarg = <%s>\n", GetOpt.optarg);
-function processArgs(args)
-{
- if (args.length < 1)
- return printUsage();
-
- var index = 0,
- count = args.length;
-
- var unprocessed_args = [];
- for (; index < count; ++index)
- {
- var argument = args[index];
-
- switch (argument)
- {
- case "-v":
- case "--version":
- print("ojtest version 0.7.1");
- quit();
-
- case "-h":
- case "--help":
- printUsage();
- break;
-
- case "-I":
-
- break;
-
- default:
- unprocessed_args.push(argument);
- }
- }
-
- return unprocessed_args;
-}
+print("non-option arguments:");
+for (; GetOpt.optind < args.length; GetOpt.optind++)
+ print("\targs[" + GetOpt.optind + "] = <" + args[GetOpt.optind] + ">");
-args = processArgs(args);
-print(args);
+//print(args);
runner = [[OJTestRunnerText alloc] init];
[runner startWithArguments:args];

0 comments on commit 4c2b430

Please sign in to comment.