public void parse(String[] args) throws Exception { Assert.preCondition(args != null); if (this.commandLineSpec.getRequiredArgNum() > args.length) this.helpHandler.handleHelp(); int compulsoryArgs = 0; int i = 0; ArgSpec spec; String arg; while (i < args.length) { while (args[i].startsWith(argumentPrefix) == false) i++; // Now we have the first desired arg. arg = args[i].substring(argumentPrefix.length()); if (this.isHelpArg(arg)) this.helpHandler.handleHelp(); spec = this.commandLineSpec.getArg(arg); if (spec == null) { spec = this.commandLineSpec.getRequiredArg(arg); if (spec == null) break; // Make sure that the repetition of the same argument // does not get counted more than once. if (this.argList.get(arg) == null) compulsoryArgs++; } ArgValue argVal = new ArgValue(spec, args, ++i, spec.argNum()); this.argList.put(spec.getName(), argVal); argVal = null; i += spec.argNum(); } if (compulsoryArgs < this.commandLineSpec.getRequiredArgNum()) { this.helpHandler.handleHelp(); // throw new InvalidArgumentValueException("Required Argument Missing!"); } }
public ArgValue getArg(ArgSpec spec) throws AssertionException { Assert.preCondition(spec != null); return (ArgValue) this.argList.get(spec.getName()); }
public ArgParser(CommandLineSpec spec, HelpHandler helpHandler, String argumentPrefix) { Assert.preCondition(argumentPrefix != null && spec != null); this.argumentPrefix = argumentPrefix; this.commandLineSpec = spec; this.helpHandler = helpHandler; }
public ArgValue getArg(String name) { Assert.preCondition(name != null); return (ArgValue) this.argList.get(name); }