本文整理了Java中org.apache.commons.cli.Parser
类的一些代码示例,展示了Parser
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parser
类的具体详情如下:
包路径:org.apache.commons.cli.Parser
类名称:Parser
[英]Parser
creates CommandLines.
[中]Parser
创建命令行。
代码示例来源:origin: commons-cli/commons-cli
@Test
public void test31148() throws ParseException
{
Option multiArgOption = new Option("o","option with multiple args");
multiArgOption.setArgs(1);
Options options = new Options();
options.addOption(multiArgOption);
Parser parser = new PosixParser();
String[] args = new String[]{};
Properties props = new Properties();
props.setProperty("o","ovalue");
CommandLine cl = parser.parse(options,args,props);
assertTrue(cl.hasOption('o'));
assertEquals("ovalue",cl.getOptionValue('o'));
}
代码示例来源:origin: commons-cli/commons-cli
for (Option opt : options.helpOptions())
for (OptionGroup group : options.getOptionGroups())
setOptions(options);
cmd = new CommandLine();
List<String> tokenList = Arrays.asList(flatten(getOptions(), arguments, stopAtNonOption));
cmd.addArg(t);
if (stopAtNonOption && !getOptions().hasOption(t))
cmd.addArg(t);
processOption(t, iterator);
processProperties(properties);
checkRequiredOptions();
代码示例来源:origin: commons-cli/commons-cli
boolean hasOption = getOptions().hasOption(arg);
Option opt = (Option) getOptions().getOption(arg).clone();
updateRequiredOptions(opt);
processArgs(opt, iter);
cmd.addOption(opt);
代码示例来源:origin: commons-cli/commons-cli
@Test
public void testShortOptionalArgValues() throws Exception
{
String[] args = new String[] { "-j", "ink", "idea" };
Parser parser = new PosixParser();
CommandLine cmd = parser.parse(opts,args);
assertTrue( cmd.hasOption("j") );
assertEquals( "ink", cmd.getOptionValue("j") );
assertEquals( "ink", cmd.getOptionValues("j")[0] );
assertEquals( "idea", cmd.getOptionValues("j")[1] );
assertEquals( cmd.getArgs().length, 0 );
}
代码示例来源:origin: commons-cli/commons-cli
@Test
public void test13425() throws Exception
{
Options options = new Options();
Option oldpass = OptionBuilder.withLongOpt( "old-password" )
.withDescription( "Use this option to specify the old password" )
.hasArg()
.create( 'o' );
Option newpass = OptionBuilder.withLongOpt( "new-password" )
.withDescription( "Use this option to specify the new password" )
.hasArg()
.create( 'n' );
String[] args = {
"-o",
"-n",
"newpassword"
};
options.addOption( oldpass );
options.addOption( newpass );
Parser parser = new PosixParser();
try {
parser.parse( options, args );
fail( "MissingArgumentException not caught." );
} catch( MissingArgumentException expected ) {
}
}
代码示例来源:origin: apache/zookeeper
Parser parser = new PosixParser();
try {
cl = parser.parse(options, cmdArgs);
} catch (ParseException ex) {
throw new CliParseException(ex);
if (!(cl.hasOption("file") || cl.hasOption("members")) && !cl.hasOption("add") && !cl.hasOption("remove")) {
throw new CliParseException(getUsageStr());
代码示例来源:origin: com.conversantmedia/mara-core
private String[] handleHelp(String[] args, Options fullOptions) {
Options options = new Options();
options.addOption(fullOptions.getOption("help"));
Parser p = new PosixParser();
try {
CommandLine line = p.parse(options, args, true);
if (line.hasOption('?')) {
showHelpAndExit(fullOptions, 0);
new HelpFormatter().printHelp(getCommandLineSyntax(), fullOptions);
System.exit(0);
}
return line.getArgs();
} catch (ParseException e) {
// ignore
}
return args;
}
代码示例来源:origin: apache/incubator-sentry
Options simpleShellOptions = new Options();
simpleShellOptGroup.addOption(lpOpt);
simpleShellOptGroup.setRequired(true);
simpleShellOptions.addOptionGroup(simpleShellOptGroup);
simpleShellOptions.addOption(pOpt);
CommandLine cmd = parser.parse(helpOptions, args, true);
for (Option opt : cmd.getOptions()) {
if (opt.getOpt().equals("h")) {
cmd = parser.parse(simpleShellOptions, args);
for (Option opt : cmd.getOptions()) {
if (opt.getOpt().equals("p")) {
privilegeStr = opt.getValue();
代码示例来源:origin: commons-cli/commons-cli
@Test
public void testSingleOption() throws Exception
{
String[] args = new String[] { "-r" };
CommandLine cl = parser.parse( _options, args);
assertTrue( "Confirm -r is set", cl.hasOption("r") );
assertTrue( "Confirm -f is NOT set", !cl.hasOption("f") );
assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
}
代码示例来源:origin: commons-cli/commons-cli
@Test
public void testGetOptionProperties() throws Exception
{
String[] args = new String[] { "-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar" };
Options options = new Options();
options.addOption(OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D'));
options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withLongOpt("property").create());
Parser parser = new GnuParser();
CommandLine cl = parser.parse(options, args);
Properties props = cl.getOptionProperties("D");
assertNotNull("null properties", props);
assertEquals("number of properties in " + props, 4, props.size());
assertEquals("property 1", "value1", props.getProperty("param1"));
assertEquals("property 2", "value2", props.getProperty("param2"));
assertEquals("property 3", "true", props.getProperty("param3"));
assertEquals("property 4", "value4", props.getProperty("param4"));
assertEquals("property with long format", "bar", cl.getOptionProperties("property").getProperty("foo"));
}
代码示例来源:origin: org.apache.reef/tang
o.addOption(new Option("?", "help"));
final Parser g = new GnuParser();
cl = g.parse(o, args);
} catch (final ParseException e) {
throw new IOException("Could not parse config file", e);
if (cl.hasOption("?")) {
new HelpFormatter().printHelp("reef", o);
return null;
for (final Option option : cl.getOptions()) {
代码示例来源:origin: OneBusAway/onebusaway-application-modules
Parser parser = new GnuParser();
Options options = new Options();
buildOptions(options);
CommandLine commandLine = parser.parse(options, args);
String[] remainingArgs = commandLine.getArgs();
代码示例来源:origin: LiveRamp/hank
public static CommandLine parseAndHelp(String appName, Options options, String[] args) {
options.addOption(HELP_OPTION);
Parser parser = new GnuParser();
CommandLine line = null;
try {
line = parser.parse(options, args);
} catch (MissingOptionException e) {
new HelpFormatter().printHelp(appName, options, true);
throw new IllegalArgumentException();
} catch (MissingArgumentException e) {
new HelpFormatter().printHelp(appName, options, true);
throw new IllegalArgumentException();
} catch (ParseException e) {
System.err.println("Unexpected Exception: " + e);
throw new IllegalArgumentException();
}
if (line.hasOption("help")) {
new HelpFormatter().printHelp(appName, options, true);
throw new IllegalArgumentException();
}
return line;
}
代码示例来源:origin: OneBusAway/onebusaway-gtfs-realtime-visualizer
private void run(String[] args) throws Exception {
if (args.length == 0 || CommandLineInterfaceLibrary.wantsHelp(args)) {
printUsage();
System.exit(-1);
}
Options options = new Options();
buildOptions(options);
Parser parser = new GnuParser();
CommandLine cli = parser.parse(options, args);
Set<Module> modules = new HashSet<Module>();
VisualizerModule.addModuleAndDependencies(modules);
Injector injector = Guice.createInjector(modules);
injector.injectMembers(this);
VisualizerService service = injector.getInstance(VisualizerService.class);
service.setVehiclePositionsUri(new URI(
cli.getOptionValue(ARG_VEHICLE_POSITIONS_URL)));
injector.getInstance(VisualizerServer.class);
LifecycleService lifecycleService = injector.getInstance(LifecycleService.class);
lifecycleService.start();
}
代码示例来源:origin: apache/zookeeper
@Override
public CliCommand parse(String[] cmdArgs) throws CliParseException {
Parser parser = new PosixParser();
try {
cl = parser.parse(options, cmdArgs);
} catch (ParseException ex) {
throw new CliParseException(ex);
}
args = cl.getArgs();
if (cl.hasOption("?")) {
printHelp();
}
retainCompatibility(cmdArgs);
return this;
}
代码示例来源:origin: commons-cli/commons-cli
@Test
public void testValidLongOnlyOptions() throws Exception
{
CommandLine cl1 = parser.parse(_options, new String[]{"--export"});
assertTrue("Confirm --export is set", cl1.hasOption("export"));
CommandLine cl2 = parser.parse(_options, new String[]{"--import"});
assertTrue("Confirm --import is set", cl2.hasOption("import"));
}
代码示例来源:origin: apache/zookeeper
private void retainCompatibility(String[] cmdArgs) throws CliParseException {
if (args.length > 2) {
err.println("'delete path [version]' has been deprecated. "
+ "Please use 'delete [-v version] path' instead.");
Parser parser = new PosixParser();
try {
cl = parser.parse(options, cmdArgs);
} catch (ParseException ex) {
throw new CliParseException(ex);
}
args = cl.getArgs();
}
}
代码示例来源:origin: org.apache.accumulo/examples-simple
public int run(String[] unprocessed_args) throws Exception {
Parser p = new BasicParser();
CommandLine cl = p.parse(opts, unprocessed_args);
String[] args = cl.getArgs();
String username = cl.getOptionValue(usernameOpt.getOpt(), "root");
String password = cl.getOptionValue(passwordOpt.getOpt(), "secret");
代码示例来源:origin: commons-cli/commons-cli
@Before
public void setUp() throws Exception
{
opts.addOption("a", false, "toggle -a");
opts.addOption("b", true, "set -b");
opts.addOption("c", "c", false, "toggle -c");
opts.addOption("d", "d", true, "set -d");
opts.addOption(OptionBuilder.hasOptionalArg().create('e'));
opts.addOption(OptionBuilder.hasOptionalArg().withLongOpt("fish").create());
opts.addOption(OptionBuilder.hasOptionalArgs().withLongOpt("gravy").create());
opts.addOption(OptionBuilder.hasOptionalArgs(2).withLongOpt("hide").create());
opts.addOption(OptionBuilder.hasOptionalArgs(2).create('i'));
opts.addOption(OptionBuilder.hasOptionalArgs().create('j'));
String[] args = new String[] { "-a",
"-b", "foo",
"--c",
"--d", "bar"
};
Parser parser = new PosixParser();
_cl = parser.parse(opts,args);
}
代码示例来源:origin: org.onebusaway/onebusaway-phone
public static void main(String[] args) throws IOException, ParseException {
Parser parser = new GnuParser();
Options options = buildOptions();
CommandLine cli = parser.parse(options, args);
String host = cli.getOptionValue(ARG_HOSTNAME, "localhost");
int port = Integer.parseInt(cli.getOptionValue(ARG_PORT, "8001"));
String callerId = cli.getOptionValue(ARG_CALLER_ID, "2000");
AgiClientScriptImpl script = new AgiClientScriptImpl();
setupGui(script);
DefaultAgiClient client = new DefaultAgiClient(host, port, script);
client.setCallerId(callerId);
client.setNetworkScript("index.agi");
client.run();
}
内容来源于网络,如有侵权,请联系作者删除!