下面列出了怎么用org.apache.commons.cli.MissingArgumentException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testEmptyFrameworkOption() throws Exception {
RunJobCli runJobCli = new RunJobCli(getMockClientContext());
assertFalse(SubmarineLogs.isVerbose());
expectedException.expect(MissingArgumentException.class);
expectedException.expectMessage("Missing argument for option: framework");
runJobCli.run(
new String[]{"--framework", "--name", "my-job",
"--docker_image", "tf-docker:1.1.0",
"--input_path", "hdfs://input", "--checkpoint_path",
"hdfs://output",
"--num_workers", "1", "--worker_launch_cmd", "python run-job.py",
"--worker_resources", "memory=4g,vcores=2", "--tensorboard",
"true", "--verbose", "--wait_job_finish"});
}
protected boolean handleCommonOption(final GfxdOption opt, final String cmd,
final String cmdDescKey) throws ParseException {
if (SYSTEM_PROPERTY.equals(opt.getOpt())) {
String optValue = opt.getValue();
int indexOfEquals = optValue.indexOf('=');
if (indexOfEquals >= 0) {
System.setProperty(optValue.substring(0, indexOfEquals),
optValue.substring(indexOfEquals + 1));
}
else {
throw new MissingArgumentException(opt);
}
}
else if (HELP.equals(opt.getOpt())) {
showUsage(null, cmd, cmdDescKey);
throw new GemFireTerminateError(null, 0);
}
else {
return false;
}
return true;
}
@SuppressWarnings("unchecked")
public static String process(ParseException e) {
if (e instanceof MissingOptionException) {
StringBuilder sb = new StringBuilder();
Iterator<String> options = ((MissingOptionException) e).getMissingOptions().iterator();
while (options.hasNext()) {
sb.append(options.next());
if (options.hasNext()) {
sb.append(", ");
}
}
return String.format("Missing required option(s) %s.", sb.toString());
}
else if (e instanceof MissingArgumentException) {
return String.format("%s is missing a required argument.",
((MissingArgumentException) e).getOption());
}
else if (e instanceof UnrecognizedOptionException) {
return String.format("%s is not a valid option.",
((UnrecognizedOptionException) e).getOption());
}
else {
return String.format("%s.", e.getMessage());
}
}
public static void main(String[] args) throws Exception {
GraphLoader gl = new GraphLoader();
Graph<String, UserNodeValues, UserEdgeValues> graph;
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
if(args.length == 0) {
// read graph from accumulo
graph = null; // TODO : adapt to gl.getUserGraph();
} else if (args.length == 1) {
graph = gl.getUserGraphFromFiles(args[0],env);
} else {
throw new MissingArgumentException("Either use no arguments for graph import from accumulo or the path " +
"to file where the graph is stored.");
}
ConnectionAnalysis ca = new ConnectionAnalysis();
ca.analyze(graph);
}
protected boolean handleCommonOption(final GfxdOption opt, final String cmd,
final String cmdDescKey) throws ParseException {
if (SYSTEM_PROPERTY.equals(opt.getOpt())) {
String optValue = opt.getValue();
int indexOfEquals = optValue.indexOf('=');
if (indexOfEquals >= 0) {
System.setProperty(optValue.substring(0, indexOfEquals),
optValue.substring(indexOfEquals + 1));
}
else {
throw new MissingArgumentException(opt);
}
}
else if (HELP.equals(opt.getOpt())) {
showUsage(null, cmd, cmdDescKey);
throw new GemFireTerminateError(null, 0);
}
else {
return false;
}
return true;
}
@Override
/**
* Processes arguments to options but only strips matched quotes.
*/
public void processArgs(Option opt, ListIterator iter)
throws ParseException {
// Loop until an option is found.
while (iter.hasNext()) {
String str = (String) iter.next();
if (getOptions().hasOption(str) && str.startsWith("-")) {
// found an Option, not an argument.
iter.previous();
break;
}
// Otherwise, this is a value.
try {
// Note that we only strip matched quotes here.
addValForProcessing.invoke(opt, stripMatchedQuotes(str));
} catch (IllegalAccessException iae) {
throw new RuntimeException(iae);
} catch (java.lang.reflect.InvocationTargetException ite) {
// Any runtime exception thrown within addValForProcessing()
// will be wrapped in an InvocationTargetException.
iter.previous();
break;
} catch (RuntimeException re) {
iter.previous();
break;
}
}
if (opt.getValues() == null && !opt.hasOptionalArg()) {
throw new MissingArgumentException(opt);
}
}
@Override
public int run(String[] args) throws Exception {
Options opts = new Options();
opts.addOption(STATUS_CMD, true,
"List queue information about given queue.");
opts.addOption(HELP_CMD, false, "Displays help for all commands.");
opts.getOption(STATUS_CMD).setArgName("Queue Name");
CommandLine cliParser = null;
try {
cliParser = new GnuParser().parse(opts, args);
} catch (MissingArgumentException ex) {
sysout.println("Missing argument for options");
printUsage(opts);
return -1;
}
if (cliParser.hasOption(STATUS_CMD)) {
if (args.length != 2) {
printUsage(opts);
return -1;
}
return listQueue(cliParser.getOptionValue(STATUS_CMD));
} else if (cliParser.hasOption(HELP_CMD)) {
printUsage(opts);
return 0;
} else {
syserr.println("Invalid Command Usage : ");
printUsage(opts);
return -1;
}
}
@Override
public int run(String[] args) throws Exception {
Options opts = new Options();
opts.addOption(STATUS_CMD, true,
"List queue information about given queue.");
opts.addOption(HELP_CMD, false, "Displays help for all commands.");
opts.getOption(STATUS_CMD).setArgName("Queue Name");
CommandLine cliParser = null;
try {
cliParser = new GnuParser().parse(opts, args);
} catch (MissingArgumentException ex) {
sysout.println("Missing argument for options");
printUsage(opts);
return -1;
}
if (cliParser.hasOption(STATUS_CMD)) {
if (args.length != 2) {
printUsage(opts);
return -1;
}
return listQueue(cliParser.getOptionValue(STATUS_CMD));
} else if (cliParser.hasOption(HELP_CMD)) {
printUsage(opts);
return 0;
} else {
syserr.println("Invalid Command Usage : ");
printUsage(opts);
return -1;
}
}
@Test(expected = MissingArgumentException.class)
public void testParseArgumentsMissingArgumentException() throws ParseException
{
ArgumentParser argParser = new ArgumentParser("");
argParser.addArgument("a", "some_optional_parameter", true, "Some optional parameter with an argument", false);
argParser.parseArguments(new String[] {"-a"});
}
/**
* If a timestamp option is given, it must contain a field name
*/
@Test
public void mustDefineFieldnametoGoWithTimestamp() {
String[] args = new String[] {
"--timestampfield"
};
assertThrows(MissingArgumentException.class, () -> BatchProfilerCLI.main(args));
}
@Override
public void execute(CommandLine options) throws Exception {
String[] args = options.getArgs();
if (args.length < min) {
throw new MissingArgumentException("missing required arguments");
}
if (args.length > max) {
throw new UnrecognizedOptionException("unknown extra argument \"" + args[max] + "\"");
}
}
public static String getFromConf(Configuration conf, String property)
throws MissingArgumentException {
String propValue = conf.get(property);
if (propValue == null)
throw new MissingArgumentException("Forgot to set " + property + " in your script");
return propValue;
}
@Override
public void execute(CommandLine options) throws Exception {
String[] args = options.getArgs();
if (args.length < min) {
throw new MissingArgumentException("missing required arguments");
}
if (args.length > max) {
throw new UnrecognizedOptionException("unknown extra argument \"" + args[max] + "\"");
}
}
@Override
public int run(String[] args) throws Exception {
Options opts = new Options();
opts.addOption("lnl", LIST_LABELS_CMD, false,
"List cluster node-label collection");
opts.addOption("h", HELP_CMD, false, "Displays help for all commands.");
opts.addOption("dnl", DIRECTLY_ACCESS_NODE_LABEL_STORE, false,
"Directly access node label store, "
+ "with this option, all node label related operations"
+ " will NOT connect RM. Instead, they will"
+ " access/modify stored node labels directly."
+ " By default, it is false (access via RM)."
+ " AND PLEASE NOTE: if you configured "
+ YarnConfiguration.FS_NODE_LABELS_STORE_ROOT_DIR
+ " to a local directory"
+ " (instead of NFS or HDFS), this option will only work"
+ " when the command run on the machine where RM is running."
+ " Also, this option is UNSTABLE, could be removed in future"
+ " releases.");
int exitCode = -1;
CommandLine parsedCli = null;
try {
parsedCli = new GnuParser().parse(opts, args);
} catch (MissingArgumentException ex) {
sysout.println("Missing argument for options");
printUsage(opts);
return exitCode;
}
if (parsedCli.hasOption(DIRECTLY_ACCESS_NODE_LABEL_STORE)) {
accessLocal = true;
}
if (parsedCli.hasOption(LIST_LABELS_CMD)) {
printClusterNodeLabels();
} else if (parsedCli.hasOption(HELP_CMD)) {
printUsage(opts);
return 0;
} else {
syserr.println("Invalid Command Usage : ");
printUsage(opts);
}
return 0;
}
@Override
public int run(String[] args) throws Exception {
Options opts = new Options();
opts.addOption(HELP_CMD, false, "Displays help for all commands.");
opts.addOption(STATUS_CMD, true, "Prints the status report of the node.");
opts.addOption(LIST_CMD, false, "List all running nodes. " +
"Supports optional use of -states to filter nodes " +
"based on node state, all -all to list all nodes.");
Option nodeStateOpt = new Option(NODE_STATE_CMD, true,
"Works with -list to filter nodes based on input comma-separated list of node states.");
nodeStateOpt.setValueSeparator(',');
nodeStateOpt.setArgs(Option.UNLIMITED_VALUES);
nodeStateOpt.setArgName("States");
opts.addOption(nodeStateOpt);
Option allOpt = new Option(NODE_ALL, false,
"Works with -list to list all nodes.");
opts.addOption(allOpt);
opts.getOption(STATUS_CMD).setArgName("NodeId");
int exitCode = -1;
CommandLine cliParser = null;
try {
cliParser = new GnuParser().parse(opts, args);
} catch (MissingArgumentException ex) {
sysout.println("Missing argument for options");
printUsage(opts);
return exitCode;
}
if (cliParser.hasOption("status")) {
if (args.length != 2) {
printUsage(opts);
return exitCode;
}
printNodeStatus(cliParser.getOptionValue("status"));
} else if (cliParser.hasOption("list")) {
Set<NodeState> nodeStates = new HashSet<NodeState>();
if (cliParser.hasOption(NODE_ALL)) {
for (NodeState state : NodeState.values()) {
nodeStates.add(state);
}
} else if (cliParser.hasOption(NODE_STATE_CMD)) {
String[] types = cliParser.getOptionValues(NODE_STATE_CMD);
if (types != null) {
for (String type : types) {
if (!type.trim().isEmpty()) {
nodeStates.add(NodeState.valueOf(
org.apache.hadoop.util.StringUtils.toUpperCase(type.trim())));
}
}
}
} else {
nodeStates.add(NodeState.RUNNING);
}
listClusterNodes(nodeStates);
} else if (cliParser.hasOption(HELP_CMD)) {
printUsage(opts);
return 0;
} else {
syserr.println("Invalid Command Usage : ");
printUsage(opts);
}
return 0;
}
@Override
public int run(String[] args) throws Exception {
Options opts = new Options();
opts.addOption("lnl", LIST_LABELS_CMD, false,
"List cluster node-label collection");
opts.addOption("h", HELP_CMD, false, "Displays help for all commands.");
opts.addOption("dnl", DIRECTLY_ACCESS_NODE_LABEL_STORE, false,
"Directly access node label store, "
+ "with this option, all node label related operations"
+ " will NOT connect RM. Instead, they will"
+ " access/modify stored node labels directly."
+ " By default, it is false (access via RM)."
+ " AND PLEASE NOTE: if you configured "
+ YarnConfiguration.FS_NODE_LABELS_STORE_ROOT_DIR
+ " to a local directory"
+ " (instead of NFS or HDFS), this option will only work"
+ " when the command run on the machine where RM is running."
+ " Also, this option is UNSTABLE, could be removed in future"
+ " releases.");
int exitCode = -1;
CommandLine parsedCli = null;
try {
parsedCli = new GnuParser().parse(opts, args);
} catch (MissingArgumentException ex) {
sysout.println("Missing argument for options");
printUsage(opts);
return exitCode;
}
if (parsedCli.hasOption(DIRECTLY_ACCESS_NODE_LABEL_STORE)) {
accessLocal = true;
}
if (parsedCli.hasOption(LIST_LABELS_CMD)) {
printClusterNodeLabels();
} else if (parsedCli.hasOption(HELP_CMD)) {
printUsage(opts);
return 0;
} else {
syserr.println("Invalid Command Usage : ");
printUsage(opts);
}
return 0;
}
@Override
public int run(String[] args) throws Exception {
Options opts = new Options();
opts.addOption(HELP_CMD, false, "Displays help for all commands.");
opts.addOption(STATUS_CMD, true, "Prints the status report of the node.");
opts.addOption(LIST_CMD, false, "List all running nodes. " +
"Supports optional use of -states to filter nodes " +
"based on node state, all -all to list all nodes.");
Option nodeStateOpt = new Option(NODE_STATE_CMD, true,
"Works with -list to filter nodes based on input comma-separated list of node states.");
nodeStateOpt.setValueSeparator(',');
nodeStateOpt.setArgs(Option.UNLIMITED_VALUES);
nodeStateOpt.setArgName("States");
opts.addOption(nodeStateOpt);
Option allOpt = new Option(NODE_ALL, false,
"Works with -list to list all nodes.");
opts.addOption(allOpt);
opts.getOption(STATUS_CMD).setArgName("NodeId");
int exitCode = -1;
CommandLine cliParser = null;
try {
cliParser = new GnuParser().parse(opts, args);
} catch (MissingArgumentException ex) {
sysout.println("Missing argument for options");
printUsage(opts);
return exitCode;
}
if (cliParser.hasOption("status")) {
if (args.length != 2) {
printUsage(opts);
return exitCode;
}
printNodeStatus(cliParser.getOptionValue("status"));
} else if (cliParser.hasOption("list")) {
Set<NodeState> nodeStates = new HashSet<NodeState>();
if (cliParser.hasOption(NODE_ALL)) {
for (NodeState state : NodeState.values()) {
nodeStates.add(state);
}
} else if (cliParser.hasOption(NODE_STATE_CMD)) {
String[] types = cliParser.getOptionValues(NODE_STATE_CMD);
if (types != null) {
for (String type : types) {
if (!type.trim().isEmpty()) {
nodeStates.add(NodeState.valueOf(
org.apache.hadoop.util.StringUtils.toUpperCase(type.trim())));
}
}
}
} else {
nodeStates.add(NodeState.RUNNING);
}
listClusterNodes(nodeStates);
} else if (cliParser.hasOption(HELP_CMD)) {
printUsage(opts);
return 0;
} else {
syserr.println("Invalid Command Usage : ");
printUsage(opts);
}
return 0;
}
@Override
public int run(final String[] args) throws Exception {
final Configuration conf = getConf();
conf.set("tableName", ingestOptions.getQualifiedTableName());
conf.set("osmVisibility", ingestOptions.getVisibilityOptions().getVisibility());
// job settings
final Job job = Job.getInstance(conf, ingestOptions.getJobName());
job.setJarByClass(OSMRunner.class);
switch (ingestOptions.getMapperType()) {
case "NODE": {
configureSchema(AvroNode.getClassSchema());
inputAvroFile = ingestOptions.getNodesBasePath();
job.setMapperClass(OSMNodeMapper.class);
break;
}
case "WAY": {
configureSchema(AvroWay.getClassSchema());
inputAvroFile = ingestOptions.getWaysBasePath();
job.setMapperClass(OSMWayMapper.class);
break;
}
case "RELATION": {
configureSchema(AvroRelation.getClassSchema());
inputAvroFile = ingestOptions.getRelationsBasePath();
job.setMapperClass(OSMRelationMapper.class);
break;
}
default:
break;
}
if ((avroSchema == null) || (inputAvroFile == null)) {
throw new MissingArgumentException(
"argument for mapper type must be one of: NODE, WAY, or RELATION");
}
enableLocalityGroups(ingestOptions);
// input format
job.setInputFormatClass(AvroKeyInputFormat.class);
FileInputFormat.setInputPaths(job, inputAvroFile);
AvroJob.setInputKeySchema(job, avroSchema);
// mappper
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Mutation.class);
job.setOutputFormatClass(AccumuloOutputFormat.class);
AccumuloOutputFormat.setConnectorInfo(
job,
accumuloOptions.getUser(),
new PasswordToken(accumuloOptions.getPassword()));
AccumuloOutputFormat.setCreateTables(job, true);
AccumuloOutputFormat.setDefaultTableName(job, ingestOptions.getQualifiedTableName());
AccumuloOutputFormat.setZooKeeperInstance(
job,
new ClientConfiguration().withInstance(accumuloOptions.getInstance()).withZkHosts(
accumuloOptions.getZookeeper()));
// reducer
job.setNumReduceTasks(0);
return job.waitForCompletion(true) ? 0 : -1;
}
@Test(expected = MissingArgumentException.class)
public void missingUserValue() throws ParseException, MissingConfigException {
String[] args = new String[] { "-u", "-pass", "password", "-s", "server", "-d", "localDir", "-port", "22" };
propUtils.getUploadProperties(args);
}
@Test(expected = MissingArgumentException.class)
public void missingPasswordValue() throws ParseException, MissingConfigException {
String[] args = new String[] { "-u", "user", "-pass", "-s", "server", "-d", "localDir", "-port", "22" };
propUtils.getUploadProperties(args);
}
@Test(expected = MissingArgumentException.class)
public void missingServerValue() throws ParseException, MissingConfigException {
String[] args = new String[] { "-u", "user", "-pass", "password", "-s", "-d", "localDir", "-port", "22" };
propUtils.getUploadProperties(args);
}
@Test(expected = MissingArgumentException.class)
public void missingLocalDirValue() throws ParseException, MissingConfigException {
String[] args = new String[] { "-u", "user", "-pass", "password", "-s", "server", "-d", "-port", "22" };
propUtils.getUploadProperties(args);
}
@Test(expected = MissingArgumentException.class)
public void missingPortValue() throws ParseException, MissingConfigException {
String[] args = new String[] { "-u", "user", "-pass", "password", "-s", "server", "-d", "localDir", "-port" };
propUtils.getUploadProperties(args);
}