问题描述
我有一个像这样的属性文件-
I have a property file which is like this -
hostName=machineA.domain.host.com
[email protected]
[email protected]
[email protected]
现在,我正在从Java程序中读取上述属性文件,如下所示。到目前为止,我正在手动解析上述属性文件-
And now I am reading the above property file from my Java program as shown below. I am parsing the above property file manual way as of now -
public class FileReaderTask {
private static String hostName;
private static String emailFrom;
private static String emailTo;
private static String emailCc;
private static final String configFileName = "config.properties";
private static final Properties prop = new Properties();
public static void main(String[] args) {
readConfig(arguments);
// use the above variables here
System.out.println(hostName);
System.out.println(emailFrom);
System.out.println(emailTo);
System.out.println(emailCc);
}
private static void readConfig(String[] args) throws FileNotFoundException, IOException {
if (!TestUtils.isEmpty(args) && args.length != 0) {
prop.load(new FileInputStream(args[0]));
} else {
prop.load(FileReaderTask.class.getClassLoader().getResourceAsStream(configFileName));
}
StringBuilder sb = new StringBuilder();
for (String arg : args) {
sb.append(arg).append("\n");
}
String commandlineProperties = sb.toString();
if (!commandlineProperties.isEmpty()) {
// read, and overwrite, properties from the commandline...
prop.load(new StringReader(commandlineProperties));
}
hostName = prop.getProperty("hostName").trim();
emailFrom = prop.getProperty("emailFrom").trim();
emailTo = prop.getProperty("emailTo").trim();
emailCc = prop.getProperty("emailCc").trim();
}
}
大多数时候,我会在上面运行通过命令行作为可运行的jar程序进行编程-
Most of the time, I will be running my above program through command line as a runnable jar like this -
java -jar abc.jar config.properties
java -jar abc.jar config.properties hostName=machineB.domain.host.com
我的问题是-
- 是否可以添加
-帮助
运行abc.jar
时的选项,它可以告诉我们更多有关如何运行jar文件以及每个属性意味着什么以及如何使用它们的信息。我在运行大多数C ++可执行文件或Unix之类的工具时看到过-help
,所以不确定我们如何在Java中做同样的事情?
- Is there any way to add
--help
option while running theabc.jar
that can tell us more about how to run the jar file and what does each property means and how to use them? I have seen--help
while running most of the C++ executable or Unix stuff so not sure how we can do the same thing in Java?
我是否需要在Java中使用像 Common CLI
这样的命令行解析器?为此,我应该使用 Common CLI
来解析文件,而不是手动解析?如果是,那么谁能提供一个示例,说明在我的场景中该如何做?
Do I need to use CommandLine parser like Commons CLI
for this in Java to achieve this and instead of doing manual parsing, I should use Commons CLI
to parse the file as well? If yes, then can anyone provide an example how would I do that in my scenario?
推荐答案
从长远来看,如果您计划要在将来添加其他选项,那么commons-cli当然很合适,因为它使添加新选项变得容易,并且手动解析很快变得很复杂。
In the long run if you plan to add other options in the future then commons-cli is surely a fairly good fit as it makes it easy to add new options and manual parsing quickly becomes complicated.
看看,它们很好地概述了图书馆的内容
Take a look at the official examples, they provide a good overview of what the library can do.
您的特定情况可能会导致以下情况:
Your specific case would probably lead to something like the following:
// create Options object
Options options = new Options();
Option help = new Option( "h", "help", false, "print this message" );
options.addOption(help);
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse( options, args);
if(cmd.hasOption("help") || cmd.getArgList().isEmpty()) {
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "cli-test [options] <property-file>", options );
return;
}
// do your thing...
System.out.println("Had properties " + cmd.getArgList());
这篇关于如何使用Apache Commons CLI解析属性文件和--help选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!