在 Java 中从命令行传递多个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14795147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:25:58  来源:igfitidea点击:

Passing multiple parameters from the command line in Java

javaif-statementparameters

提问by

I am working on a project in which I need to pass multiple arguments from the command line-

我正在开发一个项目,在该项目中我需要从命令行传递多个参数 -

Below is the use case I have-

以下是我的用例-

  1. From the command line, I will be passing atleast four paramaters- noOfThreads, noOfTasks, startRangeand tableName1, so if I am passing these four thing, then I need to store them in a variable and for any table names- I need to add it into the string list so that I can use them in my other code.

  2. Secondly, I can pass five parameters instead of four just like above- So five parameters can be- noOfThreads, noOfTasks, startRange, tableName1and tableName2. So here tableName2is extra. so if I am passing these five thing, then I need to store them in a variable and here I am passing tableName1and tableName2as two tables so I will be storing these two tables in a string list.

  3. Thirdly, I can pass six parameters instead of five just like above- So sixparameters can be- noOfThreads, noOfTasks, startRange, tableName1, tableName2and tableName3. So here tableName3is extra. so if I am passing these six thing, then I need to store them in a variable and here I am passing tableName1, tableName2and tableName3as three tables so I will be storing these three tables in a string list again.

  1. 在命令行中,我将传递至少四个参数 - noOfThreads, noOfTasks, startRangeand tableName1,所以如果我传递这四个参数,那么我需要将它们存储在一个变量中,并且对于任何表名 - 我需要将它添加到字符串列表中这样我就可以在我的其他代码中使用它们。

  2. 其次,我可以通过五个参数,而不是四个,就像上文所以五个参数可以为─ noOfThreadsnoOfTasksstartRangetableName1tableName2。所以这里tableName2是额外的。所以如果我传递这五个东西,那么我需要将它们存储在一个变量中,在这里我传递tableName1tableName2作为两个表,所以我将这两个表存储在一个字符串列表中。

  3. 第三,我可以通过六个参数而不是5就像上文所以sixparameters可以为─ noOfThreadsnoOfTasksstartRangetableName1tableName2tableName3。所以这里tableName3是额外的。所以如果我传递了这六个东西,那么我需要将它们存储在一个变量中,在这里我传递tableName1tableName2并且tableName3作为三个表,所以我将再次将这三个表存储在一个字符串列表中。

So for above scenario, I have the below code with me. It is looking very ugly currently as I have lot of repetition in my code as mentioned below. Is there any way I can make it more cleaner?

所以对于上面的场景,我有下面的代码。目前看起来非常难看,因为我的代码中有很多重复,如下所述。有什么办法可以让它更干净吗?

Below is my code-

下面是我的代码-

private static List<String> databaseNames = new ArrayList<String>();
private static int noOfThreads;
private static int noOfTasks;
private static int startRange;
private static String tableName1;
private static String tableName2;
private static String tableName3;

public static void main(String[] args) {

if (args.length > 0 && args.length < 5) {

    noOfThreads = Integer.parseInt(args[0]);
    noOfTasks = Integer.parseInt(args[1]);
    startRange = Integer.parseInt(args[2]);
    tableName1 = args[3];
    databaseNames.add(tableName1);
} else if (args.length > 0 && args.length < 6) {
    noOfThreads = Integer.parseInt(args[0]);
    noOfTasks = Integer.parseInt(args[1]);
    startRange = Integer.parseInt(args[2]);
    tableName1 = args[3];
    tableName2 = args[4];
    databaseNames.add(tableName1);
    databaseNames.add(tableName2);
} else {
    noOfThreads = Integer.parseInt(args[0]);
    noOfTasks = Integer.parseInt(args[1]);
    startRange = Integer.parseInt(args[2]);
    tableName1 = args[3];
    tableName2 = args[4];
    tableName3 = args[5];
    databaseNames.add(tableName1);
    databaseNames.add(tableName2);
    databaseNames.add(tableName3);
}
}

采纳答案by Hovercraft Full Of Eels

You have a lotof redundancy, and when you see that think refactoring using methods to simplify. That's is in effect just what I'd do. Also, consider having String[] field for your tableNames, String[] tableNameswhich will allow you to declare it as an array of whatever size is needed once you know your parameter count. For example:

你有很多冗余,当你看到重构使用方法来简化。这实际上正是我要做的。另外,考虑为您的 tableNames 设置 String[] 字段,String[] tableNames一旦您知道参数计数,这将允许您将其声明为所需大小的数组。例如:

public void fillParams(String[] args) {
    if (args.length < someMinimum) {
       // throw some exception
    }
    noOfThreads = Integer.parseInt(args[0]);
    noOfTasks = Integer.parseInt(args[1]);
    startRange = Integer.parseInt(args[2]);

    tableNames = new String[args.length - 3];
    for (int i = 0; i < tableNames.length; i++) {
       tableNames[i] = args[i + 3];
       databaseNames.add(tableNames[i]);
    }
}

回答by Evgeniy Dorofeev

I suggest to remove tableName1 - tableName3, they are redundant, you can always get a tablename as

我建议删除 tableName1 - tableName3,它们是多余的,你总是可以得到一个表名

tablename1 =  databaseNames.get(0);

this is my version:

这是我的版本:

public static void main(String[] args) {
    if (args.length < 4) {
        System.err.println("Usage: ....");
        System.exit(1);
    }
    noOfThreads = Integer.parseInt(args[0]);
    noOfTasks = Integer.parseInt(args[1]);
    startRange = Integer.parseInt(args[2]);
    for(int i = 3; i < args.length; i++) {
        databaseNames.add(args[i]);
    }
}