Thursday, July 29, 2021

命令行 commons.cli.*

commons-cli的简单应用
import org.apache.commons.cli.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
* @Author: yc
* @Description: cmd
* @Date: 2021/07/23/14:58
*/
public class Test {
private static CommandLine commandLine;
private static Options options = new Options();
private static String HELP_MSG = null;

private static void initTest(String args[]) {

CommandLineParser commandLineParser = new DefaultParser();

// 添加Option(通俗来讲就是添加可操作符)
/*
* 常见的Option:
* builder("h"):h代表一个参数的简短名称,换成自己需要的
* required:是否需要该参数,如果设置为true,则输入命令时需要该参数
* hasArg:代表该参数之后是否有参数,true代表输入该参数之后必须有参数,如输入账户,-n cola
* argName:该参数的名称
* desc:对该参数的描述
* longOpt:该参数的全名,在命令行输入时,简短名称和全名都可被识别,即两个名称有相同的意义,如下面的 -n 和 -name,意义相同
* type:参数类型
* */
options.addOption("help", "show help");
options.addOption(Option.builder("h").required(false).hasArg(true).argName("localhost").desc("the server of localhost").type(String.class).longOpt("hello").build());
options.addOption(Option.builder("s").required(false).hasArg(false).desc("展示").type(String.class).longOpt("show").build());
options.addOption(Option.builder("n").required(false).hasArg(true).argName("账户").desc("账户名").type(String.class).longOpt("name").build());
options.addOption(Option.builder("p").required(false).hasArg(true).argName("密码").desc("用户密码").type(String.class).longOpt("password").build());

try {
commandLine = commandLineParser.parse(options, args);
} catch (ParseException e) {
e.printStackTrace();
// 错误时,输入错误信息并将help信息打印出来
System.out.println(e.getMessage() + "\n" + getHelpString());
System.exit(0);
}

}

// main方法,将项目打成jar包,然后终端运行
public static void main(String[] args) {
initTest(args);
// 判断命令行中是否有-help,如果有,则输出help信息
if (commandLine.hasOption("help")) {
System.out.println(getHelpString());
}
// 判断命令行中是否有-s
if (commandLine.hasOption("s")) {
System.out.println("苹果、香蕉");
}
// 判断命令行中是否有-n
if(commandLine.hasOption("n"))
{
String username = commandLine.getOptionValue("n");
// 判断命令行中是否有-p
if(commandLine.hasOption("p"))
{
// 判断账户和密码是否等于张三、123
String password = commandLine.getOptionValue("p");
if(username.equals("张三") && password.equals("123"))
{
System.out.println("登录成功!!!");
}
else
{
System.out.println("登录失败!!!");
}
}
}
}

/**
* help,一般为固定的格式......

原文转载:http://www.shaoqun.com/a/893373.html

跨境电商:https://www.ikjzd.com/

西农:https://www.ikjzd.com/w/1368

gtc:https://www.ikjzd.com/w/974

贝贝官网:https://www.ikjzd.com/w/1321


commons-cli的简单应用importorg.apache.commons.cli.*;importjava.io.ByteArrayOutputStream;importjava.io.PrintWriter;/***@Author:yc*@Description:cmd*@Date:2021/07/23/14:58*/publicclassTest{privatestaticComman
首信易:https://www.ikjzd.com/w/1841
穗东深度游!这份攻略快收藏!:http://www.30bags.com/a/222224.html
穗旅游展出境游报名两边倒 :http://www.30bags.com/a/409883.html
孙俪邓超隐婚_蜜月岛屿猜想地 :http://www.30bags.com/a/410409.html
孙俪邓超有爱同游的华东小城!山清水秀,古道绝美,还差点累哭邓超!:http://www.30bags.com/a/285495.html
小东西我的尺寸你吃不消 腿打开一会就不疼了:http://lady.shaoqun.com/a/256935.html
小坏蛋今晚可以不戴套 求求你别在里面会怀孕:http://lady.shaoqun.com/a/248299.html
校花练车时给教练插了 儿媳在公交车让公爹插 女孩张开裙子给男生捅:http://lady.shaoqun.com/m/a/246891.html
怀孕期间在一个房间,肚子里的宝宝会有这三种感觉,有点神奇又害羞:http://lady.shaoqun.com/a/428538.html
同房后痛苦"下文"?这些小技巧可以帮助缓解:http://lady.shaoqun.com/a/428539.html
为什么有的女生在被侵犯的时候不逃不喊,看完不可言喻的夏天才恍然大悟:http://lady.shaoqun.com/a/428540.html
产后多久可以做爱?医生告诉你最好的时间,你必须注意这些事项:http://lady.shaoqun.com/a/428541.html

No comments:

Post a Comment