java解析命令行参数(common-cli)练习
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)练习的更多相关文章
- java 解析命令行参数
下载地址: https://jcenter.bintray.com/org/apache/commons/com.springsource.org.apache.commons.cli/1.2.0/ ...
- boost之program_options库,解析命令行参数、读取配置文件
一.命令行解析 tprogram_options解析命令行参数示例代码: #include <iostream> using namespace std; #include <boo ...
- optparse模块解析命令行参数的说明及优化
一.关于解析命令行参数的方法 关于“解析命令行参数”的方法我们一般都会用到sys.argv跟optparse模块.关于sys.argv,网上有一篇非常优秀的博客已经介绍的很详细了,大家可以去这里参考: ...
- python解析命令行参数
常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...
- linux 中解析命令行参数(getopt_long用法)
linux 中解析命令行参数(getopt_long用法) http://www.educity.cn/linux/518242.html 详细解析命令行的getopt_long()函数 http:/ ...
- C语言中使用库函数解析命令行参数
在编写需要命令行参数的C程序的时候,往往我们需要先解析命令行参数,然后根据这些参数来启动我们的程序. C的库函数中提供了两个函数可以用来帮助我们解析命令行参数:getopt.getopt_long. ...
- Windows下解析命令行参数
linux通常使用GNU C提供的函数getopt.getopt_long.getopt_long_only函数来解析命令行参数. 移植到Windows下 getopt.h #ifndef _GETO ...
- 使用 Apache Commons CLI 解析命令行参数示例
很好的输入参数解析方法 ,转载记录下 转载在: https://www.cnblogs.com/onmyway20xx/p/7346709.html Apache Commons CLI 简介 Apa ...
- 3.QT中QCommandLineParser和QCommandLineOption解析命令行参数
1 新建项目 main.cpp #include <QCoreApplication> #include <QCommandLineParser> #include & ...
随机推荐
- Xcode7 Cocoapods 安装或更新出现错误
好长时间没有玩过CocoaPods了,今天在执行 pod install --verbose --no-repo-update 的时候出现了错误如下 [MT] DVTAssertions: ASSER ...
- Linux mint 14输入法问题
新安装了Linux mint 14,莫名其妙地没有了中文输入法,安装并设置IBUS为默认输入法,但怎么也没反应.点击输入法图标,上面显示“No input window”,其实这不关输入法自身程序和设 ...
- 黑马.net12期视频教程
完整高清视频http://www.ggfenxiang8.com/?p=301
- php实现的笛卡儿积
之前在网上找到一个大牛写的版本(网址已经记不得了..),如下 function Descartes1() { //Cartesian product of arrays or strings or s ...
- MySQL 重装
由于之前第一次装MySQL,默认的datadir在启动盘中,我想要将datadir移动到更大的存储盘中.无奈网上的各种文章的方法在我这里总是不work.我决定重新用homebrew来装一遍MySQL. ...
- 解决Oracle在scott用户下创建视图(VIEW)权限不足的方法
问题描述:在scott用户下创建视图的时候,报错:权限不足.(其他用户以此类推)解决方法: 以dba用户登录 sqlplus / as sysdba 赋予scott用户创建VIEW的权限 grant ...
- jsp基础知识
- rigidbody2D.velocity 提示缺少using?用的unity5?
请用 GetComponent<Rigidbody2D>().velocity
- 单点登录实现----CAS(一)
最近我们部门交接了一个新项目--- passport,即我司的单点登录系统,虽然没有交接给我,但是个人觉得登录技术是个很好的知识,于是就忙里偷闲简单地学习了下. 单点登录SSO(single sign ...
- ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...