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   * Implements a simple program options handler.
36   *
37   * @author Mathieu Champlon
38   */
39  public class Options
40  {
41      private final String[] args;
42  
43      /**
44       * Create a program options handler.
45       *
46       * @param args program arguments
47       */
48      public Options( final String[] args )
49      {
50          this.args = args.clone();
51      }
52  
53      /**
54       * Retrieve a list of the non-parsable remaining arguments.
55       *
56       * @return a list of arguments
57       */
58      public final List<String> getArgList()
59      {
60          final List<String> result = new ArrayList<String>();
61          for( int i = 0; i < args.length; ++i )
62              if( !args[i].startsWith( "-" ) )
63                  result.add( args[i] );
64          return result;
65      }
66  
67      /**
68       * Test if an option is set.
69       *
70       * @param name the option
71       * @return whether the option has been set or not
72       */
73      public final boolean hasOption( final String name )
74      {
75          for( int i = 0; i < args.length; ++i )
76              if( args[i].startsWith( '-' + name ) )
77                  return true;
78          return false;
79      }
80  
81      private boolean isOption( final String arg, final String name )
82      {
83          return arg.startsWith( '-' + name );
84      }
85  
86      /**
87       * Retrieve a list of all values for a given option.
88       * <p>
89       * With the option <em>-optionvalue</em> and the name <em>option</em> will return <em>value</em>.
90       *
91       * @param name the option
92       * @return a list of values
93       */
94      public final List<String> getOptionValues( final String name )
95      {
96          final List<String> result = new ArrayList<String>();
97          for( int i = 0; i < args.length; ++i )
98              if( isOption( args[i], name ) )
99                  result.add( args[i].substring( 1 + name.length() ) );
100         return result;
101     }
102 
103     /**
104      * Retrieve a list of all properties for a given option.
105      * <p>
106      * With the option <em>-optionproperty=value</em> and the name <em>option</em> will return <em>property</em>.
107      *
108      * @param name the option
109      * @return a list of values
110      */
111     public final List<String> getOptionProperties( final String name )
112     {
113         final List<String> result = new ArrayList<String>();
114         for( int i = 0; i < args.length; ++i )
115             if( isOption( args[i], name ) )
116             {
117                 final int index = args[i].indexOf( '=' );
118                 if( index == -1 )
119                     result.add( args[i].substring( 1 + name.length() ) );
120                 else
121                     result.add( args[i].substring( 1 + name.length(), index ) );
122             }
123         return result;
124     }
125 
126     /**
127      * Retrieve a list of all property values for a given option.
128      * <p>
129      * With the option <em>-optionproperty=value</em> and the name <em>option</em> will return <em>value</em>.
130      *
131      * @param name the option
132      * @return a list of values
133      */
134     public final List<String> getOptionPropertyValues( final String name )
135     {
136         final List<String> result = new ArrayList<String>();
137         for( int i = 0; i < args.length; ++i )
138             if( isOption( args[i], name ) )
139             {
140                 final int index = args[i].indexOf( '=' );
141                 if( index == -1 )
142                     result.add( "" );
143                 else
144                     result.add( args[i].substring( 1 + index ) );
145             }
146         return result;
147     }
148 }