Java:进行可选的命令行参数 [英] Java: Making an Optional Command Line Argument

查看:79
本文介绍了Java:进行可选的命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,该程序应该使用一个必需的命令行参数和一个可选的命令行参数.第一个参数是将要写入数据的输出文件的名称,第二个参数是将用于计算要写入输出文件的数据的数字.如果用户未输入数字,则应仅使用默认值来计算数据.例如,如果用户输入命令行参数"Foo.csv 1024",则程序将使用1024计算数据并将其写入Foo.csv,但是如果用户仅使用命令行参数"Foo.csv",则程序将使用默认值2048计算数据并将其写入Foo.csv.我正在使用Intellij IDE创建/运行该程序.我该怎么做?任何建议/建议将不胜感激.

I am working on a program that is supposed to take one required command line argument and one optional command line argument. The first argument is the name of an output file where data will be written to, and the second is a number that will be used to calculate the data to be written to the output file. If the user does not enter a number, then it should just use a default value to calculate the data. For example, if the user entered command line arguments "Foo.csv 1024" the program would use 1024 to calculate the data and write it to Foo.csv, but if the user only used the command line argument "Foo.csv" then the program would use a default value of 2048 to calculate the data and write it to Foo.csv. I am creating/running this program using the Intellij IDE. How would I do this? Any advice/suggestions would be much appreciated.

推荐答案

您的程序似乎很简单,因此对于这种特殊情况,解决方案也很简单.您可以检查主要功能的参数 args ,以测试将多少参数传递给程序:

Your program seems to be simple, so the solution is also simple for this particular case. You can test how many arguments were passed to the program checking the argument args of your main function:

public static void main(String[] args){...}

args 是一个数组,其中包含传递给程序的参数.因此,如果您的程序名为 prog ,并且使用 prog Foo.csv 1024 运行,则args将具有:

args is an array that contains the arguments passed to your program. So if your program is called prog and you run it with prog Foo.csv 1024, then args will have:

args[0] = "Foo.csv";
args[1] = "1024";

通过此操作,您可以知道将哪些参数传递给程序,并通过执行args.length可以知道它们有多少个.对于上面的示例, args.length = 2 如果用户未指定最后一个参数("1024"),则 args中将具有args.length = 1并带有以下内容:

With this, you know which arguments were passed to your program and by doing args.length, you can know how many they were. For the example above, args.length=2 If the user didn't indicate the last argument ("1024"), then you would have args.length=1 with the following in args:

args[0] = "Foo.csv";

所以您的程序将类似于:

So your program would be something like:

public static void main(String[] args){
//The default value you want
int number = 2048
if(args.length==2){
    number = Integer.parseInt(args[1]);
}
//'number' will have the number the user specified or the default value (2048) if the user didn't specify a number
}

要为程序提供参数,必须在控制台或某种终端上运行它.使用IntelliJ(或任何其他IDE),也可以使用带参数的程序运行该程序,但是您可以在运行设置中指定这些参数.

To supply arguments to your program you must run it on a console or some kind of terminal. Using IntelliJ (or any other IDE) it's also possible to run the program with arguments, but you specify those in the Run Settings.

如果您想对参数进行更复杂的处理,通常所需的操作是由参数解析器完成的.这些通常是带您argv并帮助您读取程序参数的库.除其他外,这些库通常支持可选参数,通过标志提供的参数,参数的类型检查,为命令创建自动帮助页面等.如果您的参数要求更加复杂,或者您只是想给参数一个,请考虑使用参数解析器.专业接触您的程序:)对于Java,我发现了这个特定的库: http://www.martiansoftware.com/jsap/doc/

If you want a more complex treatment of arguments, usually what you want is done by argument parsers. These are usually libraries that take you argv and help you reading arguments to your program. Among other things, these libraries usually support optional arguments, arguments supplied via flags, type checking of arguments, creating automatic help pages for your commands etc. Consider using an argument parser if your argument requirements are more complex or if you just want to give a professional touch to your program :) For java i found this particular library: http://www.martiansoftware.com/jsap/doc/

这篇关于Java:进行可选的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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