关注和分享VPS主机优惠活动
www.vpsmr.com
腾讯云双11优惠活动

使用 Apache commons-cli 包进行命令行参数解析的示例代码

Apache的commons-cli包是专门用于解析命令行参数格式的包。

依赖关系:

?

[h ]
1

2

3

4

5

commons-cli

commons-cli

1.3.1 [ h ]

要使用此软件包,您需要:

1、首先定义需要解析的参数,对应选项、带有附加选项的参数以及每个参数的说明。 class [ h] 例如,如果命令行参数是-hfbv,那么您定义的选项的目的是指示实际应该解析哪些参数。 例如,假设您按如下方式定义 Option:对于:h、f、b,在解析过程中,解析器可以将命令行与定义的选项进行匹配,以了解如何检索每个参数。 您还可以定义需要 tar -f 等选项的参数。 f 参数需要文件名选项。 只有定义解析器才能将f后面的内容解析成f指定的文件名。

2、根据定义的需要解析的参数来解析命令行参数,对应CommandLineParser类
,根据传入的String[]实参参数进行解析以下:我会的。 定义的选项对象。 ,通过匹配每个参数,您可以单独检索每个参数。

3. 解析后,将返回一个 CommandLine 对象,您可以从中检索有关命令行参数的信息。
从该对象中,您可以找出哪些参数已输入,哪些参数尚未输入,哪些参数有哪些附加选项等。 然后您可以编写自己的代码来根据不同的参数执行不同的逻辑。

代码示例:

?

[h ]
1

2

3

4

5

6

7

8

9

10

11

12

13

14
[小时] 15[小时]
16

17

18

19

20

21

22
[小时] 23[小时] [小时] 24[小时] [小时] 25[小时] [小时] 26[小时] [小时] 27[小时] [小时] 28[小时] [小时] 29[小时] [小时] ] 30[小时] [小时] 31[小时] [小时] 32[小时] [小时] 33[小时] [小时] 34[小时] [小时] 35[小时] [小时] 36[小时] [小时] 37[小时][小时] 38[小时][小时] 39[小时][小时] 40[小时][小时] 41[小时][小时] 42[小时][小时] 43[小时][小时] 44[小时][小时] 45[小时][小时] 46[小时][小时] 47[小时][小时] 48[小时][小时] 49[小时][小时] 50[小时]]
51

52

53

54

55

56

57

58

59

60

61

62

63

64
[小时] 65[小时] [小时] 66[小时] [小时] 67[小时] [小时] 68[小时] [小时] 69[小时] [小时] 70[小时] [小时] 71[小时] [小时] ] 72[小时] [小时] 73[小时] [小时] 74[小时] [小时] 75[小时] [小时] 76[小时] [小时] 77[小时] [小时] 78[小时] [小时] 79[小时] [小时] 80[小时] [小时] 81[小时] [小时] 82[小时] [小时] 83[小时] [小时] 84[小时] [小时]85[小时] [小时] 86[小时] [小时] 87[小时] [小时] 88[小时] [小时] 89[小时] [小时] 90[小时] [小时] 91[小时] [小时] 92

93

94

95

96

97

98

99[小时] [小时] 100[小时] [小时] 101[小时] [小时] 102[小时] [小时] 103[小时] [小时] 104[小时] [小时] 105[小时] [小时] 106[小时] ]
107

108

109

110

111

112

113[h ] [小时] 114[小时] [小时] 115[小时] [小时] 116[小时] [小时] 117[小时] [小时] 118[小时] [小时]119[小时][小时] 120[小时][小时] 121[小时][小时] 122[小时][小时] 123[小时][小时] 124[小时][小时] 125[小时][小时] 126

127

128

129

130

131

132

133[小时] [小时] 134[小时] [小时] 135[小时] [小时] 136[小时] [小时] 137[小时] [小时] 138[小时] [小时] 139[小时] [小时] 140[小时] ]
141

142

143

144

145

146

147[h ] [小时] 148[小时] [小时] 149[小时] [小时] 150[小时] [小时] 151[小时] [小时] 152[小时]
153

154

155

156

157

158

159
[ 160

161

162

163

164

165

166
[h ] ] 167

168

169

170

171

172

173
[h ] 174[小时] [小时] 175[小时] [小时] 176[小时] [小时] 177[小时] [小时] 178[小时] [小时] 179[小时] [小时] 180[小时] [小时] 181[小时][小时] 182[小时][小时] 183[小时][小时] 184[小时][小时] 185[小时][186

187

188

189

190

191

192
[h ] ] 193

194

195

196

197

198

199
[h ] 200

201

202

203

导入 java.io.File;
[h ] 导入 java.io.IOException;

导入 java.util.ArrayList;

导入 java.util.Arrays;

导入 java.util.HashSet;

导入 o​​rg.apache.commons.cli.CommandLine;

导入 o​​rg.apache.commons.cli.CommandLineParser;

导入 o​​rg.apache.commons.cli.DefaultParser;

导入 o​​rg.apache.commons.cli.HelpFormatter;

导入 o​​rg.apache .commons.cli.Option;

导入 o​​rg.apache.commons.cli.Options;

导入 o​​rg.apache.commons.cli.ParseException;

导入 com . lwt.util.DirUtil;

公共类 CommandLineUtil {

私有 String[] args;

私有选项 opts = new Options();
[h ] 私有文件 keyFile;

私有布尔加密;

私有布尔创建;

私有 boolean enName;

private File[] files;

private File[] dirs;

public File getKeyFile() {

return keyFile;[h ] ]
}

public boolean isEncrypt() {

返回加密;

}

public boolean isEnName() {
[ h] return enName;

}

public boolean isCreate() {

return create;

}

public File[] getFiles() {

返回文件;

}

public File[] getDirs() {

返回目录;[ h]
}

public CommandLineUtil(String[] args) {

this.args = args;

generatedOptions();[ h ]
parseOptions();

Duplicate_removal();

}

// 定义命令行参数

private void DefinedOptions() {[ [ h]
  Option opt_h = new Option(“h”, “显示此页面。”);

Option opt_e = new Option(“e”, “加密”, false, “加密文件。”); [ h]
Option opt_d = new Option(“d”, “解密”, false, “解密文件。”);

Option opt_c = new Option(” c”, “create”, false, “创建一个新的密钥文件。”);

Options opt_n = new option(“n”, “name”, false, “加密文件名。”) ;

option opt_k = 选项; builder(“k”).hasArg().argName(“keyFile”)

.desc(“指定密钥文件”).build();

Option opt_f = Option.builder(“f”).hasArgs().argName(“file1,file2 . ..”)

.valueSeparator(‘,’)

.desc(“以 ‘,’ 分隔句柄的文件列表”).build();
[h ] 选项opt_r = 选项

.builder(“r”)

.hasArgs()

&注: .argName(“dir1,dir1…”)

.valueSeparator(‘,’)

.desc(“以’,’分隔处理子文件。目录列表” )

.build();

选项 opt_R = 选项

.builder(“R”)

.hasArgs()[ h] [ h ] .argName(“dir1,dir1…”)

                                              sp; .valueSeparator(‘,’)

.desc(“递归子文件的目录列表”)

.build();[ h] [h ] opts .addOption(opt_n);

opts.addOption(opt_c);

opts.addOption(opt_k);

opts.addOption(opt_h) ;
[ h] opts.addOption(opt_e);

opts.addOption(opt_d);

opts.addOption(opt_f);

&注: opts.addOption(opt_r);

opts.addOption(opt_R);

}

// 解析处理命令行参数

private void parseOptions (){

CommandLineParser parser = new DefaultParser();

CommandLine line = null;

// 解析命令行参数 [ h]
try {[ h ]
line = parser.parse(opts, args);

} catch (ParseException e) {

System.err.println(e .getMessage()); [小时] [小时]System.exit(1);

}

// 如果指定 h

,则显示帮助 if (args == null | | args .长度 == 0 || line.hasOption(“h”)) {

Help = new HelpFormatter();

help.printHelp(“加密”, opts);[ h ] [ h] }

// 选择加密或解密操作。 默认情况下,文件已加密

if (line.hasOption(“d”) ) {[h ]
if (line.hasOption(“e”)) {

System.err

.println(“不能同时指定-e和-d选项。”);
[ h ] System.exit(1);

}

加密 = false;

} else {

加密 = true;
[h ] if(line .hasOption(“n”)){
enName= true;

}

}

if (line.hasOption(“k”)) {

String k = line.getOptionValue(“k” “);

file file = new File(k);

if (line.hasOption(“c”)) {

keyFile = file;
[ h] create = true;

}else {

if(file.isFile()){

keyFile = file;

} else{

System.err.println(file + ” 不是可用的密钥文件”); ]
System.exit(1);

}

}

}

ArrayList 文件 = new ArrayList();

ArrayList dirs = new ArrayList();

if (line.hasOption(“f”)) {

String[] fs = line.getOptionValues(“f”) ;

for(String f : fs){

文件 file = new File(f);

if(file.isFile()){
[h ] |文件”);

System.exit(1);

}

}

}

[h ] if (line.hasOption(“r”)) {

String[] rs = line.getOptionValues(“r”);

for(String r : rs){
[ h] 文件目录 = new File(r);

if(dir.isDirectory()){

dirs.add(dir);

DirUtil dirUtil = new DirUtil(dir);

files.addAll(Arrays.asList(dirUtil.getFiles()));

dirs.addAll(Arrays.asList(dirUtil.getDirs()));

}else{

System.err.println(dir + ” 不是目录”);

System.exit(1);

  }

}

}

if (line.hasOption(“R”)) {

字符串[ ] Rs = line.getOptionValues(“R”);

for(String R : Rs){

文件目录 = new File(R);

if(dir . isDirectory ()){

dirs.add(dir);

 DirUtil dirUtil = new DirUtil(dir);

files.addAll(Arrays.asList(dirUtil.getAllFiles()));

dirs.addAll(Arrays.asList(dirUtil.getAllDirs) ) ()));

else{

System.err.println(dir + ” 不是目录”);

System.exit(1);[ h ]
}

}

}

this.files = files.toArray(new file[0]);

this.dirs = dirs.toArray(new file[0]);

}[ h]
public void private_removal (){

HashSet fileSet = new HashSet();

for(文件 file : files){
[h ] ] try {

fileSet.add(file.getCanonicalFile());

} catch (IOException e) {

System.err.println(e.getMessage());

System.exit(1);

}

}

files = fileSet.toArray(new File[0]) ;

fileSet = new HashSet();

for(File dir : dirs){

try {

fileSet.add(dir .getCanonicalFile());

catch (IOException e) {
System.err.println(e.getMessage());

System.exit(1);

}

}

dirs = fileSet.toArray(new File[0]);

}

}

概述

以上是编辑器中介绍的使用Apache commons-cli包解析命令行参数的示例代码。 我希望这对你有帮助。 请帮我。 如有疑问,请留言。 编辑会及时回复。 另外,感谢您对 国外VPS主机测评 网站的支持。

原文链接:https://blog.csdn.net/xuejianbest/article/details/80404971

未经允许不得转载:国外VPS主机测评 » 使用 Apache commons-cli 包进行命令行参数解析的示例代码
任何商家都有倒闭和跑路可能,本站仅分享优惠活动及信息,购买前请自行斟酌,衡量评估风险,自负责任。数据勤备份是最佳选择!友情提示

评论 抢沙发

评论前必须登录!