英文:
How to pass empty args to Netbeans
问题
我尝试在Netbeans Java项目中使用Properties-->Run-->Arguments传递空白命令行参数,使用""或''但没有效果。我有一些参数有时必须为空或null。
英文:
I tried to pass blank command line arguments in a Netbeans Java project using Properties-->Run-->Arguments with "" or '' but nothing.
I have some arguments that some times have to be empty or null.
答案1
得分: 2
处理这种情况的常见方式是在参数为空或空白时不指定参数。在CLI/脚本世界中,这是相当常见的做法。未指定的参数意味着要么使用默认值,要么没有值。
此外,还有一些Java的良好库可帮助您解析程序的options
或参数,例如:
- Args4j http://args4j.kohsuke.org/
- Apache CLI http://commons.apache.org/proper/commons-cli/
编辑
附加说明:
- 如果没有更智能的选项/参数解析器,就无法正确处理空参数(即仅通过设置空白、空格或带引号的空字符串来处理)。要实现这一点的唯一方法是定义一个关键字或特殊字符,以便在处理
args[]
时识别空/未指定的参数。 - 这种行为与Netbeans无关。它与Java如何解析命令行的
main()
方法的varargs
相关。
有趣的链接:
- https://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java
- https://stackoverflow.com/questions/30761043/how-to-handle-empty-parameters-in-a-main-method-java-call
英文:
The common way to handle that is to not specify arguments when they are blank or empty. It is quite the norm in the CLI/scripting world to work like this. Unspecified arguments mean either to use the defaults or no value.
Aside there are Java nice libraries to help you parse your program options
or arguments, such as:
- Args4j http://args4j.kohsuke.org/
- Apache CLI http://commons.apache.org/proper/commons-cli/
EDIT
Additonal remarks:
- Without a smarter options/arguments parser, it is not possible to handle empty arguments properly (i.e. just by setting a blank, whitespace or quoted empty string). The only way to achieve that would be to define a keyword or special character in order to identify empty/unspecified arguments when processing
args[]
. - This behavior is not bound to Netbeans. Rather it relates to how Java parses the
varargs
of themain()
method from the command-line.
Interesting links:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论