@Test public void testParseFailure() throws Exception { String[] argv = new String[]{"-a"}; Getopt instance = new Getopt(argv, "a:"); try { instance.parse(); fail("Parse shall not allow missing arguments"); } catch (ParseException exp) { if (!exp.getMessage().contains("requires an argument")) { throw exp; } } instance = new Getopt(argv, "b"); try { instance.parse(); fail("Parse shall not allow unknown arguments"); } catch (ParseException exp) { if (!exp.getMessage().contains("Unknown argument: ")) { throw exp; } } }
public void parse() throws ParseException { int ii = 0; while (ii < argv.length) { char[] chars = argv[ii].toCharArray(); if (chars.length > 0 && chars[0] == '-') { if (argv[ii].equals("--")) { optind = ii + 1; break; } for (int jj = 1; jj < chars.length; ++jj) { int idx = opts.indexOf(chars[jj]); if (idx == -1) { throw new ParseException("Unknown argument: " + argv[ii].substring(jj), ii); } Option option = new Option(); option.option = chars[jj]; options.add(option); if ((idx + 1) < opts.length() && (opts.charAt(idx + 1) == ':')) { if ((jj + 1) < chars.length) { option.argument = argv[ii].substring(jj + 1); break; } ++ii; if (ii < argv.length) { option.argument = argv[ii]; } else { throw new ParseException("Option " + chars[jj] + " requires an argument", ii); } } } ++ii; } else { optind = ii; break; } } }
Getopt { public void parse() throws ParseException { int ii = 0; while (ii < argv.length) { char[] chars = argv[ii].toCharArray(); if (chars.length > 0 && chars[0] == '-') { if (argv[ii].equals("--")) { optind = ii + 1; break; } for (int jj = 1; jj < chars.length; ++jj) { int idx = opts.indexOf(chars[jj]); if (idx == -1) { throw new ParseException("Unknown argument: " + argv[ii].substring(jj), ii); } Option option = new Option(); option.option = chars[jj]; options.add(option); if ((idx + 1) < opts.length() && (opts.charAt(idx + 1) == ':')) { if ((jj + 1) < chars.length) { option.argument = argv[ii].substring(jj + 1); break; } ++ii; if (ii < argv.length) { option.argument = argv[ii]; } else { throw new ParseException("Option " + chars[jj] + " requires an argument", ii); } } } ++ii; } else { optind = ii; break; } } } }
Getopt { public void parse() throws ParseException { int ii = 0; while (ii < argv.length) { char[] chars = argv[ii].toCharArray(); if (chars.length > 0 && chars[0] == '-') { if (argv[ii].equals("--")) { optind = ii + 1; break; } for (int jj = 1; jj < chars.length; ++jj) { int idx = opts.indexOf(chars[jj]); if (idx == -1) { throw new ParseException("Unknown argument: " + argv[ii].substring(jj), ii); } Option option = new Option(); option.option = chars[jj]; options.add(option); if ((idx + 1) < opts.length() && (opts.charAt(idx + 1) == ':')) { if ((jj + 1) < chars.length) { option.argument = argv[ii].substring(jj + 1); break; } ++ii; if (ii < argv.length) { option.argument = argv[ii]; } else { throw new ParseException("Option " + chars[jj] + " requires an argument", ii); } } } ++ii; } else { optind = ii; break; } } } Getopt(String[] argv, String opts); }
Getopt { public void parse() throws ParseException { int ii = 0; while (ii < argv.length) { char[] chars = argv[ii].toCharArray(); if (chars.length > 0 && chars[0] == '-') { if (argv[ii].equals("--")) { optind = ii + 1; break; } for (int jj = 1; jj < chars.length; ++jj) { int idx = opts.indexOf(chars[jj]); if (idx == -1) { throw new ParseException("Unknown argument: " + argv[ii].substring(jj), ii); } Option option = new Option(); option.option = chars[jj]; options.add(option); if ((idx + 1) < opts.length() && (opts.charAt(idx + 1) == ':')) { if ((jj + 1) < chars.length) { option.argument = argv[ii].substring(jj + 1); break; } ++ii; if (ii < argv.length) { option.argument = argv[ii]; } else { throw new ParseException("Option " + chars[jj] + " requires an argument", ii); } } } ++ii; } else { optind = ii; break; } } } Getopt(String[] argv, String opts); void parse(); int getOpt(); void reset(); String getOptarg(); int getOptind(); }
Getopt { public void parse() throws ParseException { int ii = 0; while (ii < argv.length) { char[] chars = argv[ii].toCharArray(); if (chars.length > 0 && chars[0] == '-') { if (argv[ii].equals("--")) { optind = ii + 1; break; } for (int jj = 1; jj < chars.length; ++jj) { int idx = opts.indexOf(chars[jj]); if (idx == -1) { throw new ParseException("Unknown argument: " + argv[ii].substring(jj), ii); } Option option = new Option(); option.option = chars[jj]; options.add(option); if ((idx + 1) < opts.length() && (opts.charAt(idx + 1) == ':')) { if ((jj + 1) < chars.length) { option.argument = argv[ii].substring(jj + 1); break; } ++ii; if (ii < argv.length) { option.argument = argv[ii]; } else { throw new ParseException("Option " + chars[jj] + " requires an argument", ii); } } } ++ii; } else { optind = ii; break; } } } Getopt(String[] argv, String opts); void parse(); int getOpt(); void reset(); String getOptarg(); int getOptind();  }