package foo;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options; public class test {
public static void main(String[] args) throws Exception{ CommandLineParser parser = new BasicParser();
Options options = new Options();
options.addOption("h","help",false,"Print this usage information");
options.addOption("v","verbose",false,"Print out VERBOSE information");
options.addOption("f","file",true,"File to save program output to");
//Parse the program arguments
CommandLine commandLine = parser.parse(options, args);
// Set the appropriate variables based on supplied options
boolean verbose = false;
String file = ""; if(commandLine.hasOption('h')){
System.out.println("Help Message");
System.exit(0);
} if(commandLine.hasOption('v')){
verbose = true;
} if(commandLine.hasOption('f')){
file = commandLine.getOptionValue('f');
}
}
}

A class that implements the CommandLineParser interface can parse a String array according to the Options specified and return a CommandLine.

//获取配置文件
String configXmlFile = System.getProperty("Config");
//加载配置文件
AbstractApplicationContext applicationContext = new FileSystemXmlApplicationContext(
configXmlFile);
//MBeanServer是一个包含所有注册MBean的仓库.它是JMX代理层的核心
MBeanServer mbs = java.lang.management.ManagementFactory
.getPlatformMBeanServer();
logger.info("注册ApplicationServer.");
ObjectName mbeanName = getApplicationObjectName();
ApplicationServer applicationServer = new ApplicationServer();
applicationServer.setApplicationContext(applicationContext);
mbs.registerMBean(applicationServer, mbeanName); public static ObjectName getApplicationObjectName()
throws MalformedObjectNameException {
ObjectName mbeanName = new ObjectName("fm5:name=ApplicationServer");
return mbeanName;
}

java解析命令行参数(common-cli)练习的更多相关文章

  1. java 解析命令行参数

    下载地址: https://jcenter.bintray.com/org/apache/commons/com.springsource.org.apache.commons.cli/1.2.0/ ...

  2. boost之program_options库,解析命令行参数、读取配置文件

    一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...

  3. optparse模块解析命令行参数的说明及优化

    一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...

  4. python解析命令行参数

    常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...

  5. linux 中解析命令行参数(getopt_long用法)

    linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...

  6. C语言中使用库函数解析命令行参数

    在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. ...

  7. Windows下解析命令行参数

    linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETO ...

  8. 使用 Apache Commons CLI 解析命令行参数示例

    很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...

  9. 3.QT中QCommandLineParser和QCommandLineOption解析命令行参数

     1  新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include & ...

随机推荐

  1. c++ 顺序容器适配器

    第一次看到stack,以为它是一个和vector同等地位的容器,其实不是 官方解释:stacks are a type of container adaptor, specifically desig ...

  2. Tomcat启动超过45S

    Timeout waiting for Tomcat v5.5&nbspServer @localhost  to start. Server did not start after 45s ...

  3. pylab,matplotlib Invalid DISPLAY variable

    在cetos 服务器使用源码包,安装matplotlib, 安装成功后, import pylab as pl pl.figure(figsize=(16,8)) python 解析器报错,Inval ...

  4. 手把手教你如何加入到github的开源世界!

    http://www.cnblogs.com/wenber/p/3630921.html 我曾经一直想加入到开源项目中,但是因为没有人指导流程,网上看了很多,基本都是说了个大概,如果你也是一个初出茅庐 ...

  5. margin负值的几种妙用

    1:定位+margin负值实现元素水平垂直居中 div{ position: absolute; z-index: 1; left: 50%; margin-left: -50px; width: 1 ...

  6. python内建函数

    人太懒了,博客就慢慢添加吧,这个话题还有很多要学的,后面实践了再来添加,现在就当是开个头. print(dir(__builtins__))  #获取内建属性.函数列表     print(help( ...

  7. Tomcat调试笔记

    调试笔记 在使用Tomcat过程中经常碰到问题,导致tomcat启动失败.如下↓ 由于报错太过笼统,我根本无法找出错误.后来我切换到Console视图下,看到了如下错误信息. 根据报错信息,错误原因是 ...

  8. Android开发教程:shape和selector的结合使用

    shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...

  9. Python实例1

    1.有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 错解: 正解: 源码: #!/usr/bin/python for i in range(1,5): for j in ...

  10. 详解java方法的重载

    1.方法的重载: 方法名相同,参数列表不同就叫做方法的重载.