在 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
Passing multiple parameters from the command line in Java
提问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-
以下是我的用例-
From the command line, I will be passing atleast four paramaters-
noOfThreads
,noOfTasks
,startRange
andtableName1
, 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.Secondly, I can pass five parameters instead of four just like above- So five parameters can be-
noOfThreads
,noOfTasks
,startRange
,tableName1
andtableName2
. So heretableName2
is extra. so if I am passing these five thing, then I need to store them in a variable and here I am passingtableName1
andtableName2
as two tables so I will be storing these two tables in a string list.Thirdly, I can pass six parameters instead of five just like above- So sixparameters can be-
noOfThreads
,noOfTasks
,startRange
,tableName1
,tableName2
andtableName3
. So heretableName3
is extra. so if I am passing these six thing, then I need to store them in a variable and here I am passingtableName1
,tableName2
andtableName3
as three tables so I will be storing these three tables in a string list again.
在命令行中,我将传递至少四个参数 -
noOfThreads
,noOfTasks
,startRange
andtableName1
,所以如果我传递这四个参数,那么我需要将它们存储在一个变量中,并且对于任何表名 - 我需要将它添加到字符串列表中这样我就可以在我的其他代码中使用它们。其次,我可以通过五个参数,而不是四个,就像上文所以五个参数可以为─
noOfThreads
,noOfTasks
,startRange
,tableName1
和tableName2
。所以这里tableName2
是额外的。所以如果我传递这五个东西,那么我需要将它们存储在一个变量中,在这里我传递tableName1
和tableName2
作为两个表,所以我将这两个表存储在一个字符串列表中。第三,我可以通过六个参数而不是5就像上文所以sixparameters可以为─
noOfThreads
,noOfTasks
,startRange
,tableName1
,tableName2
和tableName3
。所以这里tableName3
是额外的。所以如果我传递了这六个东西,那么我需要将它们存储在一个变量中,在这里我传递tableName1
,tableName2
并且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[] tableNames
which 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]);
}
}