如何使命令行参数既不运行,又不运行,又不运行? -Java [英] How do I make command line arguments run with either, neither, or both arguments? - Java

查看:132
本文介绍了如何使命令行参数既不运行,又不运行,又不运行? -Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个硒Web驱动程序,我有25个应用程序和4个环境。我将它们设置为枚举,以便提供特定的命令行参数。现在,我可以正常运行它,以便该应用程序需要一个应用程序,然后需要一个环境。我需要能够做

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);
}


推荐答案

考虑使用库进行评估您的命令行参数,例如 JOptSimple 。他们在他们的网站上提供了一组不错的示例,而您不必重新发明轮子。按照您的示例,根据文档(尚未尝试过),以下应该可以工作:

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)然后应返回 app1, app2和 app3的列表。

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

这篇关于如何使命令行参数既不运行,又不运行,又不运行? -Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆