Skip to content

Commit

Permalink
Any global configuration defined more than once will produce an excep…
Browse files Browse the repository at this point in the history
…tion; javadoc updated to clarify parsing rules, new test files added and tests updated accordingly.
  • Loading branch information
zendawg committed Oct 17, 2019
1 parent 77a115c commit a56dfdd
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
* least one {@link OptionConfiguration}.
*
* <p>
* Lines beginning with &#35; are ignored.
*
* Lines beginning with &#35; are ignored. Each global configuration can only be
* declared once; redeclaring a global option will produce an error.
* <p>
* Global configuration items are as follows - the global options beginning with
* {@code HELP_} enable callers to print help linked to an option without any
Expand Down Expand Up @@ -370,20 +370,40 @@ else if (OptionsTypeEnum.LONG.getType().equals(data))
private void parseHelp(final String line) throws ConfigurationException
{
String[] data = line.split("=");
if (GlobalConfiguration.HELP_COMMAND_NAME.equals(data[0].trim()))
if (HELP_COMMAND_NAME.equals(data[0].trim()))
{
if (helpCommandName != null)
{
throw new ConfigurationException(HELP_COMMAND_NAME
+ " has already been defined.");
}
helpCommandName = data[1].trim();
}
else if (GlobalConfiguration.HELP_COMMAND_HEADER.equals(data[0].trim()))
{
if (helpCommandHeader != null)
{
throw new ConfigurationException(HELP_COMMAND_HEADER
+ " has already been defined.");
}
helpCommandHeader = data[1].trim();
}
else if (GlobalConfiguration.HELP_COMMAND_FOOTER.equals(data[0].trim()))
else if (HELP_COMMAND_FOOTER.equals(data[0].trim()))
{
if (helpCommandFooter != null)
{
throw new ConfigurationException(HELP_COMMAND_FOOTER
+ " has already been defined.");
}
helpCommandFooter = data[1].trim();
}
else if (GlobalConfiguration.HELP_OPTION_NAME.equals(data[0].trim()))
else if (HELP_OPTION_NAME.equals(data[0].trim()))
{
if (helpOptionName != null)
{
throw new ConfigurationException(HELP_OPTION_NAME
+ " has already been defined.");
}
helpOptionName = data[1].trim();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@
* Lines beginning with &#35; are ignored.
*
* <p>
* In all cases, lines may be escaped; escaped lines must end in a trailing
* backslash character; lines to be appended must be indented using white space
* (space character and/or tab characters). This is especially useful when
* defining help descriptions. For example:
*
* <pre>
* option.file.description=Supply the output file to write results to. If the \
* file doesn't exist, it is created. If the file does exist, it is
* appended to.
* </pre>
*
* Note in the above example the spaces before the backslashes - this is so
* sentences are not 'glued' together and provide spacing that is easy on the
* eye to readers of the output.
*
* <p>
* Options must be defined in groups - that is, once an option has been defined,
* it cannot have other values set on it (like description, argument name etc.)
* after another option has been defined. Options cannot have a value set on it
* more than once - so you cannot define the description twice, for example. In
* either case an exception will be thrown with the offending line and its line
* number.
*
* <p>
* For example, a configuration file named {@code opt.config} could be created
* with the following option configuration (note that typically, creators of
* configurations will likely have the {@code name} and the long option text
Expand Down Expand Up @@ -94,22 +118,6 @@
* {@link OptionListener#option(java.lang.String, java.lang.Object)} and would
* receive an update with the option {@code file} given a value of
* {@code datafile.txt}.
*
* <p>
* In all cases, lines may be escaped; escaped lines must end in a trailing
* backslash character; lines to be appended must be indented using white space
* (space character and/or tab characters). This is especially useful when
* defining help descriptions. For example:
*
* <pre>
* option.file.description=Supply the output file to write results to. If the \
* file doesn't exist, it is created. If the file does exist, it is
* appended to.
* </pre>
*
* Note in the above example the spaces before the backslashes - this is so
* sentences are not 'glued' together and provide spacing that is easy on the
* eye to readers of the output.
*/
public class OptionConfiguration
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.apache.commons.cli.config.GlobalConfiguration.HELP_COMMAND_NAME;
import static org.apache.commons.cli.config.GlobalConfiguration.OPTION_TYPE;
import org.junit.After;
import org.junit.AfterClass;
Expand Down Expand Up @@ -605,6 +606,102 @@ public void testParseInputStreamOptionsBadOrdering() throws Exception
}
}

/**
* Test that an option that is redefined later in the file throws an
* exception.
*/
@Test
public void testParseInputStreamHelpCommandDefinedTwice() throws Exception
{
ConfigurationParser configParser = new ConfigurationParser();
InputStream is = ConfigurationParserTest.class.getResourceAsStream(
"/config/config_027_help_command_defined_twice.conf");
try
{
configParser.parse(is, "UTF-8");
fail("Expected exception.");
}
catch (ConfigurationException ex)
{
System.out.println(ex.getMessage());
assertTrue(ex.getMessage().contains(
GlobalConfiguration.HELP_COMMAND_NAME
+ " has already been defined."));
}
}

/**
* Test that an option that is redefined later in the file throws an
* exception.
*/
@Test
public void testParseInputStreamHelpOptionNameDefinedTwice() throws Exception
{
ConfigurationParser configParser = new ConfigurationParser();
InputStream is = ConfigurationParserTest.class.getResourceAsStream(
"/config/config_028_help_option_defined_twice.conf");
try
{
configParser.parse(is, "UTF-8");
fail("Expected exception.");
}
catch (ConfigurationException ex)
{
System.out.println(ex.getMessage());
assertTrue(ex.getMessage().contains(
GlobalConfiguration.HELP_OPTION_NAME
+ " has already been defined."));
}
}

/**
* Test that an option that is redefined later in the file throws an
* exception.
*/
@Test
public void testParseInputStreamHelpFooterDefinedTwice() throws Exception
{
ConfigurationParser configParser = new ConfigurationParser();
InputStream is = ConfigurationParserTest.class.getResourceAsStream(
"/config/config_029_help_footer_defined_twice.conf");
try
{
configParser.parse(is, "UTF-8");
fail("Expected exception.");
}
catch (ConfigurationException ex)
{
System.out.println(ex.getMessage());
assertTrue(ex.getMessage().contains(
GlobalConfiguration.HELP_COMMAND_FOOTER
+ " has already been defined."));
}
}

/**
* Test that an option that is redefined later in the file throws an
* exception.
*/
@Test
public void testParseInputStreamHelpHeaderDefinedTwice() throws Exception
{
ConfigurationParser configParser = new ConfigurationParser();
InputStream is = ConfigurationParserTest.class.getResourceAsStream(
"/config/config_030_help_header_defined_twice.conf");
try
{
configParser.parse(is, "UTF-8");
fail("Expected exception.");
}
catch (ConfigurationException ex)
{
System.out.println(ex.getMessage());
assertTrue(ex.getMessage().contains(
GlobalConfiguration.HELP_COMMAND_HEADER
+ " has already been defined."));
}
}

/**
* Test of addOptionListener method, of class ConfigurationParser.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Error, command name defined twice:
HELP_COMMAND_NAME=foo_command
HELP_COMMAND_NAME=foo_command
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Error, command name defined twice:
HELP_OPTION_NAME=showHelp
HELP_OPTION_NAME=showHelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Error, command name defined twice:
HELP_COMMAND_FOOTER=Help footer
HELP_COMMAND_FOOTER=Help footer
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Error, command name defined twice:
HELP_COMMAND_HEADER=Help header
HELP_COMMAND_HEADER=Help header

0 comments on commit a56dfdd

Please sign in to comment.