我正在编写一个 JAVA 控制台应用程序。用户启动 jar 后,他应该得到一个类似于 Linux shell 的命令行,他可以在其中输入命令。这是我以前从未编程过的东西。通常我是在写 GUI 或者我用一些被解析的参数来启动 jar。
基本上我有2个问题:
是否有提供“无限输入”的最佳实践?我找到的所有解决方案都有 10 年或更长时间,例如:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.print("Enter String");
String s = br.readLine();
}
Run Code Online (Sandbox Code Playgroud)
这仍然是最佳实践还是今天有更好的方法?
我正在尝试构建一个复杂的参数列表,而不使用commons-cli项目链接多个解析器...
基本上我试图理解参数和可选参数是如何协同工作的......
示例命令帮助
$ admin <endpoint> <update> <name> [<type>] [<endpoint>] [<descriptions>]
//sample option creation for a
options.addOption(OptionBuilder.hasArgs(3).hasOptionalArgs(2)
.withArgName("name> <type> <uri> [<description>] [<endpoint>]")
.withValueSeparator(' ')
.create("add"));
CommandLine line = parser.parse(options, args, true);
Run Code Online (Sandbox Code Playgroud)
CommandLine不区分必需参数和可选参数...如何在不必为可选选项链接第二个解析器的情况下检索它们?
我正在尝试编写一个程序,执行时java -jar -cf file.txt将检索cf参数的值.我到目前为止的代码是:
Options options = new Options();
final Option configFileOption = Option.builder("cf")
.longOpt("configfile")
.desc("Config file for Genome Store").argName("cf")
.build();
options.addOption(configFileOption);
CommandLineParser cmdLineParser = new DefaultParser();
CommandLine commandLineGlobal= cmdLineParser.parse(options, commandLineArguments);
if(commandLineGlobal.hasOption("cf")) {
System.out.println(commandLineGlobal.getOptionValue("cf"));
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是正在打印的值为null.谁能告诉我我错过了什么?
Apache common-cli在其网站上有一个ls命令示例:
options.addOption( "a", "all", false, "do not hide entries starting with ." );
options.addOption( "A", "almost-all", false, "do not list implied . and .." );
options.addOption( "b", "escape", false, "print octal escapes for nongraphic " + "characters" );
options.addOption( OptionBuilder.withLongOpt( "block-size" )
.withDescription( "use SIZE-byte blocks" )
.hasArg()
.withArgName("SIZE")
.create() );
Run Code Online (Sandbox Code Playgroud)
这显示了这样的帮助:
-a, --all do not hide entries starting with .
-A, --almost-all do not list implied . and ..
-b, --escape print octal escapes for nongraphic …Run Code Online (Sandbox Code Playgroud) java command-line-interface apache-commons apache-commons-cli