Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

How do I make command line arguments run with either, neither, or both arguments? - Java

I am making a selenium webdriver and I have 25 apps and 4 environments. I have them set up as enums so I can give specific command line arguments. Right now I have it working so that it takes one app and then one environment in that order. I need to be able to do something like

-app app1 app2 app3 -env env1 env2

I need it to either take no arguments, either arguments, or both arguments. This is what I have so far.

if (args.length == 0)
{
    LogIn.loginTest(testResults);
    DatabaseTest.testResults(testResults);
    LinkTest.linkTests(testResults);
}
else 
{
    // First choose application, then choose environment
    Application.chooseAppTest(args);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Consider using a library for evaluating your command line arguments, like JOptSimple. They provide a nice set of examples on their website and you wouldn't have to re-invent the wheel. Adopting to your example, the following should work, according to the documentation (haven't tried it out):

OptionParser parser = new OptionParser("app:env:");
OptionSet options = parser.parse("-app", "app1", "app2", "app3", "-env", "env1", "env2");

options.valuesOf("app") should then return a list of "app1", "app2", and "app3".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...