Skip to content

Commit 3c221ef

Browse files
committed
Fix long optional args to require "=" as per documentation
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/branches/avalon-implementation@539978 13f79535-47bb-0310-9956-ffa450edef68
1 parent 01de7d4 commit 3c221ef

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/java/org/apache/commons/cli/avalon/CLArgsParser.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,6 @@ else if( STATE_NO_OPTIONS == m_state )
450450
//should never get to here when stringIndex != 0
451451
addOption( new CLOption( m_args[m_argIndex++] ) );
452452
}
453-
else if( STATE_OPTIONAL_ARG == m_state && m_isLong && m_ch != 0)
454-
{
455-
m_state = STATE_NORMAL;
456-
addOption( m_option );
457-
}
458453
else
459454
{
460455
parseArguments();
@@ -560,12 +555,15 @@ private final char readChar()
560555
return m_args[m_argIndex].charAt( m_stringIndex++ );
561556
}
562557

558+
private char m_tokesep; // Keep track of token separator
559+
563560
private final Token nextToken( final char[] separators )
564561
{
565562
m_ch = getChar();
566563

567564
if( isSeparator( m_ch, separators ) )
568565
{
566+
m_tokesep = m_ch;
569567
m_ch = getChar();
570568
return new Token( TOKEN_SEPARATOR, null );
571569
}
@@ -579,6 +577,7 @@ private final Token nextToken( final char[] separators )
579577
}
580578
while( !isSeparator( m_ch, separators ) );
581579

580+
m_tokesep = m_ch;
582581
return new Token( TOKEN_STRING, sb.toString() );
583582
}
584583

@@ -660,6 +659,13 @@ else if( STATE_OPTIONAL_ARG == m_state )
660659
return;
661660
}
662661

662+
if (m_isLong && '=' != m_tokesep) // Long optional arg must have = as separator
663+
{
664+
addOption(m_option);
665+
m_state = STATE_NORMAL;
666+
return;
667+
}
668+
663669
if( '=' == m_ch ) // $NON-NLS-1$
664670
{
665671
getChar();

src/test/org/apache/commons/cli/avalon/ClutilTestCase.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,30 @@ public void testOptionalArgLong()
196196
assertEquals( option2.getArgument( 0 ), null );
197197
}
198198

199+
public void testOptionalArgLongEquals() {
200+
final CLOptionDescriptor[] options = new CLOptionDescriptor[] { ALL, TAINT };
201+
202+
// Check that optional args work woth long options
203+
final String[] args = new String[] { "--taint=param", "-a" };
204+
205+
final CLArgsParser parser = new CLArgsParser(args, options);
206+
207+
assertNull(parser.getErrorString(), parser.getErrorString());
208+
209+
final List clOptions = parser.getArguments();
210+
final int size = clOptions.size();
211+
212+
assertEquals("Option count", 2, size);
213+
214+
final CLOption option0 = (CLOption) clOptions.get(0);
215+
assertEquals("Option Code: " + option0.getDescriptor().getId(), TAINT_OPT, option0.getDescriptor().getId());
216+
assertEquals("Option Arg: " + option0.getArgument(0), "param", option0.getArgument(0));
217+
218+
final CLOption option2 = (CLOption) clOptions.get(1);
219+
assertEquals(option2.getDescriptor().getId(), ALL_OPT);
220+
assertEquals(option2.getArgument(0), null);
221+
}
222+
199223
public void testShortOptArgUnenteredBeforeOtherOpt()
200224
{
201225
final CLOptionDescriptor[] options = new CLOptionDescriptor[]

0 commit comments

Comments
 (0)