JAKARTA-COMMON-CLI Use Notes

Source: Internet
Author: User
Tags dsn

This is a tool for processing commands. For example, the main method input string[] needs parsing. You can define the rules of the parameters in advance, and then you can invoke the CLI to resolve them.

Download Address: http://commons.apache.org/downloads/download_cli.cgi

The following is a demo code for generating command-line comments using the CLI:

Package demo;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;

Import Org.apache.commons.cli.BasicParser;
Import Org.apache.commons.cli.CommandLine;
Import Org.apache.commons.cli.HelpFormatter;
Import org.apache.commons.cli.Options;
Import org.apache.commons.cli.ParseException;

public class Optionstip ... {

public static void Main (string[] args) ... {

BufferedReader in=new BufferedReader (New InputStreamReader (system.in));
Try ... {
args = In.readline (). Split ("");
Options opt = new options ();
Opt.addoption ("H", false, "Print Help for this application");
Opt.addoption ("U", true, "the username to use");
Opt.addoption ("DSN", True, "the data source to use");
Basicparser parser = new Basicparser ();
CommandLine cl = Parser.parse (opt, args);
if (cl.hasoption (' h ')) ... {
Helpformatter f = new Helpformatter ();
F.printhelp ("Optionstip", opt);
else ... {
System.out.println (Cl.getoptionvalue ("U"));
System.out.println (Cl.getoptionvalue ("DSN"));
}
catch (ParseException e) ... {
E.printstacktrace ();
catch (IOException e) ... {
E.printstacktrace ();
}
}
}
After the appeal code is run, enter-h on the console, displaying the following information:

usage: OptionsTip
-dsn <arg>  The data source to use
-h      Print help for this application
-u <arg>   The username to use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.