Joh*_*ood 39
如果您计划通过单个arg扩展,请使用apache commons cli.
"Apache Commons CLI库提供了一个API,用于解析传递给程序的命令行选项.它还能够打印详细说明命令行工具可用选项的帮助消息."
Commons CLI支持不同类型的选项:
Bjö*_*örn 36
public class YourClass {
public static void main(String[] args) {
if (args.length > 0 && args[0].equals("a")){
//...
}
}
}
Run Code Online (Sandbox Code Playgroud)
Bil*_*ard 17
每个Java程序都以
public static void main(String[] args) {
Run Code Online (Sandbox Code Playgroud)
类型的数组String
的是main()
作为一个参数保持命令行参数给程序.如果用户运行您的程序
$ java myProgram a
Run Code Online (Sandbox Code Playgroud)
然后args[0]
将保持字符串"a".
命令行参数在第一个String[]
参数中传递给main()
,例如
public static void main( String[] args ) {
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,args
包含所有命令行参数.
对提出的问题的简短而甜蜜的回答是:
public static void main( String[] args ) {
if( args.length > 0 && args[0].equals( "a" ) ) {
// first argument is "a"
} else {
// oh noes!?
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
128885 次 |
最近记录: |