diff --git a/.gitignore b/.gitignore index c8d9cd68b..a7e149555 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ site-content /.classpath /.project /.settings/ +/nb-configuration.xml diff --git a/src/main/java/org/apache/commons/cli/config/CommandLineConfiguration.java b/src/main/java/org/apache/commons/cli/config/CommandLineConfiguration.java new file mode 100644 index 000000000..064719d22 --- /dev/null +++ b/src/main/java/org/apache/commons/cli/config/CommandLineConfiguration.java @@ -0,0 +1,271 @@ +/** + * 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. + */ +package org.apache.commons.cli.config; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; + +/** + * Main entry point to the configuration library. + * + *

+ * Combines processing a command line configuration file and passes the options + * to the command line parser. Listeners should register themselves with this + * instance in order to be notified of options as they are encountered. + * + *

+ * Configuration comes in two forms, each in the same file - global or + * option configurations. {@link GlobalConfiguration} enables the + * definition of command line help properties (command name, header, footer + * etc.) as well as what combination of options to use - short, long or both. + * {@link OptionConfiguration}s enable users to define options that are + * programmatically built into {@link Options} and dealt with under the hood. + * All that is required is that an {@link OptionListener} is added as a listener + * and contains the code to execute when different options are passed in. + * + *

+ * By default, no global options are required and only one option configuration + * is required minimum to get going. Regardless, all global options must be + * defined before any standard options, otherwise an error will be thrown. Each + * option configuration takes the form of {@code option.[optionName].[property]} + * where each {@code property} is a pre-defined configuration proeprty defined + * in {@link OptionConfiguration}. + * + *

+ * An example of using the command line configuration - see + * {@link OptionListener} for how a listener would deal with the updates from + * the processing of command line arguments, and {@link OptionConfiguration} for + * an example configuration that matches this example: + * + *

+ * {@code
+ * MyAppListener listener = new MyAppListener();
+ * InputStream is = new FileInputStream(new File("opt.config"));
+ * CommandLineConfiguration cliConfig = new CommandLineConfiguration();
+ * cliConfig.addOptionListener(listener);
+ * // args[] from the public static void main(String[] args) call:
+ * cliConfig.process(is, args);
+ * Application application = new Application();
+ * if (listener.file != null && listener.text != null)
+ * {
+ *     application.write(listener.file, listener.text, listener.overwrite);
+ * }
+ * else
+ * {
+ *     System.err.println("File and text must be supplied.");
+ *     System.exit(1);
+ * }
+ * }
+ * 
+ */ +public class CommandLineConfiguration +{ + + /** + * Listeners to be notified of updates. + */ + private final List listeners = new ArrayList<>(); + + /** + * Global configuration parsed by this CLI configuration. + */ + private GlobalConfiguration globalConfig; + + /** + * Options parsed from the command line parser. + */ + private List