[config] CLIConfiguration?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[config] CLIConfiguration?

Poppe, Troy

Curious if anyone has see, or has any use for, a Configuration that extracts
properties from commons-cli?

There would need to be a way of mapping the commons-cli Options to a
Configuration property key...

This would allow for having global, local, user, and command line all composed
into a single Configuration...

Any thoughts?

Troy


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [config] CLIConfiguration?

Emmanuel Bourg-3
Hi Troy, a bridge to commons-cli would be a nice addition, any
suggestion for implementing it is welcome. You may be also interested in
  the command line configuration submited in Bugzilla, it's not very
sophisticated but it works fine for simple cases.

http://issues.apache.org/bugzilla/show_bug.cgi?id=34165

Emmanuel Bourg


Poppe, Troy wrote:

> Curious if anyone has see, or has any use for, a Configuration that extracts
> properties from commons-cli?
>
> There would need to be a way of mapping the commons-cli Options to a
> Configuration property key...
>
> This would allow for having global, local, user, and command line all composed
> into a single Configuration...
>
> Any thoughts?
>
> Troy

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [cli][config] CLIConfiguration?

Rob Oxspring
In reply to this post by Poppe, Troy
Hi,

I did write some adapters a month or two back that allow integration
between CLI2 and Configuration.  I had intended on getting CLI 2.0 out
of the door and then adding the new classes so that they could undergo
some more testing - then release them as part of CLI 2.1.  Since CLI 2.0
is being delayed again while we add documentation, it could end up being
added ahead of a 2.0 release if someone can confirm that they work as
expected.

The two adapter classes that the patch provides are a
CommandLineConfiguration that allows a parsed command line to be used as
a Configuration implementation and a ConfigurationCommandLine that
allows command line defaults to be read from a Configuration instance.

Let me know how you get on if you try the patch out!

Rob


Poppe, Troy wrote:

> Curious if anyone has see, or has any use for, a Configuration that extracts
> properties from commons-cli?
>
> There would need to be a way of mapping the commons-cli Options to a
> Configuration property key...
>
> This would allow for having global, local, user, and command line all composed
> into a single Configuration...
>
> Any thoughts?
>
> Troy
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

Index: project.xml
===================================================================
--- project.xml (revision 190841)
+++ project.xml (working copy)
@@ -131,6 +131,16 @@
     </dependency>
 
     <dependency>
+      <id>commons-configuration</id>
+      <version>1.0</version>
+    </dependency>
+
+    <dependency>
+      <id>commons-collections</id>
+      <version>3.1</version>
+    </dependency>
+
+    <dependency>
       <id>junit</id>
       <version>3.8.1</version>
     </dependency>
Index: src/test/org/apache/commons/cli2/commandline/ConfigurationCommandLineTest.java
===================================================================
--- src/test/org/apache/commons/cli2/commandline/ConfigurationCommandLineTest.java (revision 0)
+++ src/test/org/apache/commons/cli2/commandline/ConfigurationCommandLineTest.java (revision 0)
@@ -0,0 +1,47 @@
+/**
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.cli2.commandline;
+
+import java.util.Arrays;
+
+import org.apache.commons.cli2.CommandLine;
+import org.apache.commons.cli2.CommandLineTestCase;
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.configuration.Configuration;
+
+/**
+ * @author Rob Oxspring
+ */
+public class ConfigurationCommandLineTest extends CommandLineTestCase {
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.CommandLineTest#createCommandLine()
+ */
+ protected CommandLine createCommandLine() {
+ final Configuration config = new BaseConfiguration();
+ config.addProperty("--present","present value");
+ config.addProperty("--alsopresent","present value");
+ config.addProperty("--multiple",Arrays.asList(new String[]{"value 1", "value 2" , "value 3"}));
+ config.addProperty("--bool",Boolean.TRUE);
+ config.setProperty("present","present property");
+
+ return new ConfigurationCommandLine(root,config);
+ }
+
+ public void testToMakeEclipseSpotTheTestCase(){
+ // nothing to test
+ }
+}
Index: src/java/org/apache/commons/cli2/commandline/ConfigurationCommandLine.java
===================================================================
--- src/java/org/apache/commons/cli2/commandline/ConfigurationCommandLine.java (revision 0)
+++ src/java/org/apache/commons/cli2/commandline/ConfigurationCommandLine.java (revision 0)
@@ -0,0 +1,119 @@
+/**
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.cli2.commandline;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.cli2.Option;
+import org.apache.commons.configuration.Configuration;
+
+/**
+ * @author roxspring
+ */
+public class ConfigurationCommandLine extends CommandLineImpl {
+
+ private final Option root;
+ private final Configuration config;
+
+ public ConfigurationCommandLine(final Option root, final Configuration config) {
+ this.root = root;
+ this.config = config;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.CommandLine#hasOption(org.apache.commons.cli2.Option)
+ */
+ public boolean hasOption(Option option) {
+ if(option==null){
+ return false;
+ }
+        return config.containsKey(option.getPreferredName());
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.CommandLine#getOption(java.lang.String)
+ */
+ public Option getOption(String trigger) {
+ return root.findOption(trigger);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.CommandLine#getValues(org.apache.commons.cli2.Option, java.util.List)
+ */
+ public List getValues(Option option, List defaultValues) {
+ return config.getList(option.getPreferredName(), defaultValues);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.CommandLine#getSwitch(org.apache.commons.cli2.Option, java.lang.Boolean)
+ */
+ public Boolean getSwitch(Option option, Boolean defaultValue) {
+ return config.getBoolean(option.getPreferredName(), defaultValue);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.CommandLine#getProperty(java.lang.String, java.lang.String)
+ */
+ public String getProperty(String property, String defaultValue) {
+ return config.getString(property, defaultValue);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.CommandLine#getProperties()
+ */
+ public Set getProperties() {
+ final HashSet properties = new HashSet();
+ final Iterator keys = config.getKeys();
+ while(keys.hasNext()) {
+ properties.add(keys.next());
+ }
+ return properties;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.CommandLine#getOptions()
+ */
+ public List getOptions() {
+ final List options = new ArrayList();
+ final Iterator keys = config.getKeys();
+ while(keys.hasNext()){
+ final String trigger = (String)keys.next();
+ final Option option = root.findOption(trigger);
+ if(option!=null){
+ options.add(option);
+ }
+ }
+ return Collections.unmodifiableList(options);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.cli2.CommandLine#getOptionTriggers()
+ */
+ public Set getOptionTriggers() {
+ final Set triggers = new HashSet();
+ final Iterator options = getOptions().iterator();
+ while(options.hasNext()){
+ final Option option = (Option)options.next();
+ triggers.addAll(option.getTriggers());
+ }
+ return Collections.unmodifiableSet(triggers);
+ }
+}
Index: src/java/org/apache/commons/cli2/commandline/CommandLineConfiguration.java
===================================================================
--- src/java/org/apache/commons/cli2/commandline/CommandLineConfiguration.java (revision 0)
+++ src/java/org/apache/commons/cli2/commandline/CommandLineConfiguration.java (revision 0)
@@ -0,0 +1,75 @@
+/**
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed 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.cli2.commandline;
+
+import java.util.Iterator;
+
+import org.apache.commons.cli2.CommandLine;
+import org.apache.commons.configuration.AbstractConfiguration;
+
+/**
+ * @author roxspring
+ */
+public class CommandLineConfiguration extends AbstractConfiguration {
+
+ private final CommandLine cmd;
+
+ public CommandLineConfiguration(final CommandLine cmd) {
+ this.cmd = cmd;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.configuration.AbstractConfiguration#getPropertyDirect(java.lang.String)
+ */
+ protected Object getPropertyDirect(String key) {
+ return cmd.getValue(key);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.configuration.AbstractConfiguration#addPropertyDirect(java.lang.String, java.lang.Object)
+ */
+ protected void addPropertyDirect(String key, Object value) {
+ throw new UnsupportedOperationException("CommandLineConfiguration is currently read only");
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.configuration.AbstractConfiguration#isEmpty()
+ */
+ public boolean isEmpty() {
+ return cmd.getOptions().isEmpty();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.configuration.AbstractConfiguration#containsKey(java.lang.String)
+ */
+ public boolean containsKey(String key) {
+ return cmd.hasOption(key);
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.configuration.AbstractConfiguration#clearProperty(java.lang.String)
+ */
+ public void clearProperty(String arg0) {
+ throw new UnsupportedOperationException("CommandLineConfiguration is currently read only");
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.commons.configuration.AbstractConfiguration#getKeys()
+ */
+ public Iterator getKeys() {
+ return cmd.getOptionTriggers().iterator();
+ }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]