Skip to content

Instantly share code, notes, and snippets.

@stalep
Created July 15, 2015 13:06
Show Gist options
  • Save stalep/7840342051a1b03bd0f7 to your computer and use it in GitHub Desktop.
Save stalep/7840342051a1b03bd0f7 to your computer and use it in GitHub Desktop.
how to parse command line arguments
@CommandDefinition(name = "testparams", description = "")
public class TestParams implements Command {
@Option(shortName = 'f', description = "set the foo value")
private String foo;
@Option(shortName = 'b', required = true)
private String bar;
public static void main(String[] args) {
TestParams test = new TestParams();
try {
ParserGenerator.parseAndPopulate(test, "testparams", args);
System.out.println("you set foo to: " + test.foo);
System.out.println("you set bar to: "+test.bar);
}
catch(CommandLineParserException e) {
System.out.println("Error when parsing: "+e.toString());
}
catch(OptionValidatorException e) {
System.out.println("Validation failed: "+e.toString());
}
}
@Override
//never called
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment