zoukankan      html  css  js  c++  java
  • org.apache.commons.cli.Options

    在使用java -jar命令执行包时,使用Options封装一些参数,
    如下方法中使用:
    public static void run(String[] args)  {
        if (args.length > 0) {
            Properties properties = new Properties();
            Options options = new Options();
            options.addOption("M", true, "main app classname");
            options.addOption("P", true, "properties filename");
            OptionBuilder.withArgName("property=value");
            OptionBuilder.hasArgs(2);
            OptionBuilder.withValueSeparator();
            OptionBuilder.withDescription("use value for given property");
            options.addOption(OptionBuilder.create("D"));
            CommandLineParser parser = new PosixParser();
            CommandLine cmd = null;
            try{
               cmd =  parser.parse(options, args);
            }catch (ParseException e){
                e.printStackTrace();
            }
            if (cmd.hasOption('M')) {
                String fullClassName = cmd.getOptionValue('M');
                if (fullClassName != null && !fullClassName.isEmpty()) {
                    if (cmd.hasOption('P')) {
                        File file = new File(cmd.getOptionValue('P'));
                        try{
                            FileInputStream fStream = new FileInputStream(file);
                            try {
                                properties.load(fStream);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }catch (FileNotFoundException e1){
                            e1.printStackTrace();
                        }
                    }
                    properties.putAll(cmd.getOptionProperties("D"));
                    properties.setProperty("conf.app.startup.timestamp", String.valueOf((new Date()).getTime())); //当前时间戳加入到配置中
                    logger.info("properties:"+properties.toString());
                }
            }
        }
    }
  • 相关阅读:
    IDEA 运行junit单元测试方法
    IDEA 修改编码
    接口文档word版
    java 上传文件到七牛云中
    单例模式
    洛谷P3092 [USACO13NOV]没有找零No Change
    Codevs 1159 最大全0子矩阵
    洛谷P2733 家的范围 Home on the Range
    洛谷P2280 [HNOI2003]激光炸弹
    洛谷P2023 [AHOI2009]维护序列
  • 原文地址:https://www.cnblogs.com/zyanrong/p/12738478.html
Copyright © 2011-2022 走看看