commons-cli第三方库

本文介绍了一个使用Apache Commons CLI库进行命令行参数解析的Java示例。通过创建自定义的命令行解析器,文章展示了如何定义并处理命令行选项,如帮助信息、表格导入、文件路径及压缩类型等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导航设置

package com.sjj.advance.thirdlibrary.commons_cli;

import org.apache.commons.cli.*;

public class CommandDemo {
    private static CommandLineParser parser = new BasicParser();

    /**
     * 命令参数信息
     */
    private static Options options;

    public static void main(String[] args) {
        initCommandLine();
        try {
            CommandLine commandline = parser.parse(options, args);
            if(commandline.hasOption("h")){
                showHelp();
            }else if(commandline.hasOption("t")&& commandline.hasOption("")){
                // TODO
            }
            else {
                showHelp();
            }
        } catch (ParseException e) {
            System.out.println("解析命令行报错");
        }

    }

    /**
     * 初始化命令行
     */
    private static void initCommandLine() {
        options = new Options();

        Option helpOption = new Option("h","help",false,"Print the usage information");
        options.addOption(helpOption);

        Option tableOption = new Option("t","table",true,"import which table");
        tableOption.setArgs(1);
        tableOption.setArgName("tableNmae");
        options.addOption(tableOption);

        Option fileOption = new Option("f","file",true,"filePath location");
        fileOption.setArgs(1);
        fileOption.setArgName("filePath");
        options.addOption(fileOption);

        Option compressOption = new Option("c","compress",true,"compress type(NONE,ZLIB,SNAPPY,LZO,LZ4,ZSTD)");
        compressOption.setArgs(1);
        compressOption.setArgName("compressType");
        options.addOption(compressOption);

        Option sizeOption = new Option("s","size",true,"size of one orc file");
        sizeOption.setArgs(1);
        sizeOption.setArgName("orcSize");
        options.addOption(compressOption);

    }

    private static void showHelp(){
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("sh dataImport.sh [option]",options);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值