前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java命令行参数解析

java命令行参数解析

作者头像
用户7886150
修改2021-02-26 10:10:27
2.2K0
修改2021-02-26 10:10:27
举报
文章被收录于专栏:bit哲学院

参考链接: Java 命令行参数

import org.apache.commons.cli.CommandLineParser;

import org.apache.commons.cli.BasicParser;

import org.apache.commons.cli.Options;

import org.apache.commons.cli.CommandLine;

public static void main(String[] args) throws Exception {

  // Create a Parser

  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');

  }

}

 cli下载地址:http://commons.apache.org/proper/commons-cli/download_cli.cgi 

上面是代码片段使用方法: 

java xxxx -h 

java xxxx -f 119 

http://heipark.iteye.com/blog/1397513

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档