最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Apache commons-cli包進(jìn)行命令行參數(shù)解析的示例代碼

 更新時(shí)間:2018年05月22日 14:14:40   作者:xuejianbest  
Apache的commons-cli包是專門用于解析命令行參數(shù)格式的包。這篇文章給大家介紹使用Apache commons-cli包進(jìn)行命令行參數(shù)解析的示例代碼,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧

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

 依賴:

<dependency>
  <groupId>commons-cli</groupId>
  <artifactId>commons-cli</artifactId>
  <version>1.3.1</version>
</dependency>

使用此包需要:

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

2.根據(jù)定義的需要解析的參數(shù)對命令行參數(shù)進(jìn)行解析,對應(yīng)CommandLineParser類
 根據(jù)定義的Options對象去解析傳入的String[] argus參數(shù),從而匹配出每個(gè)參數(shù),然后我們就可以單獨(dú)獲取每個(gè)參數(shù)。

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

示例代碼:

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();
  }
  // 定義命令行參數(shù)
  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);
  }
  // 解析處理命令行參數(shù)
  private void parseOptions(){
    CommandLineParser parser = new DefaultParser();
    CommandLine line = null;
    // 解析命令行參數(shù)
    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);
    }
​
    // 選擇加密或解密操作,默認(rèn)是加密文件
    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]);
  }
}

總結(jié)

以上所述是小編給大家介紹的使用Apache commons-cli包進(jìn)行命令行參數(shù)解析的示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 初識centos7與centos6的區(qū)別整理(內(nèi)核、命令等)

    初識centos7與centos6的區(qū)別整理(內(nèi)核、命令等)

    這篇文章主要介紹了初識centos7與centos6的區(qū)別整理,需要的朋友可以參考下
    2017-08-08
  • CentOS 8 正式發(fā)布 基于Red Hat Enterprise Linux 8

    CentOS 8 正式發(fā)布 基于Red Hat Enterprise Linux 8

    緊隨CentOS Linux 7.7發(fā)行版之后,CentOS Linux 8現(xiàn)已正式發(fā)布,新版本基于Red Hat Enterprise Linux 8.0源,這意味著它具有混合云時(shí)代的所有強(qiáng)大的新特性和增強(qiáng)功能
    2019-09-09
  • linux下圖形界面卡死不能操作的問題及解決

    linux下圖形界面卡死不能操作的問題及解決

    這篇文章主要介紹了linux下圖形界面卡死不能操作的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 詳解ubuntu14.04如何設(shè)置靜態(tài)IP的方法

    詳解ubuntu14.04如何設(shè)置靜態(tài)IP的方法

    本篇文章主要介紹了ubuntu14.04如何設(shè)置靜態(tài)IP的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • 關(guān)于AIX掛載NFS寫入效率低效的解決方法

    關(guān)于AIX掛載NFS寫入效率低效的解決方法

    這篇文章主要給大家介紹了關(guān)于AIX掛載NFS寫入效率低效的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • 服務(wù)器端如何開啟GZIP壓縮功能(Apache、IIS、Nginx)

    服務(wù)器端如何開啟GZIP壓縮功能(Apache、IIS、Nginx)

    在負(fù)載均衡中有一個(gè)必須要做的事情就是給服務(wù)器開啟GZIP壓縮功能,本文主要介紹了服務(wù)器端如何開啟GZIP壓縮功能,具有一定的參考價(jià)值,感興趣的可以了解下
    2022-04-04
  • Linux中chmod權(quán)限設(shè)置方式

    Linux中chmod權(quán)限設(shè)置方式

    本文介紹了Linux系統(tǒng)中文件和目錄權(quán)限的設(shè)置方法,包括chmod、chown和chgrp命令的使用,以及權(quán)限模式和符號模式的詳細(xì)說明,通過這些命令,用戶可以靈活地控制文件和目錄的訪問權(quán)限
    2025-01-01
  • ubuntu系統(tǒng)中/etc/rc.local和/etc/init.d/rc.local的區(qū)別詳解

    ubuntu系統(tǒng)中/etc/rc.local和/etc/init.d/rc.local的區(qū)別詳解

    這篇文章主要給大家介紹了關(guān)于在ubuntu系統(tǒng)下/etc/rc.local和/etc/init.d/rc.local區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對需要的朋友們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-08-08
  • Linux服務(wù)器從頭配置全過程

    Linux服務(wù)器從頭配置全過程

    這篇文章主要介紹了Linux服務(wù)器從頭配置全過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-03-03
  • Linux ssh服務(wù)器配置代碼實(shí)例

    Linux ssh服務(wù)器配置代碼實(shí)例

    這篇文章主要介紹了Linux ssh服務(wù)器配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評論

彰武县| 和平县| 博爱县| 察隅县| 吉首市| 吉安县| 芮城县| 齐河县| 宜川县| 东乌珠穆沁旗| 元谋县| 乐至县| 伽师县| 儋州市| 固镇县| 延安市| 普兰店市| 安新县| 莱西市| 攀枝花市| 阿荣旗| 汝阳县| 通辽市| 安化县| 广昌县| 慈利县| 綦江县| 游戏| 胶南市| 沈丘县| 海城市| 漠河县| 阳东县| 三门峡市| 东至县| 额济纳旗| 易门县| 将乐县| 仲巴县| 射阳县| 兴业县|