Commons CLI usage

Source: Internet
Author: User

Preface:

 

The Apache commons CLI Library provides an API for users to interpret command lines. It has three major statuses in command line interpretation: definition, interpretation, and inquiry interaction. The following sections detail the content of these three sections and how to implement them using CLI.

 

The following sections illustrate how to use commons CLI to explain and process commands.

 

 

Example:

 

The following figure shows the implementation result (the parameters and commands do not have any special significance here, just to show how to complete the corresponding functions ):

  1. Usage: gmkdir [-p] [-V/-- verbose] [-- block-size] [-H/-- help] directoryname
  2. -- Block-size use size-byte Blocks
  3. -O <File> Search for buildfile towards the root of the filesystem and use it
  4. -P no error if existing, make parent directories as needed.
  5. -V, -- verbose explain what is being done.
  6. -H, -- help print help for the command.

 

Parameter description and use cases:

-- Block-size: Use size-ByteBlocks. The usage scenario in command parameters is: gmkdir -- block-size = 10 tew.kdir.

-O <File> SearchForBuildfile towards the root of the filesystem and use it. Use the following command: gmkdir-O test.txt tew.kdir.

-P no errorIfExisting, make parent directories as needed. The usage scenario in the command is: gmkdir-P tew.kdir

-V, -- verbose explain what is being done. The usage scenario in the command is: gmkdir-V tew.kdir or gmkdir -- verbose tew.kdir

-H, -- help print helpForThe command. The usage scenario in the command is: gmkdir-H

 

Comprehensive application scenarios:

Gmkdir-h -- you can view the help information of this command;

Gmkdir-V tew.kdir or gmkdir tew.kdir-V

Gmkdir-v-P tew.kdir/test1/Test2/test3 or gmkdir tew.kdir/test1/Test2/test3-v-P

Gmkdir-O test.txt tew.kdir-V or gmkdir tew.kdir-O test.txt-V

Gmkdir -- block-size = 10 tew.kdir-V or gmkdir tew.kdir-V -- block-size = 10

Gmkdir -- block-size = 10 tew.kdir/test1/Test2/test3-P

......

 

In the above scenario, we can see that the location of the command parameters can be placed anywhere after the command. cli will help us complete the corresponding interpretation work, it should be noted that, similar to-O <File> and -- block-size, the parameters following it must be followed, such as-O test.txt and -- block-size = 10...

 

 

The above chaotic descriptions need to be implemented, as shown in the preceding command results and scenarios. The following describes the specific implementation in detail. The Code is as follows:

 

  1. Package org. system. Faye. CommandLine;
  2. Import org. Apache. commons. cli. CommandLine;
  3. Import org. Apache. commons. cli. commandlineparser;
  4. Import org. Apache. commons. cli. helpformatter;
  5. Import org. Apache. commons. cli. optionbuilder;
  6. Import org. Apache. commons. cli. options;
  7. Import org. Apache. commons. cli. parseexception;
  8. Import org. Apache. commons. cli. posixparser;
  9. Public class mkdir {
  10. /**
  11. * @ Param ARGs
  12. */
  13. Public static void main (string [] ARGs ){
  14. Options opt = new options ();
  15. Opt. addoption ("P", false, "no error if existing," +
  16. "Make parent directories as needed .");
  17. Opt. addoption ("V", "verbose", false, "explain what is being done .");
  18. Opt. addoption (optionbuilder. withargname ("file ")
  19. . Hasarg ()
  20. . Withdescription ("Search for buildfile towards the root of the filesystem and use it ")
  21. . Create ("O "));
  22. Opt. addoption (optionbuilder. withlongopt ("block-size ")
  23. . Withdescription ("use size-byte blocks ")
  24. . Withvalueseparator ('= ')
  25. . Hasarg ()
  26. . Create ());
  27. Opt. addoption ("H", "help", false, "Print help for the command .");
  28. String formatstr = "gmkdir [-p] [-V/-- verbose] [-- block-size] [-H/-- help] directoryname ";
  29. Helpformatter formatter = new helpformatter ();
  30. Commandlineparser parser = new posixparser ();
  31. CommandLine Cl = NULL;
  32. Try {
  33. // Process options and Parameters
  34. CL = parser. parse (OPT, argS );
  35. } Catch (parseexception e ){
  36. Formatter. printhelp (formatstr, OPT); // if an exception occurs, the help information is printed.
  37. }
  38. // If-H or -- help is included, the help information is printed.
  39. If (Cl. hasoption ("H ")){
  40. Helpformatter HF = new helpformatter ();
  41. HF. printhelp (formatstr, "", opt ,"");
  42. Return;
  43. }
  44. // Determine whether the-p parameter exists
  45. If (Cl. hasoption ("p ")){
  46. System. Out. println ("has P ");
  47. }
  48. // Determine whether the-V or -- verbose parameter exists
  49. If (Cl. hasoption ("v ")){
  50. System. Out. println ("has V ");
  51. }
  52. // Obtain the parameter value. The value is directoryname.
  53. String [] STR = Cl. getargs ();
  54. Int length = Str. length;
  55. System. Out. println ("length =" + length );
  56. System. Out. println ("str [0] =" + STR [0]);
  57. // Determine whether the block-size parameter is included
  58. If (Cl. hasoption ("block-size ")){
  59. // Print the value of block-size
  60. System. Out. println ("block-size =" + Cl. getoptionvalue ("block-size "));
  61. }
  62. }
  63. }

 

Due to the time relationship, we will not describe options, options, and commandlineparser for the moment. We will add them later. If you have any questions, please feel free to contact me and I will restore you as soon as possible.

 

I have a rough idea about cli. How can I check it now? (* ^__ ^ *) Xi ...... Is it cool? Haha !~

 

 

 

 

Note: If you need to reprint the record, please contact me; otherwise, you will be held legally accountable.

 

Author

9-9-27

 

 

 

 

 

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.