本文整理汇总了Java中net.sourceforge.argparse4j.inf.ArgumentParser类的典型用法代码示例。如果您正苦于以下问题:Java ArgumentParser类的具体用法?Java ArgumentParser怎么用?Java ArgumentParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ArgumentParser类属于net.sourceforge.argparse4j.inf包,在下文中一共展示了ArgumentParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadFromCLI
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
/**
* Reads a configuration file from the passed CLI args
*
* @return Path to config file, or exception
* @throws ConfigParseException if arguments were incorrectly specified, or nothing was passed (in which case it will print "usage" details)
*/
public static String loadFromCLI(String programName, String... args) throws ConfigParseException {
ArgumentParser parser = ArgumentParsers.newArgumentParser("java -jar " + programName + "-[VERSION].jar").defaultHelp(true);
parser.addArgument("--config").metavar("/path/to/app-config.json").required(true).help("Path to configuration file");
try {
// parse CLI args and return config file
Namespace cli = parser.parseArgs(args);
return cli.getString("config");
} catch (ArgumentParserException e) {
// show help message and stop execution
parser.handleError(e);
throw new ConfigParseException(e);
}
}
示例2: main
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
public static void main(String[] args) {
ArgumentParser parser = argParser();
if (args.length == 0) {
parser.printHelp();
Exit.exit(0);
}
try {
final VerifiableConsumer consumer = createFromArgs(parser, args);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
consumer.close();
}
});
consumer.run();
} catch (ArgumentParserException e) {
parser.handleError(e);
Exit.exit(1);
}
}
示例3: run
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
@Override
public void run(final ArgumentParser parser, final Argument arg, final Map<String, Object> attrs, final String flag, final Object value) throws ArgumentParserException {
@SuppressWarnings("unchecked")
final String paramValues = ((List<String>) value).get(0);
final Set<String> unique = new HashSet<>(Arrays.asList(paramValues.split(",")));
final String[] protocols = unique.toArray(new String[unique.size()]);
if (!(protocols.length == 1 && protocols[0].equals("all"))) {
for (final String protocol : protocols) {
if (!(protocol.equals("ipv4") || protocol.equals("udp") || protocol.equals("icmp"))) {
throw new ArgumentParserException("invalid protocol: " + protocol, parser, arg);
}
}
}
attrs.put(arg.getDest(), new ArrayList<String>(Arrays.asList(protocols)));
}
示例4: run
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
@Override
public void run(final ArgumentParser parser, final Argument arg,
final Map<String, Object> attrs, final String flag, final Object value) throws ArgumentParserException {
@SuppressWarnings("unchecked")
final List<String> values = (List<String>) value;
final String key = values.get(0);
if (key.length() != 32 || !Util.stringNumInRadix(key, 16)) {
throw new ArgumentParserException("AES key must be a 32 character hexidecimal string (128-bit)", parser, arg);
}
final String maskString = values.get(1);
final String maskValueString = maskString.substring(1);
if (!maskString.startsWith("/") || !Util.stringNumInRadix(maskValueString, 10)) {
throw new ArgumentParserException("IPv6 mask must be an integer prefixed with /", parser, arg);
}
final int mask = Integer.parseInt(maskValueString);
if (mask < 0 || mask > 120) {
throw new ArgumentParserException("IPv6 mask must be in range [0, 120]", parser, arg);
}
attrs.put(arg.getDest(), value);
}
示例5: run
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
@Override
public void run(final ArgumentParser parser, final Argument arg,
final Map<String, Object> attrs, final String flag, final Object value) throws ArgumentParserException {
@SuppressWarnings("unchecked")
final List<String> values = (List<String>) value;
final String key = values.get(0);
if (key.length() != 32 || !Util.stringNumInRadix(key, 16)) {
throw new ArgumentParserException("AES key must be a 32 character hexidecimal string (128-bit)", parser, arg);
}
final String maskString = values.get(1);
final String maskValueString = maskString.substring(1);
if (!maskString.startsWith("/") || !Util.stringNumInRadix(maskValueString, 10)) {
throw new ArgumentParserException("IPv4 mask must be an integer prefixed with /", parser, arg);
}
final int mask = Integer.parseInt(maskValueString);
if (mask < 0 || mask > 24) {
throw new ArgumentParserException("IPv4 mask must be in range [0, 24]", parser, arg);
}
attrs.put(arg.getDest(), value);
}
示例6: run
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
@Override
public void run(ArgumentParser parser, Argument arg,
Map<String, Object> attrs, String flag, Object value)
throws ArgumentParserException {
try {
attrs.put(arg.getDest(),
ParameterUtil.parseDateHour(value.toString()));
} catch (DateTimeParseException dateHourException) {
try {
attrs.put(arg.getDest(),
ParameterUtil.parseDate(value.toString()));
} catch (Exception dateException) {
throw new ArgumentParserException(
String.format(
"could not parse date/datehour for parameter '%s'; if datehour: [%s], if date: [%s]",
arg.textualName(), dateHourException.getMessage(), dateException.getMessage()),
parser);
}
}
}
示例7: doMain
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
protected void doMain(String args[]) {
ArgumentParser parser = ArgumentParsers.newArgumentParser(getClass().getSimpleName());
parser.addArgument("--out")
.dest("out")
.metavar("FILE")
.nargs("?")
.setDefault("stdout")
.help("Writes the script to the output file; default is stdout");
addArguments(parser);
Namespace namespace = parser.parseArgsOrFail(args);
String file = namespace.getString("out");
try (PrintStream out = file.equals("stdout") ? System.out : new PrintStream(new FileOutputStream(file))) {
generateScript(namespace, out);
} catch (IOException e) {
System.err.println("Script generation failed");
e.printStackTrace(System.err);
}
}
示例8: main
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
public static void main(String[] args) {
ArgumentParser parser = ArgumentParsers.newArgumentParser("AngusMe");
parser.description("Angus SDK configurator");
parser.addArgument("-s", "--show").action(Arguments.storeTrue())
.help("display current configuration if exists");
parser.addArgument("-d", "--delete").action(Arguments.storeTrue())
.help("remove current configuration if exists");
try {
Namespace res = parser.parseArgs(args);
if (res.getBoolean("show")) {
show();
} else if (res.getBoolean("delete")) {
delete();
} else {
update();
}
} catch (ArgumentParserException e) {
parser.handleError(e);
}
}
示例9: main
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
public static void main(String... args) {
WSFRealtimeMain m = new WSFRealtimeMain();
ArgumentParser parser = ArgumentParsers.newArgumentParser("wsf-gtfsrealtime");
parser.description("Produces a GTFS-realtime feed from the Washington State Ferries API");
parser.addArgument("--" + ARG_CONFIG_FILE).type(File.class).help("configuration file path");
Namespace parsedArgs;
try {
parsedArgs = parser.parseArgs(args);
File configFile = parsedArgs.get(ARG_CONFIG_FILE);
m.run(configFile);
} catch (CreationException | ConfigurationException | ProvisionException e) {
_log.error("Error in startup:", e);
System.exit(-1);
} catch (ArgumentParserException ex) {
parser.handleError(ex);
}
}
示例10: main
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
ArgumentParser parser = ArgumentParsers.newArgumentParser("ImpressionsToInfluxDb")
.defaultHelp(true)
.description("Read Seldon impressions and send stats to influx db");
parser.addArgument("-t", "--topic").setDefault("impressions").help("Kafka topic to read from");
parser.addArgument("-k", "--kafka").setDefault("localhost:9092").help("Kafka server and port");
parser.addArgument("-z", "--zookeeper").setDefault("localhost:2181").help("Zookeeper server and port");
parser.addArgument("-i", "--influxdb").setDefault("localhost:8086").help("Influxdb server and port");
parser.addArgument("-u", "--influx-user").setDefault("root").help("Influxdb user");
parser.addArgument("-p", "--influx-password").setDefault("root").help("Influxdb password");
parser.addArgument("-d", "--influx-database").setDefault("seldon").help("Influxdb database");
parser.addArgument("--influx-measurement-impressions").setDefault("impressions").help("Influxdb impressions measurement");
parser.addArgument("--influx-measurement-requests").setDefault("requests").help("Influxdb requests measurement");
Namespace ns = null;
try {
ns = parser.parseArgs(args);
ImpressionsToInfluxDb.process(ns);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
}
}
示例11: main
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
ArgumentParser parser = ArgumentParsers.newArgumentParser("PredictionsToInfluxDb")
.defaultHelp(true)
.description("Read Seldon predictions and send stats to influx db");
parser.addArgument("-t", "--topic").setDefault("Predictions").help("Kafka topic to read from");
parser.addArgument("-k", "--kafka").setDefault("localhost:9092").help("Kafka server and port");
parser.addArgument("-z", "--zookeeper").setDefault("localhost:2181").help("Zookeeper server and port");
parser.addArgument("-i", "--influxdb").setDefault("localhost:8086").help("Influxdb server and port");
parser.addArgument("-u", "--influx-user").setDefault("root").help("Influxdb user");
parser.addArgument("-p", "--influx-password").setDefault("root").help("Influxdb password");
parser.addArgument("-d", "--influx-database").setDefault("seldon").help("Influxdb database");
parser.addArgument("--influx-measurement").setDefault("predictions").help("Influxdb Predictions measurement");
Namespace ns = null;
try {
ns = parser.parseArgs(args);
PredictionsToInfluxDb.process(ns);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
}
}
示例12: verifyZKStructure
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
private static void verifyZKStructure(Options opts, ArgumentParser parser) throws ArgumentParserException {
if (opts.zkHost != null) {
assert opts.collection != null;
ZooKeeperInspector zki = new ZooKeeperInspector();
try {
opts.shardUrls = zki.extractShardUrls(opts.zkHost, opts.collection);
} catch (Exception e) {
LOG.debug("Cannot extract SolrCloud shard URLs from ZooKeeper", e);
throw new ArgumentParserException(e, parser);
}
assert opts.shardUrls != null;
if (opts.shardUrls.size() == 0) {
throw new ArgumentParserException("--zk-host requires ZooKeeper " + opts.zkHost
+ " to contain at least one SolrCore for collection: " + opts.collection, parser);
}
opts.shards = opts.shardUrls.size();
LOG.debug("Using SolrCloud shard URLs: {}", opts.shardUrls);
}
}
示例13: main
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
public static void main(String[] args) {
ArgumentParser parser = ArgumentParsers.newArgumentParser("AdlChecker")
.defaultHelp(true)
.description("Checks the syntax of ADL files");
parser.addArgument("file").nargs("*")
.help("File to calculate checksum");
Namespace ns = null;
try {
ns = parser.parseArgs(args);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
}
if(ns.getList("file").isEmpty()) {
parser.printUsage();
parser.printHelp();
}
validateArchetypes(ns.getList("file"));
}
示例14: handleArguments
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
public Option handleArguments(String argv[]) {
ArgumentParser parser = ArgumentParsers.newArgumentParser("Detector")
.defaultHelp(true)
.description("Convert Zawgyi <-> Unicode encodings.");
parser.addArgument("-t", "--to")
.choices("zawgyi", "unicode")
.required(true)
.help("Specify hash function to use");
parser.addArgument("files").nargs("*")
.help("Files to convert");
try {
parser.parseArgs(argv, this);
} catch (ArgumentParserException e) {
parser.handleError(e);
System.exit(1);
}
return this;
}
示例15: parseArgs
import net.sourceforge.argparse4j.inf.ArgumentParser; //导入依赖的package包/类
private static Namespace parseArgs(String[] args) {
ArgumentParser parser =
ArgumentParsers.newArgumentParser("moco", false).description("The Monty compiler.").epilog(
"Without -S or -c the program is compiled and directly executed.");
parser.addArgument("--help").action(Arguments.help()).help("Print this help and exit.");
parser.addArgument("-S", "--emit-assembly").action(Arguments.storeTrue()).dest("emitAssembly").help(
"Emit the LLVM assembly and stop.");
parser.addArgument("-c", "--compile-only").action(Arguments.storeTrue()).dest("compileOnly").help(
"Only compile the executable without running it.");
parser.addArgument("-e", "--stop-on-first-error").action(Arguments.storeTrue()).dest("stopOnFirstError").help(
"Stop the compilation on the first encountered error.");
parser.addArgument("-p", "--print-ast").action(Arguments.storeTrue()).dest("printAST").help("Print the AST.");
parser.addArgument("-d", "--debug-parsetree").action(Arguments.storeTrue()).dest("debugParseTree").help(
"Debug the parsetree without running anything.");
parser.addArgument("-o").metavar("<file>").dest("outputFile").help("Write output to <file>.");
parser.addArgument("file").nargs("?").help("Monty file to run.");
Namespace ns = null;
try {
ns = parser.parseArgs(args);
} catch (ArgumentParserException ape) {
parser.handleError(ape);
}
return ns;
}