View Javadoc

1   /**
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package cpptools;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * Provides a means to format an application usage.
36   *
37   * @author Mathieu Champlon
38   */
39  public final class Usage
40  {
41      private static final int SECURITY_PADDING = 5;
42      private static final int DEFAULT_PADDING = 2;
43      private final List<String> options = new ArrayList<String>();
44      private final List<String> descriptions = new ArrayList<String>();
45      private final String name;
46      private final String url;
47      private final String version;
48      private int padding;
49  
50      /**
51       * Create a usage helper.
52       *
53       * @param name the application name
54       * @param url the application site url
55       * @param version the application version
56       */
57      public Usage( final String name, final String url, final String version )
58      {
59          this.name = name;
60          this.url = url;
61          this.version = version;
62          this.padding = DEFAULT_PADDING;
63      }
64  
65      /**
66       * Add an option description.
67       *
68       * @param option the name of the option
69       * @param description the description of the option
70       */
71      public void addOption( final String option, final String description )
72      {
73          options.add( option );
74          padding = Math.max( padding, option.length() + SECURITY_PADDING );
75          descriptions.add( description );
76      }
77  
78      /**
79       * Display the usage information.
80       */
81      public void display()
82      {
83          System.out.println();
84          System.out.println( "Usage: " + name + " [options] [file [file2 [directory [directory2] ...]]]" );
85          System.out.println( "Version: " + version );
86          System.out.println();
87          System.out.println( "Options:" );
88          for( int index = 0; index < options.size(); ++index )
89          {
90              final String string = "  -" + options.get( index );
91              System.out.print( string );
92              pad( string );
93              System.out.println( descriptions.get( index ) );
94          }
95          System.out.println();
96          System.out.println( "See " + url + " for more information." );
97      }
98  
99      private void pad( final String string )
100     {
101         for( int i = padding - string.length(); i > 0; --i )
102             System.out.print( ' ' );
103     }
104 }