1 | private final void parseArguments() throws ParseException {
| | 1 | private final Token nextToken(final char[] separators) {
|
2 | if (STATE_REQUIRE_ARG == m_state) {
| | 2 | m_ch = getChar();
|
3 | if ('=' == m_ch || 0 == m_ch) {
| | 3 |
|
4 | getChar();
| | 4 | if (isSeparator(m_ch, separators)) {
|
5 | }
| | 5 | m_tokesep=m_ch;
|
6 |
| | 6 | m_ch = getChar();
|
7 | final Token token = nextToken(NULL_SEPARATORS);
| | 7 | return new Token(TOKEN_SEPARATOR, null);
|
8 | m_option.addArgument(token.getValue());
| | 8 | }
|
9 |
| | 9 |
|
10 | addOption(m_option);
| | 10 | final StringBuilder sb = new StringBuilder();
|
11 | m_state = STATE_NORMAL;
| | 11 |
|
12 | } else if (STATE_OPTIONAL_ARG == m_state) {
| | 12 | do {
|
13 | if ('-' == m_ch || 0 == m_ch) {
| | 13 | sb.append(m_ch);
|
14 | getChar(); // consume stray character
| | 14 | m_ch = getChar();
|
15 | addOption(m_option);
| | 15 | } while (!isSeparator(m_ch, separators));
|
16 | m_state = STATE_NORMAL;
| | 16 |
|
17 | return;
| | 17 | m_tokesep=m_ch;
|
18 | }
| | 18 | return new Token(TOKEN_STRING, sb.toString());
|
19 |
| | 19 | } |
20 | if (m_isLong && '=' != m_tokesep){ // Long optional arg must have = as separator
| | | |
21 | addOption(m_option);
| | | |
22 | m_state = STATE_NORMAL;
| | | |
23 | return;
| | | |
24 | }
| | | |
25 |
| | | |
26 | if ('=' == m_ch) {
| | | |
27 | getChar();
| | | |
28 | }
| | | |
29 |
| | | |
30 | final Token token = nextToken(NULL_SEPARATORS);
| | | |
31 | m_option.addArgument(token.getValue());
| | | |
32 |
| | | |
33 | addOption(m_option);
| | | |
34 | m_state = STATE_NORMAL;
| | | |
35 | } else if (STATE_REQUIRE_2ARGS == m_state) {
| | | |
36 | if (0 == m_option.getArgumentCount()) {
| | | |
37 | /*
| | | |
38 | * Fix bug: -D arg1=arg2 was causing parse error; however
| | | |
39 | * --define arg1=arg2 is OK This seems to be because the parser
| | | |
40 | * skips the terminator for the long options, but was not doing
| | | |
41 | * so for the short options.
| | | |
42 | */
| | | |
43 | if (!m_isLong) {
| | | |
44 | if (0 == peekAtChar()) {
| | | |
45 | getChar();
| | | |
46 | }
| | | |
47 | }
| | | |
48 | final Token token = nextToken(ARG_SEPARATORS);
| | | |
49 |
| | | |
50 | if (TOKEN_SEPARATOR == token.getType()) {
| | | |
51 | final CLOptionDescriptor descriptor = getDescriptorFor(m_option.getDescriptor().getId());
| | | |
52 | final String message = "Unable to parse first argument for option "
| | | |
53 | + getOptionDescription(descriptor);
| | | |
54 | throw new ParseException(message, 0);
| | | |
55 | } else {
| | | |
56 | m_option.addArgument(token.getValue());
| | | |
57 | }
| | | |
58 | // Are we about to start a new option?
| | | |
59 | if (0 == m_ch && '-' == peekAtChar()) {
| | | |
60 | // Yes, so the second argument is missing
| | | |
61 | m_option.addArgument("");
| | | |
62 | m_options.addElement(m_option);
| | | |
63 | m_state = STATE_NORMAL;
| | | |
64 | }
| | | |
65 | } else // 2nd argument
| | | |
66 | {
| | | |
67 | final StringBuilder sb = new StringBuilder();
| | | |
68 |
| | | |
69 | m_ch = getChar();
| | | |
70 | while (!isSeparator(m_ch, NULL_SEPARATORS)) {
| | | |
71 | sb.append(m_ch);
| | | |
72 | m_ch = getChar();
| | | |
73 | }
| | | |
74 |
| | | |
75 | final String argument = sb.toString();
| | | |
76 |
| | | |
77 | // System.out.println( "Arguement:" + argument );
| | | |
78 |
| | | |
79 | m_option.addArgument(argument);
| | | |
80 | addOption(m_option);
| | | |
81 | m_option = null;
| | | |
82 | m_state = STATE_NORMAL;
| | | |
83 | }
| | | |
84 | }
| | | |
85 | } | | | |