Java command line configuration loading module detailed

Source: Internet
Author: User
Tags static class

Recently in Java, wrote a dynamic from XML to load command-line hints, support for multi-language modules

Package Com.common;

Import org.apache.commons.cli.*;
Import Org.dom4j.io.SAXReader;

Import java.io.*;
Import Java.util.Iterator;
Import java.util.List;

/**
* Created by Lonelyrain on 16/7/12.
*/

public class Parsecommand {
Static Options optionlist;

public static CommandLine parseoption (string[] args) {

        Options options = new options ();
        CommandLine usercommand = null;
        try {
            //According to args compare the commands in XML
            UserCommand = programoptionlist.loading (options, args);
       } catch (Exception error) {
      & nbsp;     System.err.println ("error:\r\n\t==>" + error.getmessage ());
            system.exit (0);
       }
        return usercommand;
   }

public static void Help () throws Exception {
String programname = "";
Creating a parser

InputStream inputfile = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Xml/CommandList.xml") ;
Saxreader reader = new Saxreader ();
Org.dom4j.Document Document = Reader.read (inputfile);

Get root
Org.dom4j.Element root = Document.getrootelement ();
ProgramName = Root.attributevalue ("name");
Helpformatter HF = new Helpformatter ();
Hf.printhelp (ProgramName, optionlist);

}


public static class Programoptionlist {
Static CommandLine commandlist;


public static CommandLine loading (Options defaultoptions, string[] args) throws Exception {
Commandlineparser commandparser = new Defaultparser ();
__loadconfigfile (defaultoptions, "en");
CommandLine UserCommand = Commandparser.parse (defaultoptions, args);
Optionlist = defaultoptions;
if (Usercommand.hasoption ("L")) {
Options otheroptions = new options ();
__loadconfigfile (Otheroptions, Usercommand.getoptionvalue ("L"));
Optionlist = otheroptions;
}
Commandlist = UserCommand;
return commandlist;
}

private static void __loadconfigfile (options options, String language) throws Exception {
Creating a parser
Saxreader reader = new Saxreader ();

Reading documents
File Inputfile = new file ("/users/silverrat/development/java/domainsearcher/src/xml/commandlist.xml");
Org.dom4j.Document Document = Reader.read (inputfile);

Get root
Org.dom4j.Element root = Document.getrootelement ();

Getting child nodes
list<org.dom4j.element> list = Root.elements ();

for (Org.dom4j.Element e:list) {
String shortcmd = "";
String longcmd = "";
String describetion = "";
Boolean With_args = false;
for (iterator s = e.elementiterator (); S.hasnext ();) {
Org.dom4j.Element CommandInfo = (org.dom4j.Element) s.next ();
Switch (Commandinfo.getname ()) {
Case "Short":
Shortcmd = Commandinfo.getstringvalue ();
Break
Case "Long":
Longcmd = Commandinfo.getstringvalue ();
Break
Case "Describetion":
if (Commandinfo.attributevalue ("Lang"). Equals (language)) {
Describetion = Commandinfo.getstringvalue ();
}
Break
Case "With_args":
With_args = Boolean.parseboolean (Commandinfo.getstringvalue ());
Break
}
}
Options.addoption (Shortcmd, Longcmd, With_args, describetion);
}
}

}
}
The above is the parsing module
The following is the configured XML file

<?xml version= "1.0" encoding= "Utf-8"?>
<command_list name= "Domainsearcher" >
<command id= "0" >
<short>h</short>
<long>help</long>
<with_args>false</with_args>
<describetion lang= "en" >help information</describetion>
<describetion lang= "en" > Help information </describetion>
</command>
<command id= "1" >
<short>t</short>
<long>target</long>
<with_args>true</with_args>
<describetion lang= "en" >your Target host/ip</describetion>
<describetion lang= "en" > Target domain name/ip</describetion>
</command>
<command id= "2" >
<short>d</short>
<long>dict</long>
<with_args>true</with_args>
<describetion lang= "en" >your dictionary</describetion>
<describetion lang= "en" > Load dictionary path </describetion>
</command>
<command id= "3" >
<short>l</short>
<long>language</long>
<with_args>true</with_args>
<describetion lang= "en" > Selector language (EN/ZH) </describetion>
<describetion lang= "en" >choose Display Language (En/zh) </describetion>
</command>
</command_list>


Here's how to use the example

Import parsecommand.*;
public class Domainsearcher {
public static void Main (string[] args) {
Parsecommand Parsecommand = new Parsecommand ();
Parsecommand. Parse (args);
}


Row effect:

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.