您如何解析Java程序的参数? [英] How do you parse arguments for a java program?

查看:174
本文介绍了您如何解析Java程序的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Selenium WebDriver Java程序。我有25个应用程序和4个环境。我需要能够通过-app app1 app2 app3 ... appn -env env1 env2 envn

I'm making a Selenium WebDriver java program. I have 25 application and 4 environments. I need to be able to pass something like -app app1 app2 app3 ... appn -env env1 env2 envn

论点。现在,我可以按此顺序传递一个应用程序和一个环境,但是我需要能够按任一顺序进行传递,并且不能同时执行两种选择。这是我到目前为止的内容。有了这个,我要么不传递任何参数,就为每个环境运行每个应用程序(这就是我想要的),要么我可以按顺序为特定的测试选择app1 env1。

I need to be able to pass either, neither or both arguments. Right now I have it being able to pass one app and one env in that order but I need to be able to do it in either order and with the either neither or both possibility. Here's what I have so far. With this I can either pass no arguments and runs every app for every environment (which is what I want) or I can pick app1 env1 in that order for that specific test.

 public static Application chooseAppTest(String[] args) 
    {
        Application application = null;

        switch (Application.valueOf(args[0]))
        {
        case ACCOUNTINVENTORY:
            new AccountInventory(Environment.valueOf(args[1]));
            AccountInventory.accountInventoryDatabaseTests(testResults);
            break;

if (args.length == 0)
    {
       LogIn.loginTest(testResults);
       DatabaseTest.testResults(testResults);
       LinkTest.linkTests(testResults);
    }
    else 
    {
            // First choose application, then choose environment
        Application.chooseAppTest(args);
    }


推荐答案

我不认为递归是必需的。您可以执行以下操作:

I don't think recursion is needed. You can do something like this:

public static void main (String[] args)
{
    List<String> apps = new LinkedList<>();
    List<String> envs = new LinkedList<>();
    List<String> current = null;
    // parse arguments
    for (String arg : args)
    {
        if (arg.equals("-app")) current = apps;
        else if (arg.equals("-env")) current = envs;
        else if (current != null) // add argument
            current.add(arg);
    }
    // parsing finished
    Application.doSomethingWith(apps, envs);
}

这篇关于您如何解析Java程序的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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