• 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());
                }
            }
        }
    }
  • 相关阅读:
    基于perl的网络爬虫
    ios cell展示可滑动的图片
    iOS计算字符串的宽度高度
    swift水波效果
    iOS添加另一个控制器的时候要注意啊
    swift隐藏显示导航栏的底线
    swift集成alamofire的简单封装
    tableview详细介绍
    xmpp xml基本语义
    xmpp SASL 定义
  • 原文地址:https://www.cnblogs.com/zyanrong/p/12738478.html
Copyright © 2020-2023  润新知