avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/cli/src/java/org/apache/avalon/excalibur/cli CLArgsParser.java
Date Tue, 19 Mar 2002 11:57:17 GMT
donaldp     02/03/19 03:57:17

  Modified:    cli/src/java/org/apache/avalon/excalibur/cli
                        CLArgsParser.java
  Log:
  Fixed bug where options with Optional args could not have space between themselves and arg
  
  Renamed all variables to have m_ prefixed
  
  Revision  Changes    Path
  1.14      +73 -48    jakarta-avalon-excalibur/cli/src/java/org/apache/avalon/excalibur/cli/CLArgsParser.java
  
  Index: CLArgsParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/cli/src/java/org/apache/avalon/excalibur/cli/CLArgsParser.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- CLArgsParser.java	19 Mar 2002 11:12:48 -0000	1.13
  +++ CLArgsParser.java	19 Mar 2002 11:57:17 -0000	1.14
  @@ -55,12 +55,12 @@
       private String[] m_unparsedArgs = new String[]{};
   
       //variables used while parsing options.
  -    private char ch;
  -    private String[] args;
  -    private boolean isLong;
  -    private int argIndex;
  -    private int stringIndex;
  -    private int stringLength;
  +    private char m_ch;
  +    private String[] m_args;
  +    private boolean m_isLong;
  +    private int m_argIndex;
  +    private int m_stringIndex;
  +    private int m_stringLength;
   
       private int m_lastChar = INVALID;
   
  @@ -169,7 +169,7 @@
        */
       private final int getStateFor( final CLOptionDescriptor descriptor )
       {
  -        int flags = descriptor.getFlags();
  +        final int flags = descriptor.getFlags();
           if( ( flags & CLOptionDescriptor.ARGUMENTS_REQUIRED_2 ) ==
               CLOptionDescriptor.ARGUMENTS_REQUIRED_2 )
           {
  @@ -205,7 +205,7 @@
           m_optionDescriptors = optionDescriptors;
           m_control = control;
           m_options = new Vector();
  -        this.args = args;
  +        m_args = args;
   
           try
           {
  @@ -375,23 +375,23 @@
       private final void parse()
           throws ParseException
       {
  -        if( 0 == args.length )
  +        if( 0 == m_args.length )
           {
               return;
           }
   
  -        stringLength = args[ argIndex ].length();
  +        m_stringLength = m_args[ m_argIndex ].length();
   
           //ch = peekAtChar();
   
           while( true )
           {
  -            ch = peekAtChar();
  +            m_ch = peekAtChar();
   
               //System.out.println( "Pre State=" + m_state );
               //System.out.println( "Pre Char=" + (char)ch + "/" + (int)ch );
   
  -            if( argIndex >= args.length )
  +            if( m_argIndex >= m_args.length )
               {
                   break;
               }
  @@ -399,7 +399,7 @@
               if( null != m_control && m_control.isFinished( m_lastOptionId ) )
               {
                   //this may need mangling due to peeks
  -                m_unparsedArgs = subArray( args, argIndex, stringIndex );
  +                m_unparsedArgs = subArray( m_args, m_argIndex, m_stringIndex );
                   return;
               }
   
  @@ -410,7 +410,7 @@
               {
                   //if get to an arg barrier then return to normal mode
                   //else continue accumulating options
  -                if( 0 == ch )
  +                if( 0 == m_ch )
                   {
                       getChar(); //strip the null
                       m_state = STATE_NORMAL;
  @@ -427,9 +427,9 @@
               else if( STATE_NO_OPTIONS == m_state )
               {
                   //should never get to here when stringIndex != 0
  -                addOption( new CLOption( args[ argIndex++ ] ) );
  +                addOption( new CLOption( m_args[ m_argIndex++ ] ) );
               }
  -            else if( STATE_OPTIONAL_ARG == m_state && '-' == ch )
  +            else if( STATE_OPTIONAL_ARG == m_state && '-' == m_ch )
               {
                   m_state = STATE_NORMAL;
                   addOption( m_option );
  @@ -477,7 +477,7 @@
   
       private final String getOptionDescription( final CLOptionDescriptor descriptor )
       {
  -        if( isLong )
  +        if( m_isLong )
           {
               return "--" + descriptor.getName();
           }
  @@ -512,36 +512,38 @@
   
       private final char readChar()
       {
  -        if( stringIndex >= stringLength )
  +        if( m_stringIndex >= m_stringLength )
           {
  -            argIndex++;
  -            stringIndex = 0;
  +            m_argIndex++;
  +            m_stringIndex = 0;
   
  -            if( argIndex < args.length )
  +            if( m_argIndex < m_args.length )
               {
  -                stringLength = args[ argIndex ].length();
  +                m_stringLength = m_args[ m_argIndex ].length();
               }
               else
               {
  -                stringLength = 0;
  +                m_stringLength = 0;
               }
   
               return 0;
           }
   
  -        if( argIndex >= args.length )
  +        if( m_argIndex >= m_args.length )
  +        {
               return 0;
  +        }
   
  -        return args[ argIndex ].charAt( stringIndex++ );
  +        return m_args[ m_argIndex ].charAt( m_stringIndex++ );
       }
   
       private final Token nextToken( final char[] separators )
       {
  -        ch = getChar();
  +        m_ch = getChar();
   
  -        if( isSeparator( ch, separators ) )
  +        if( isSeparator( m_ch, separators ) )
           {
  -            ch = getChar();
  +            m_ch = getChar();
               return new Token( TOKEN_SEPARATOR, null );
           }
   
  @@ -549,9 +551,9 @@
   
           do
           {
  -            sb.append( ch );
  -            ch = getChar();
  -        } while( !isSeparator( ch, separators ) );
  +            sb.append( m_ch );
  +            m_ch = getChar();
  +        } while( !isSeparator( m_ch, separators ) );
   
           return new Token( TOKEN_STRING, sb.toString() );
       }
  @@ -597,10 +599,10 @@
       private final void parseShortOption()
           throws ParseException
       {
  -        ch = getChar();
  -        final CLOptionDescriptor descriptor = getDescriptorFor( ch );
  -        isLong = false;
  -        parseOption( descriptor, "-" + ch );
  +        m_ch = getChar();
  +        final CLOptionDescriptor descriptor = getDescriptorFor( m_ch );
  +        m_isLong = false;
  +        parseOption( descriptor, "-" + m_ch );
   
           if( STATE_NORMAL == m_state )
           {
  @@ -613,7 +615,7 @@
       {
           if( STATE_REQUIRE_ARG == m_state )
           {
  -            if( '=' == ch || 0 == ch )
  +            if( '=' == m_ch || 0 == m_ch )
               {
                   getChar();
               }
  @@ -626,7 +628,30 @@
           }
           else if( STATE_OPTIONAL_ARG == m_state )
           {
  -            if( '-' == ch || 0 == ch )
  +            if( 0 == m_ch )
  +            {
  +                m_lastChar = INVALID;
  +                m_ch = peekAtChar();
  +
  +                if( '-' == m_ch )
  +                {
  +                    addOption( m_option );
  +                    m_state = STATE_NORMAL;
  +                    return;
  +                }
  +
  +                //if the option with optional argument appears
  +                //at end of command line then make sure it
  +                //is added and finalized
  +                if( m_argIndex >= m_args.length )
  +                {
  +                    addOption( m_option );
  +                    m_state = STATE_NORMAL;
  +                    return;
  +                }
  +            }
  +
  +            if( '-' == m_ch )
               {
                   getChar(); //consume stray character
                   addOption( m_option );
  @@ -634,7 +659,7 @@
                   return;
               }
   
  -            if( '=' == ch )
  +            if( '=' == m_ch )
               {
                   getChar();
               }
  @@ -668,16 +693,16 @@
               {
                   final StringBuffer sb = new StringBuffer();
   
  -                ch = getChar();
  -                if( '-' == ch )
  +                m_ch = getChar();
  +                if( '-' == m_ch )
                   {
  -                    m_lastChar = ch;
  +                    m_lastChar = m_ch;
                   }
   
  -                while( !isSeparator( ch, ARG2_SEPARATORS ) )
  +                while( !isSeparator( m_ch, ARG2_SEPARATORS ) )
                   {
  -                    sb.append( ch );
  -                    ch = getChar();
  +                    sb.append( m_ch );
  +                    m_ch = getChar();
                   }
   
                   final String argument = sb.toString();
  @@ -698,7 +723,7 @@
       private final void parseNormal()
           throws ParseException
       {
  -        if( '-' != ch )
  +        if( '-' != m_ch )
           {
               //Parse the arguments that are not options
               final String argument = nextToken( NULL_SEPARATORS ).getValue();
  @@ -715,10 +740,10 @@
               }
               else
               {
  -                ch = peekAtChar();
  +                m_ch = peekAtChar();
   
                   //if it is a short option then parse it else ...
  -                if( '-' != ch )
  +                if( '-' != m_ch )
                   {
                       parseShortOption();
                   }
  @@ -738,7 +763,7 @@
                           //its a long option
                           final String optionName = nextToken( ARG_SEPARATORS ).getValue();
                           final CLOptionDescriptor descriptor = getDescriptorFor( optionName
);
  -                        isLong = true;
  +                        m_isLong = true;
                           parseOption( descriptor, "--" + optionName );
                       }
                   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message