Trial implementation of CLISetup that use commons-cli.

"Eung-ju Park" <[email protected]>
Newsgroups gmane.comp.jakarta.avalon.phoenix.devel
Message-ID <DAV34B9Dj17mqPU1r6F0002e23e__12734.8020709634$1047430177@hotmail.com>
excalibur-cli goes into compatibility mode.
Here is trial implementation of CLISetup. It has some problems in help
message(omit some options).

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
cli-patch.txt (text/plain, 13.3 KB)
Index: CLISetup.java
===================================================================
RCS file: /home/cvs/avalon-phoenix/src/java/org/apache/avalon/phoenix/frontends/CLISetup.java,v
retrieving revision 1.21
diff -u -r1.21 CLISetup.java
--- CLISetup.java	22 Feb 2003 05:34:46 -0000	1.21
+++ CLISetup.java	12 Mar 2003 00:46:51 -0000
@@ -7,21 +7,23 @@
  */
 package org.apache.avalon.phoenix.frontends;
 
-import java.util.List;
-import org.apache.avalon.excalibur.cli.CLArgsParser;
-import org.apache.avalon.excalibur.cli.CLOption;
-import org.apache.avalon.excalibur.cli.CLOptionDescriptor;
-import org.apache.avalon.excalibur.cli.CLUtil;
 import org.apache.avalon.excalibur.i18n.ResourceManager;
 import org.apache.avalon.excalibur.i18n.Resources;
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.avalon.phoenix.interfaces.SystemManager;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
 
 /**
  * The class prepare parameters based on input options.
  *
- * @author <a href="mailto:peter at apache.org">Peter Donald</a>
- * @author <a href="[email protected]">Leo Simons</a>
+ * @author Eung-ju Park
  */
 class CLISetup
 {
@@ -31,28 +33,28 @@
     private static final String MANAGER_IMPL =
         "org.apache.avalon.phoenix.components.manager.DefaultManager";
 
-    private static final int DEBUG_LOG_OPT = 'd';
+    private static final String DEBUG_LOG_OPT = "d";
 
-    private static final int HELP_OPT = 'h';
+    private static final String HELP_OPT = "h";
 
-    private static final int LOG_FILE_OPT = 'l';
+    private static final String LOG_FILE_OPT = "l";
 
-    private static final int APPS_PATH_OPT = 'a';
+    private static final String APPS_PATH_OPT = "a";
 
-    private static final int PERSISTENT_OPT = 'p';
+    private static final String PERSISTENT_OPT = "p";
 
-    private static final int CONFIGFILE_OPT = 'f';
+    private static final String CONFIGFILE_OPT = "f";
 
-    private static final int REMOTE_MANAGER_OPT = 1;
+    private static final String REMOTE_MANAGER_OPT = "remote-manager";
 
-    private static final int DISABLE_HOOK_OPT = 2;
+    private static final String DISABLE_HOOK_OPT = "disable-hook";
 
-    private static final int APPLICATION_OPT = 3;
+    private static final String APPLICATION_OPT = "application";
 
-    ///Parameters created by parsing CLI options
+    /** Parameters created by parsing CLI options */
     private final Parameters m_parameters = new Parameters();
 
-    ///Command used to execute program
+    /** Command used to execute program */
     private final String m_command;
 
     public CLISetup( final String command )
@@ -60,150 +62,116 @@
         m_command = command;
     }
 
-    /**
-     * Display usage report.
-     */
-    private void usage( final CLOptionDescriptor[] options )
+    public Parameters getParameters()
     {
-        System.err.println( m_command );
-        System.err.println( "\t" + REZ.getString( "cli.desc.available.header" ) );
-        System.err.println( CLUtil.describeOptions( options ) );
+        return m_parameters;
     }
 
     /**
-     * Initialise the options for command line parser.
+     * Display usage report.
      */
-    private CLOptionDescriptor[] createCLOptions()
+    private void usage( final Options options )
     {
-        final CLOptionDescriptor options[] = new CLOptionDescriptor[ 9 ];
-        options[ 0 ] =
-            new CLOptionDescriptor( "help",
-                                    CLOptionDescriptor.ARGUMENT_DISALLOWED,
-                                    HELP_OPT,
-                                    REZ.getString( "cli.opt.help.desc" ) );
-        options[ 1 ] =
-            new CLOptionDescriptor( "log-file",
-                                    CLOptionDescriptor.ARGUMENT_REQUIRED,
-                                    LOG_FILE_OPT,
-                                    REZ.getString( "cli.opt.log-file.desc" ) );
-
-        options[ 2 ] =
-            new CLOptionDescriptor( "apps-path",
-                                    CLOptionDescriptor.ARGUMENT_REQUIRED,
-                                    APPS_PATH_OPT,
-                                    REZ.getString( "cli.opt.apps-path.desc" ) );
-
-        options[ 3 ] =
-            new CLOptionDescriptor( "debug-init",
-                                    CLOptionDescriptor.ARGUMENT_DISALLOWED,
-                                    DEBUG_LOG_OPT,
-                                    REZ.getString( "cli.opt.debug-init.desc" ) );
-
-        options[ 4 ] =
-            new CLOptionDescriptor( "remote-manager",
-                                    CLOptionDescriptor.ARGUMENT_DISALLOWED,
-                                    REMOTE_MANAGER_OPT,
-                                    REZ.getString( "cli.opt.remote-manager.desc" ) );
-
-        options[ 5 ] =
-            new CLOptionDescriptor( "disable-hook",
-                                    CLOptionDescriptor.ARGUMENT_DISALLOWED,
-                                    DISABLE_HOOK_OPT,
-                                    REZ.getString( "cli.opt.disable-hook.desc" ) );
-
-        options[ 6 ] =
-            new CLOptionDescriptor( "application",
-                                    CLOptionDescriptor.ARGUMENT_REQUIRED,
-                                    APPLICATION_OPT,
-                                    REZ.getString( "cli.opt.application.desc" ) );
-
-        options[ 7 ] =
-            new CLOptionDescriptor( "persistent",
-                                    CLOptionDescriptor.ARGUMENT_DISALLOWED,
-                                    PERSISTENT_OPT,
-                                    REZ.getString( "cli.opt.persistent.desc" ) );
-
-        options[ 8 ] =
-            new CLOptionDescriptor( "configfile",
-                                    CLOptionDescriptor.ARGUMENT_REQUIRED,
-                                    CONFIGFILE_OPT,
-                                    REZ.getString( "cli.opt.configfile.desc" ) );
-
-        return options;
+        final HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp( m_command, REZ.getString( "cli.desc.available.header" ), options, "" );
     }
-
-    public Parameters getParameters()
-    {
-        return m_parameters;
-    }
-
+    
     public boolean parseCommandLineOptions( final String[] args )
     {
-        final CLOptionDescriptor[] options = createCLOptions();
-        final CLArgsParser parser = new CLArgsParser( args, options );
-
-        if( null != parser.getErrorString() )
-        {
-            final String message = REZ.getString( "cli.error.parser", parser.getErrorString() );
-            System.err.println( message );
-            return false;
-        }
-
-        final List clOptions = parser.getArguments();
-        final int size = clOptions.size();
-
-        for( int i = 0; i < size; i++ )
+        final Options options = createOptions();
+        final CommandLineParser parser = new PosixParser();
+        try
         {
-            final CLOption option = (CLOption)clOptions.get( i );
+            final CommandLine line = parser.parse( options, args );
 
-            switch( option.getDescriptor().getId() )
+            if ( line.hasOption( HELP_OPT ) )
+            {
+                usage( options );
+                return false;
+            }
+            if ( line.hasOption( DEBUG_LOG_OPT ) )
+            {
+                m_parameters.setParameter( "log-priority", "DEBUG" );
+            }
+            if ( line.hasOption( LOG_FILE_OPT ) )
             {
-                case 0:
-                    {
-                        final String message =
-                            REZ.getString( "cli.error.unknown.arg", option.getArgument() );
-                        System.err.println( message );
-                    }
-                    return false;
-
-                case HELP_OPT:
-                    usage( options );
-                    return false;
-
-                case DEBUG_LOG_OPT:
-                    m_parameters.setParameter( "log-priority", "DEBUG" );
-                    break;
-
-                case LOG_FILE_OPT:
-                    m_parameters.setParameter( "log-destination", option.getArgument() );
-                    break;
-
-                case APPS_PATH_OPT:
-                    m_parameters.setParameter( "phoenix.apps.dir", option.getArgument() );
-                    break;
-
-                case REMOTE_MANAGER_OPT:
-                    m_parameters.setParameter( SystemManager.ROLE, MANAGER_IMPL );
-                    break;
-
-                case APPLICATION_OPT:
-                    m_parameters.setParameter( "application-location", option.getArgument() );
-                    break;
-
-                case PERSISTENT_OPT:
-                    m_parameters.setParameter( "persistent", "true" );
-                    break;
-
-                case DISABLE_HOOK_OPT:
-                    m_parameters.setParameter( "disable-hook", "true" );
-                    break;
-
-                case CONFIGFILE_OPT:
-                    m_parameters.setParameter( "phoenix.configfile", option.getArgument() );
-                    break;
+                m_parameters.setParameter( "log-destination", line.getOptionValue( LOG_FILE_OPT ) );
+            }
+            if ( line.hasOption( APPS_PATH_OPT ) )
+            {
+                m_parameters.setParameter( "phoenix.apps.dir", line.getOptionValue( APPS_PATH_OPT ) );
+            }
+            if ( line.hasOption( REMOTE_MANAGER_OPT ) )
+            {
+                m_parameters.setParameter( SystemManager.ROLE, MANAGER_IMPL );
+            }
+            if ( line.hasOption( LOG_FILE_OPT ) )
+            {
+                m_parameters.setParameter( "log-destination", line.getOptionValue( LOG_FILE_OPT ) );
+            }
+            if ( line.hasOption( APPLICATION_OPT ) )
+            {
+                m_parameters.setParameter( "application-location", line.getOptionValue( APPLICATION_OPT ) );
+            }
+            if ( line.hasOption( PERSISTENT_OPT ) )
+            {
+                m_parameters.setParameter( "persistent", "true" );
+            }
+            if ( line.hasOption( DISABLE_HOOK_OPT ) )
+            {
+                m_parameters.setParameter( "disable-hook", "true" );
+            }
+            if ( line.hasOption( CONFIGFILE_OPT ) )
+            {
+                m_parameters.setParameter( "phoenix.configfile", line.getOptionValue( CONFIGFILE_OPT ) );
             }
         }
-
+        catch ( final ParseException pe )
+        {
+            return false;
+        }
         return true;
+    }
+    
+    /**
+     * Initialise the options for command line parser.
+     */
+    private Options createOptions()
+    {
+        final Options options = new Options();
+        options.addOption( HELP_OPT, "help", false, REZ.getString( "cli.opt.help.desc" ) );
+        options.addOption( OptionBuilder.withLongOpt( "log-file" )
+                                        .withDescription( REZ.getString( "cli.opt.log-file.desc" ) )
+                                        .hasArg()
+                                        .withArgName( "argument" )
+                                        .create( LOG_FILE_OPT ) );
+        options.addOption( OptionBuilder.withLongOpt( "apps-path" )
+                                        .withDescription( REZ.getString( "cli.opt.apps-path.desc" ) )
+                                        .hasArg()
+                                        .withArgName( "argument" )
+                                        .create( APPS_PATH_OPT ) );
+        options.addOption( OptionBuilder.withLongOpt( "debug-init" )
+                                        .withDescription( REZ.getString( "cli.opt.debug-init.desc" ) )
+                                        .create( DEBUG_LOG_OPT ) );
+        options.addOption( OptionBuilder.withLongOpt( REMOTE_MANAGER_OPT )
+                                        .withDescription( REZ.getString( "cli.opt.remote-manager.desc" ) )
+                                        .create() );
+        options.addOption( OptionBuilder.withLongOpt( DISABLE_HOOK_OPT )
+                                        .withDescription( REZ.getString( "cli.opt.disable-hook.desc" ) )
+                                        .create() );
+        options.addOption( OptionBuilder.withLongOpt( APPLICATION_OPT )
+                                        .withDescription( REZ.getString( "cli.opt.application.desc" ) )
+                                        .hasArg()
+                                        .withArgName( "argument" )
+                                        .create() );
+        options.addOption( OptionBuilder.withLongOpt("persistent" )
+                                        .withDescription( REZ.getString( "cli.opt.persistent.desc" ) )
+                                        .create( PERSISTENT_OPT ) );
+        options.addOption( OptionBuilder.withLongOpt( "configfile" )
+                                        .withDescription( REZ.getString( "cli.opt.configfile.desc" ) )
+                                        .hasArg()
+                                        .withArgName( "argument" )
+                                        .create( CONFIGFILE_OPT ) );
+        return options;
     }
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.