Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/yaftp/utils/CommandArgs.java


1    /**
2    *
3    * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package com.yaftp.utils;
20  
21  import java.util.*  ;
22  
23  /**
24  
25    a practical class to scan Unix style command lines arguments
26    parser
27  
28    @author Jean-Yves MENGANT
29  
30  */
31  
32  public class CommandArgs {
33  
34      public final static boolean CASE_SENSITIVE   = true  ;
35      public final static boolean CASE_INSENSITIVE = false ;
36      public final static boolean MANDATORY  = true  ;
37      public final static boolean OPTIONAL   = false ;
38      public final static boolean HAVEVALUE  = true  ;
39      public final static boolean ISSET      = false ;
40  
41      private final static String _PARM_NAME_START_CHARACTER_ = "-" ;
42  
43      private String _programName  ; // name of the program to be displayed
44      private String _example ; // optional example to be displayed
45      private String _args[]                    ; // current Command Line
46      private Hashtable _argList  = new Hashtable()   ; // acceptables _ARGS_
47      private Vector _userArgs    = new Vector()      ; // user args are stored there
48  
49      // defining acceptable argument
50      class _ARGS_ {
51         private String  _name           ; // Argument name
52         private boolean _caseSensitive  ;
53         private boolean _mandatory      ;
54         private boolean _hasValue       ;
55         private String  _comment        ;
56         private String  _value          ;
57         private boolean _set            ;
58  
59         public _ARGS_( String  name ,
60                        boolean caseSensitive ,
61                        boolean mandatory     ,
62                        boolean hasValue      ,
63                        String  comment
64                      )
65         {
66           _name          = name ;
67           _caseSensitive = caseSensitive ;
68           _mandatory     = mandatory ;
69           _comment       = comment ;
70           _hasValue      = hasValue ;
71         }
72  
73         public boolean hasValue()
74         { return _hasValue ; }
75  
76         public boolean is_mandatory()
77         { return _mandatory  ; }
78  
79         public boolean is_set()
80         { return _set ; }
81  
82         public boolean is_caseSensitive()
83         { return _caseSensitive ; }
84  
85         public String get_name()
86         { return _name ; }
87  
88         public String get_value()
89         { return _value ; }
90  
91         public void set_value ( String value )
92         { _value = value ; }
93  
94         public void set( boolean onof )
95         { _set = onof ; }
96  
97         public String toString()
98         {
99         String returned = _name + " " ;
100          if ( _comment != null )
101            returned += _comment  ;
102          return returned ;
103        }
104     }
105 
106     public CommandArgs( String arg[] )
107     { _args     = arg     ; }
108 
109     public Enumeration getUserArguments()
110     { return _userArgs.elements() ; }
111 
112     public int getUserArgumentSize()
113     { return _userArgs.size() ; }
114 
115     public void set_programName ( String programName )
116     { _programName = programName ; }
117 
118     public void set_example ( String example )
119     { _example = example ; }
120 
121     private void displayParmList( String error )
122     {
123       if ( _programName != null )
124         System.out.println("java " + _programName ) ;
125 
126       Enumeration e = _argList.elements() ;
127       while ( e.hasMoreElements() )
128         System.out.println( (_ARGS_) e.nextElement() ) ;
129 
130       if ( _example != null )
131         System.out.println(_example) ;
132       System.out.println(error) ;
133       return ;
134     }
135 
136     public void accept( String  name ,
137                         boolean caseSensitive ,
138                         boolean mandatory ,
139                         boolean hasValue  ,
140                         String  comment
141                       )
142     {
143       _argList.put ( name.toUpperCase() ,
144                      new _ARGS_( name,
145                                  caseSensitive,
146                                  mandatory,
147                                  hasValue ,
148                                  comment
149                                 )
150                     ) ;
151     }
152 
153     private boolean parameter_missing( String args[] , int cur )
154     {
155       if ( cur < args.length )
156         return false ;
157       else
158       {
159         displayParmList("** ERROR : parameter missing" ) ;
160         return true ;
161       }
162     }
163 
164     private boolean checkMandatory()
165     {
166     Enumeration list = _argList.elements() ;
167 
168       while ( list.hasMoreElements() )
169       {
170       _ARGS_  cur = (_ARGS_) list.nextElement() ;
171         if (    ( cur.is_mandatory() )
172              && ( cur.get_value() == null )
173            )
174         {
175           displayParmList( cur.get_name() + " is mandatory !!! " ) ;
176           return false ;
177         }
178       }
179       return true ;
180     }
181 
182     public  boolean checkCommandLine()
183     /**
184       pick up  each possible parameter and set corresponding
185       value
186     */
187     {
188     int curParm = 0     ;
189 
190       while ( curParm < _args.length )
191       {
192         // check only '-' starting parameters
193         if ( _args[curParm].startsWith(_PARM_NAME_START_CHARACTER_) )
194         {
195           // do not make any control if arglist is empty
196           if ( ! _argList.isEmpty() )
197           {
198             _ARGS_ parm = (_ARGS_) _argList.get ( _args[curParm].toUpperCase() ) ;
199             if ( parm != null  )
200             {
201               if ( parm.is_caseSensitive() )
202               {
203                 if ( parm.get_name().equals(_args[curParm]) )
204                   curParm++ ;
205                 else
206                 {
207                   displayParmList("**ERROR : invalid parameter :"+_args[curParm] ) ;
208                   return false ;
209                 }
210               }
211               else
212                 curParm++ ;
213 
214               if ( parm.hasValue() )
215               {
216                 if ( curParm == _args.length )
217                   displayParmList(   "** Parameter Value missing for "
218                                    + parm.get_name()
219                                    + " **" ) ;
220                 else
221                   parm.set_value(_args[curParm]) ;
222               }
223               else
224                 parm.set(true) ;
225             }
226             //else
227             //{
228               // VPHAM 2000/09/04
229               // The command line can contains more elements (optional parameters)
230               // than the list of arguments we have to check.
231               // displayParmList("**ERROR : invalid parameter :"+_args[curParm] ) ;
232               // return false ;
233             //}
234           }
235         }
236         else
237           _userArgs.addElement(_args[curParm]) ;
238 
239         curParm++  ;
240       }
241       return checkMandatory()  ;
242     }
243 
244     /**
245       returns null or the currently associated comandLine
246       parameter value
247     */
248     public String getParameterValue( String parameter )
249     {
250       _ARGS_ parm = (_ARGS_) _argList.get ( parameter.toUpperCase() ) ;
251       if ( parm != null  )
252         return parm.get_value() ;
253       return null ;
254     }
255 
256     /**
257       returns true if given parameter is presently set
258     */
259     public boolean isParameterSet( String parameter )
260     {
261       _ARGS_ parm = (_ARGS_) _argList.get ( parameter.toUpperCase() ) ;
262       if ( parm != null  )
263         return parm.is_set() ;
264       return false ;
265     }
266 
267     public static void main( String args[] )
268     {
269       System.out.println("CommandArgs testing") ;
270 
271       CommandArgs argument  = new CommandArgs( args ) ;
272       argument.accept( "-test" ,
273                    CommandArgs.CASE_SENSITIVE ,
274                    CommandArgs.MANDATORY  ,
275                    CommandArgs.HAVEVALUE  ,
276                    "it's toto"
277                  )  ;
278 
279       if ( argument.checkCommandLine() )
280       {
281         System.out.println("Ok I got it : "+ argument.getParameterValue("-test") ) ;
282         Enumeration userArgs = argument.getUserArguments() ;
283 
284         System.out.println("user arguments starts here") ;
285 
286         while ( userArgs.hasMoreElements() )
287           System.out.println( (String)userArgs.nextElement() );
288 
289         System.out.println("user arguments stops here") ;
290       }
291       else
292         System.out.println("args check failed" ) ;
293 
294 
295     }
296 
297   }
298