In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Apache's commons-cli package is designed to parse the command-line argument format.
Dependence:
Commons-cli commons-cli 1.3.1
Using this package requires:
1. First define which parameters need to be parsed, which parameters have additional options, the description of each parameter, and so on, corresponding to the Options class
For example, a command line argument is-hfbv. The purpose of our definition of Options is to indicate which parameters really need to be parsed: for example, if we define Option:h, f, b, then the parser will know how to match the command line with the defined Option to get each parameter. And you can define which parameters need options, such as tar-f, f parameters need file name options, by defining a parser to parse the content after f to the file name specified by f.
two。 Parse the command line parameters according to the defined parameters that need to be parsed, corresponding to the CommandLineParser class
Parse the incoming String [] argus parameters according to the defined Options object to match each parameter, and then we can get each parameter individually.
3. After parsing, the CommandLine object is returned, which can get the information about the command line parameters.
You can know from this object which parameters have been entered, which parameters have not been entered, the contents of the additional options for which parameters, and so on. Then we can write our own code to perform different logic according to different parameters.
Sample code:
Import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet; import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.CommandLineParser;import org.apache.commons.cli.DefaultParser;import org.apache.commons.cli.HelpFormatter;import org.apache.commons.cli.Option;import org.apache.commons.cli.Options;import org.apache.commons.cli.ParseException; import com.lwt.util.DirUtil public class CommandLineUtil {private String [] args; private Options opts = new Options (); private File keyFile; private boolean encrypt; private boolean create; private boolean enName; private File [] files; private File [] dirs; public File getKeyFile () {return keyFile;} public boolean isEncrypt () {return encrypt;} public boolean isEnName () {return enName;} public boolean isCreate () {return create;} public File [] getFiles () {return files } public File [] getDirs () {return dirs;} public CommandLineUtil (String [] args) {this.args = args; definedOptions (); parseOptions (); duplicate_removal ();} / / define command line parameter private void definedOptions () {Option opt_h = new Option ("h", "Show this page."); Option opt_e = new Option ("e", "encrypt", false, "Encrypt file.") Option opt_d = new Option ("d", "decrypt", false, "Decrypt file."); Option opt_c = new Option ("c", "create", false, "Create new key file."); Option opt_n = new Option ("n", "name", false, "Encrypt file name.") Option opt_k = Option.builder ("k"). HasArg (). ArgName ("keyFile") .desc ("Specify the key file"). Build (); Option opt_f = Option.builder ("f"). HasArgs (). ArgName ("file1,file2...") .valueSeparator (',') .desc ("A files list with', 'separate to handle"). Build () Option opt_r = Option .builder ("r") .hasArgs () .argName ("dir1,dir1...") .valueSeparator (',') .desc ("A directories list with', 'separate to handle its child files") .build () Option opt_R = Option .builder ("R") .hasArgs () .argName ("dir1,dir1...") .valueSeparator (',') .desc ("A directories list with', 'separate to recurse handle child files") .build (); opts.addOption (opt_n); opts.addOption (opt_c); opts.addOption (opt_k); opts.addOption (opt_h) Opts.addOption (opt_e); opts.addOption (opt_d); opts.addOption (opt_f); opts.addOption (opt_r); opts.addOption (opt_R);} / / parse the command line parameter private void parseOptions () {CommandLineParser parser = new DefaultParser (); CommandLine line = null; / / parse the command line parameter try {line = parser.parse (opts, args) } catch (ParseException e) {System.err.println (e.getMessage ()); System.exit (1);} / / if h is specified, help if (args = = null | | args.length = = 0 | | line.hasOption ("h")) {HelpFormatter help = new HelpFormatter (); help.printHelp ("encrypt", opts) } / / Select encryption or decryption operation. Default is encrypted file if (line.hasOption ("d")) {if (line.hasOption ("e")) {System.err .println ("The-e and-d option can't specify at the same time."); System.exit (1);} encrypt = false;} else {encrypt = true If (line.hasOption ("n")) {enName = true;}} if (line.hasOption ("k")) {String k = line.getOptionValue ("k"); File file = new File (k); if (line.hasOption ("c")) {keyFile = file; create = true;} else {if (file.isFile ()) {keyFile = file } else {System.err.println (file + "is not an available key file"); System.exit (1);} ArrayList files = new ArrayList (); ArrayList dirs = new ArrayList (); if (line.hasOption ("f")) {String [] fs = line.getOptionValues ("f"); for (String f: fs) {File file = new File (f) If (file.isFile ()) {files.add (file);} else {System.err.println (file + "is not a file"); System.exit (1);}} if (line.hasOption ("r")) {String [] rs = line.getOptionValues ("r") For (String r: rs) {File dir = new File (r); if (dir.isDirectory ()) {dirs.add (dir); DirUtil dirUtil = new DirUtil (dir); files.addAll (Arrays.asList (dirUtil.getFiles (); dirs.addAll (Arrays.asList (dirUtil.getDirs () } else {System.err.println (dir + "is not a directory"); System.exit (1);} if (line.hasOption ("R")) {String [] Rs = line.getOptionValues ("R"); for (String R: Rs) {File dir = new File (R) If (dir.isDirectory ()) {dirs.add (dir); DirUtil dirUtil = new DirUtil (dir); files.addAll (Arrays.asList (dirUtil.getAllFiles (); dirs.addAll (Arrays.asList (dirUtil.getAllDirs ();} else {System.err.println (dir + "is not a directory"); System.exit (1) }} this.files = files.toArray (new File [0]); this.dirs = dirs.toArray (new File [0]);} public void duplicate_removal () {HashSet fileSet = new HashSet (); for (File file: files) {try {fileSet.add (file.getCanonicalFile ());} catch (IOException e) {System.err.println (e.getMessage ()) System.exit (1);}} files = fileSet.toArray (new File [0]); fileSet = new HashSet (); for (File dir: dirs) {try {fileSet.add (dir.getCanonicalFile ());} catch (IOException e) {System.err.println (e.getMessage ()); System.exit (1) Dirs = fileSet.toArray (new File [0]);}}
Summary
The above is the sample code introduced by the editor to you using the Apache commons-cli package to parse the command line parameters. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.