一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

云服務器|WEB服務器|FTP服務器|郵件服務器|虛擬主機|服務器安全|DNS服務器|服務器知識|Nginx|IIS|Tomcat|

服務器之家 - 服務器技術 - 服務器知識 - 使用Apache commons-cli包進行命令行參數解析的示例代碼

使用Apache commons-cli包進行命令行參數解析的示例代碼

2022-03-10 17:01xuejianbest 服務器知識

Apache的commons-cli包是專門用于解析命令行參數格式的包。這篇文章給大家介紹使用Apache commons-cli包進行命令行參數解析的示例代碼,感興趣的朋友跟隨腳本之家小編一起學習吧

Apache的commons-cli包是專門用于解析命令行參數格式的包。

 依賴:

?
1
2
3
4
5
<dependency>
  <groupId>commons-cli</groupId>
  <artifactId>commons-cli</artifactId>
  <version>1.3.1</version>
</dependency>

使用此包需要:

1.先定義有哪些參數需要解析、哪些參數有額外的選項、每個參數的描述等等,對應Options類
 比如說一個命令行參數是 -hfbv,我們定義的Options的目的是,說明哪些參數是真正需要解析的參數:如我們定義了Option:h、f、b,那么在解析的時候解析器就可以知道怎么去用定義的Option匹配命令行從而獲取每個參數。而且可以定義哪些參數需要選項,如tar -f ,f參數就需要文件名選項,通過定義解析器才可以把f后面的內容解析為f指定的文件名。

2.根據定義的需要解析的參數對命令行參數進行解析,對應CommandLineParser類
 根據定義的Options對象去解析傳入的String[] argus參數,從而匹配出每個參數,然后我們就可以單獨獲取每個參數。

3.解析完成返回CommandLine對象,由這個對象可獲取此次命令行參數的信息。
 可以從這個對象中知道哪些參數輸入了,哪些參數沒有輸入,哪些參數的額外選項的內容等等。然后我們就能自己寫代碼根據不同參數執行不同邏輯了。

示例代碼:

?
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;?
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;?
import com.lwt.util.DirUtil;?
public class CommandLineUtil {
  private String[] args;
  private Options opts = new Options();
  private File keyFile;
  private boolean encrypt;
  private boolean create;
  private boolean enName;
  private File[] files;
  private File[] dirs;
  public File getKeyFile() {
    return keyFile;
  }
  public boolean isEncrypt() {
    return encrypt;
  }
  public boolean isEnName() {
    return enName;
  }
  public boolean isCreate() {
    return create;
  }
  public File[] getFiles() {
    return files;
  }
  public File[] getDirs() {
    return dirs;
  }
?
  public CommandLineUtil(String[] args) {
    this.args = args;
    definedOptions();
    parseOptions();
    duplicate_removal();
  }
  // 定義命令行參數
  private void definedOptions(){
    Option opt_h = new Option("h", "Show this page.");
    Option opt_e = new Option("e", "encrypt", false, "Encrypt file.");
    Option opt_d = new Option("d", "decrypt", false, "Decrypt file.");
    Option opt_c = new Option("c", "create", false, "Create new key file.");
    Option opt_n = new Option("n", "name", false, "Encrypt file name.");
    Option opt_k = Option.builder("k").hasArg().argName("keyFile")
        .desc("Specify the key file").build();
    Option opt_f = Option.builder("f").hasArgs().argName("file1,file2...")
        .valueSeparator(',')
        .desc("A files list with ',' separate to handle").build();
    Option opt_r = Option
        .builder("r")
        .hasArgs()
        .argName("dir1,dir1...")
        .valueSeparator(',')
        .desc("A directories list with ',' separate to handle its child files")
        .build();
    Option opt_R = Option
        .builder("R")
        .hasArgs()
        .argName("dir1,dir1...")
        .valueSeparator(',')
        .desc("A directories list with ',' separate to recurse handle child files")
        .build();
    opts.addOption(opt_n);
    opts.addOption(opt_c);
    opts.addOption(opt_k);
    opts.addOption(opt_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;
    // 解析命令行參數
    try {
      line = parser.parse(opts, args);
    } catch (ParseException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }
?
    // 若指定h則顯示幫助
    if (args == null || args.length == 0 || line.hasOption("h")) {
      HelpFormatter help = new HelpFormatter();
      help.printHelp("encrypt", opts);
    }
?
    // 選擇加密或解密操作,默認是加密文件
    if (line.hasOption("d")) {
      if (line.hasOption("e")) {
        System.err
            .println("The -e and -d option can't specify at the same time.");
        System.exit(1);
      }
      encrypt = false;
    } else {
      encrypt = true;
      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;
        create = true;
      }else {
        if(file.isFile()){
          keyFile = file;
        } else{
          System.err.println(file + " is not a available key file");
          System.exit(1);
        }
      }
    }
?
    ArrayList<File> files = new ArrayList<File>();
    ArrayList<File> dirs = new ArrayList<File>();
    if (line.hasOption("f")) {
      String[] fs = line.getOptionValues("f");
      for(String f : fs){
        File file = new File(f);
        if(file.isFile()){
          files.add(file);
        }else{
          System.err.println(file + " is not a file");
          System.exit(1);
        }
      }
    }
?
    if (line.hasOption("r")) {
      String[] rs = line.getOptionValues("r");
      for(String r : rs){
        File dir = 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 + " is not a directory");
          System.exit(1);
        }
      }
    }
?
    if (line.hasOption("R")) {
      String[] Rs = line.getOptionValues("R");
      for(String R : Rs){
        File dir = 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 + " is not a directory");
          System.exit(1);
        }
      }
    }
    this.files = files.toArray(new File[0]);
    this.dirs = dirs.toArray(new File[0]);
  }
  public void duplicate_removal (){
    HashSet<File> fileSet = new HashSet<File>();
    for(File file : files){
      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<File>();
    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包進行命令行參數解析的示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女班主任下面好爽好湿好紧 | 欧美人成绝费网站色www吃脚 | 亚洲图片一区二区三区 | 免费看欧美一级特黄a大片一 | 波多野结衣亚洲一区 | 亚洲高清在线天堂精品 | 午夜一区二区免费视频 | 92在线视频 | 久久精品亚洲精品国产欧美 | 91制片在线观看 | 俄罗斯美女破苞 | 欧美摸胸 | 亚洲精品卡1卡二卡3卡四卡 | 91aaa免费免费国产在线观看 | 欧美肥乳| 国产精品反差婊在线观看 | 免费观看成年肉动漫网站 | 国产一级网站 | 亚洲色图影院 | 欧美日韩亚洲综合久久久 | 亚洲视频在线免费看 | 精品无码一区在线观看 | 先锋资源av| 日韩成人在线网站 | 韩剧消失的眼角膜免费完整版 | 爱情岛永久成人免费网站 | 国产最新进精品视频 | 天天噜| 俄罗斯美女尿尿 | 欧美日本一道高清免费3区 欧美人做人爱a全程免费 | 久久久久久久99精品免费观看 | 日本高清在线精品一区二区三区 | 奇米影视久久 | 51国产| 精品国产一区二区 | 忘忧草在线 | www.精品在线 | 爱爱小视频免费看 | 四虎在线成人免费网站 | 国产成人精品免费2021 | 免费看成人毛片日本久久 |