java如何解析命令行参数

参考文章【笔记】JCommander解析命令行参数_MrXionGe的博客-CSDN博客

很多时候我们的程序都是通过main方法启动,同时传入参数。

比如 java -jar xxx.jar  args1 args2  ,这样没什么问题,但是很多时候这些参数只有我们自己知道

但是我们看好点的项目 例如datax flink,spark 等等 被人的熟知的项目 都是见名知意

比如 class 是类名, deploy-mode 是部署方式 -conf 是配置文件,为什么别人的这么优雅,我们的这么丑陋!!所以需要学习

spark-submit \

--class org.apache.spark.examples.SparkPi \

--master yarn \

--deploy-mode client \

--conf spark.dynamicAllocation.enabled=false \

--conf spark.yarn.am.memory=700m \

--conf spark.driver.memoryOverhead=1g \

--driver-memory 1g \

--executor-memory 2g \

--num-executors 2 \

--executor-cores 2 \

/data/cc_test/spark/spark-examples_2.11-2.4.0-cdh6.3.2.jar \

引入依赖

       
            com.beust
            jcommander
            1.82
       
 

   
        org.projectlombok
        lombok
        1.18.28
        compile
   

 demo 

package com.chenchi.parse.args;

import cn.hutool.json.JSONUtil;
import com.beust.jcommander.IStringConverter;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Data
public class CmdArgs2 {
    @Parameter(names = {"-s", "--string"}, description = "this is string", help = true, order = 1)
    private String s;

    @Parameter(names = {"-i", "--int"}, description = "this is int ", help = true, order = 2)
    private Integer i;

    @Parameter(names = {"-f", "--float"}, description = "this is float", help = true, order = 3)
    private float f;

    @Parameter(names = {"-d", "--decimal"}, description = "this is decimal", help = true, order = 4)
    private BigDecimal d;
    //注意加 这个
    @Parameter(names = {"-b", "--bool"}, description = "this is bool", help = true,arity = 1, order = 4)
    private boolean b;
    //password=true  这里就是类似与 mysql -uroot -p  然后输密码 和客户端交互
    @Parameter(names = {"-p", "--password"}, description = "this is password", help = true,password = true, order = 4)
    private String p;
    @Parameter(names = "--help", description = "print help message", help = true, order = 0)
    private boolean help;
    //这个通过多个参数去获取
    @Parameter(names = {"--list1","-l1"}, description = "this is list",help = true,order = 5)
    private List list1;
    //这个通过转化去获取
    @Parameter(names = {"--list2","-l2"}, description = "this is list",help = true,listConverter =myListConvert.class,order = 5)
    private List list2;
//-s chenchi -i 1234 -f 123.4567 -d 987.6543 -b false -l1 chenchi -l1 chenchi2 -l2 1,2,3,4  -p cc_password
    public static void main(String[] args) {
        System.out.println(Arrays.toString(args));
        CmdArgs2 cmdArgs = new CmdArgs2();
        JCommander commander = JCommander.newBuilder()
                //程序名
                .programName("CommandTest")
                //参数对象
                .addObject(cmdArgs)
                //创建
                .build();
        //解析参数
        commander.parse(args);
        if (cmdArgs.isHelp()) {
            //打印帮助信息
            commander.usage();
            return;
        }
        //打印转换之后的参数对象
        System.out.println(cmdArgs);
    }

    static class myListConvert implements IStringConverter>{
        @Override
        public List convert(String s) {
            String[] split = s.split(",");
            List list = Arrays.asList(split);
            return list ;
        }
    }
}

 可以将参数直接转化为list类型,或者map或者直接转化为一个pojo。

说这么多还不如直接看官网 JCommander  写的还不错通俗易懂

你可能感兴趣的:(java基础,java)