Skip to content

Commit a56dfdd

Browse files
author
zendawg
committed
Any global configuration defined more than once will produce an exception; javadoc updated to clarify parsing rules, new test files added and tests updated accordingly.
1 parent 77a115c commit a56dfdd

File tree

7 files changed

+226
-21
lines changed

7 files changed

+226
-21
lines changed

src/main/java/org/apache/commons/cli/config/GlobalConfiguration.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
* least one {@link OptionConfiguration}.
2727
*
2828
* <p>
29-
* Lines beginning with &#35; are ignored.
30-
*
29+
* Lines beginning with &#35; are ignored. Each global configuration can only be
30+
* declared once; redeclaring a global option will produce an error.
3131
* <p>
3232
* Global configuration items are as follows - the global options beginning with
3333
* {@code HELP_} enable callers to print help linked to an option without any
@@ -370,20 +370,40 @@ else if (OptionsTypeEnum.LONG.getType().equals(data))
370370
private void parseHelp(final String line) throws ConfigurationException
371371
{
372372
String[] data = line.split("=");
373-
if (GlobalConfiguration.HELP_COMMAND_NAME.equals(data[0].trim()))
373+
if (HELP_COMMAND_NAME.equals(data[0].trim()))
374374
{
375+
if (helpCommandName != null)
376+
{
377+
throw new ConfigurationException(HELP_COMMAND_NAME
378+
+ " has already been defined.");
379+
}
375380
helpCommandName = data[1].trim();
376381
}
377382
else if (GlobalConfiguration.HELP_COMMAND_HEADER.equals(data[0].trim()))
378383
{
384+
if (helpCommandHeader != null)
385+
{
386+
throw new ConfigurationException(HELP_COMMAND_HEADER
387+
+ " has already been defined.");
388+
}
379389
helpCommandHeader = data[1].trim();
380390
}
381-
else if (GlobalConfiguration.HELP_COMMAND_FOOTER.equals(data[0].trim()))
391+
else if (HELP_COMMAND_FOOTER.equals(data[0].trim()))
382392
{
393+
if (helpCommandFooter != null)
394+
{
395+
throw new ConfigurationException(HELP_COMMAND_FOOTER
396+
+ " has already been defined.");
397+
}
383398
helpCommandFooter = data[1].trim();
384399
}
385-
else if (GlobalConfiguration.HELP_OPTION_NAME.equals(data[0].trim()))
400+
else if (HELP_OPTION_NAME.equals(data[0].trim()))
386401
{
402+
if (helpOptionName != null)
403+
{
404+
throw new ConfigurationException(HELP_OPTION_NAME
405+
+ " has already been defined.");
406+
}
387407
helpOptionName = data[1].trim();
388408
}
389409
else

src/main/java/org/apache/commons/cli/config/OptionConfiguration.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@
5959
* Lines beginning with &#35; are ignored.
6060
*
6161
* <p>
62+
* In all cases, lines may be escaped; escaped lines must end in a trailing
63+
* backslash character; lines to be appended must be indented using white space
64+
* (space character and/or tab characters). This is especially useful when
65+
* defining help descriptions. For example:
66+
*
67+
* <pre>
68+
* option.file.description=Supply the output file to write results to. If the \
69+
* file doesn't exist, it is created. If the file does exist, it is
70+
* appended to.
71+
* </pre>
72+
*
73+
* Note in the above example the spaces before the backslashes - this is so
74+
* sentences are not 'glued' together and provide spacing that is easy on the
75+
* eye to readers of the output.
76+
*
77+
* <p>
78+
* Options must be defined in groups - that is, once an option has been defined,
79+
* it cannot have other values set on it (like description, argument name etc.)
80+
* after another option has been defined. Options cannot have a value set on it
81+
* more than once - so you cannot define the description twice, for example. In
82+
* either case an exception will be thrown with the offending line and its line
83+
* number.
84+
*
85+
* <p>
6286
* For example, a configuration file named {@code opt.config} could be created
6387
* with the following option configuration (note that typically, creators of
6488
* configurations will likely have the {@code name} and the long option text
@@ -94,22 +118,6 @@
94118
* {@link OptionListener#option(java.lang.String, java.lang.Object)} and would
95119
* receive an update with the option {@code file} given a value of
96120
* {@code datafile.txt}.
97-
*
98-
* <p>
99-
* In all cases, lines may be escaped; escaped lines must end in a trailing
100-
* backslash character; lines to be appended must be indented using white space
101-
* (space character and/or tab characters). This is especially useful when
102-
* defining help descriptions. For example:
103-
*
104-
* <pre>
105-
* option.file.description=Supply the output file to write results to. If the \
106-
* file doesn't exist, it is created. If the file does exist, it is
107-
* appended to.
108-
* </pre>
109-
*
110-
* Note in the above example the spaces before the backslashes - this is so
111-
* sentences are not 'glued' together and provide spacing that is easy on the
112-
* eye to readers of the output.
113121
*/
114122
public class OptionConfiguration
115123
{

src/test/java/org/apache/commons/cli/config/ConfigurationParserTest.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121
import java.util.regex.Matcher;
2222
import java.util.regex.Pattern;
23+
import static org.apache.commons.cli.config.GlobalConfiguration.HELP_COMMAND_NAME;
2324
import static org.apache.commons.cli.config.GlobalConfiguration.OPTION_TYPE;
2425
import org.junit.After;
2526
import org.junit.AfterClass;
@@ -605,6 +606,102 @@ public void testParseInputStreamOptionsBadOrdering() throws Exception
605606
}
606607
}
607608

609+
/**
610+
* Test that an option that is redefined later in the file throws an
611+
* exception.
612+
*/
613+
@Test
614+
public void testParseInputStreamHelpCommandDefinedTwice() throws Exception
615+
{
616+
ConfigurationParser configParser = new ConfigurationParser();
617+
InputStream is = ConfigurationParserTest.class.getResourceAsStream(
618+
"/config/config_027_help_command_defined_twice.conf");
619+
try
620+
{
621+
configParser.parse(is, "UTF-8");
622+
fail("Expected exception.");
623+
}
624+
catch (ConfigurationException ex)
625+
{
626+
System.out.println(ex.getMessage());
627+
assertTrue(ex.getMessage().contains(
628+
GlobalConfiguration.HELP_COMMAND_NAME
629+
+ " has already been defined."));
630+
}
631+
}
632+
633+
/**
634+
* Test that an option that is redefined later in the file throws an
635+
* exception.
636+
*/
637+
@Test
638+
public void testParseInputStreamHelpOptionNameDefinedTwice() throws Exception
639+
{
640+
ConfigurationParser configParser = new ConfigurationParser();
641+
InputStream is = ConfigurationParserTest.class.getResourceAsStream(
642+
"/config/config_028_help_option_defined_twice.conf");
643+
try
644+
{
645+
configParser.parse(is, "UTF-8");
646+
fail("Expected exception.");
647+
}
648+
catch (ConfigurationException ex)
649+
{
650+
System.out.println(ex.getMessage());
651+
assertTrue(ex.getMessage().contains(
652+
GlobalConfiguration.HELP_OPTION_NAME
653+
+ " has already been defined."));
654+
}
655+
}
656+
657+
/**
658+
* Test that an option that is redefined later in the file throws an
659+
* exception.
660+
*/
661+
@Test
662+
public void testParseInputStreamHelpFooterDefinedTwice() throws Exception
663+
{
664+
ConfigurationParser configParser = new ConfigurationParser();
665+
InputStream is = ConfigurationParserTest.class.getResourceAsStream(
666+
"/config/config_029_help_footer_defined_twice.conf");
667+
try
668+
{
669+
configParser.parse(is, "UTF-8");
670+
fail("Expected exception.");
671+
}
672+
catch (ConfigurationException ex)
673+
{
674+
System.out.println(ex.getMessage());
675+
assertTrue(ex.getMessage().contains(
676+
GlobalConfiguration.HELP_COMMAND_FOOTER
677+
+ " has already been defined."));
678+
}
679+
}
680+
681+
/**
682+
* Test that an option that is redefined later in the file throws an
683+
* exception.
684+
*/
685+
@Test
686+
public void testParseInputStreamHelpHeaderDefinedTwice() throws Exception
687+
{
688+
ConfigurationParser configParser = new ConfigurationParser();
689+
InputStream is = ConfigurationParserTest.class.getResourceAsStream(
690+
"/config/config_030_help_header_defined_twice.conf");
691+
try
692+
{
693+
configParser.parse(is, "UTF-8");
694+
fail("Expected exception.");
695+
}
696+
catch (ConfigurationException ex)
697+
{
698+
System.out.println(ex.getMessage());
699+
assertTrue(ex.getMessage().contains(
700+
GlobalConfiguration.HELP_COMMAND_HEADER
701+
+ " has already been defined."));
702+
}
703+
}
704+
608705
/**
609706
* Test of addOptionListener method, of class ConfigurationParser.
610707
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# Error, command name defined twice:
19+
HELP_COMMAND_NAME=foo_command
20+
HELP_COMMAND_NAME=foo_command
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# Error, command name defined twice:
19+
HELP_OPTION_NAME=showHelp
20+
HELP_OPTION_NAME=showHelp
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# Error, command name defined twice:
19+
HELP_COMMAND_FOOTER=Help footer
20+
HELP_COMMAND_FOOTER=Help footer
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# Error, command name defined twice:
19+
HELP_COMMAND_HEADER=Help header
20+
HELP_COMMAND_HEADER=Help header

0 commit comments

Comments
 (0)