You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2003/08/13 17:18:48 UTC

cvs commit: incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/util HelpFormatter.java

jdillon     2003/08/13 08:18:48

  Modified:    modules/twiddle project.xml
               modules/twiddle/src/java/org/apache/geronimo/twiddle
                        Twiddle.java
               modules/twiddle/src/java/org/apache/geronimo/twiddle/cli
                        Main.java
               modules/twiddle/src/java/org/apache/geronimo/twiddle/command
                        AbstractCommand.java CommandContainer.java
                        CommandContext.java CommandExecutor.java
                        CommandFactory.java
               modules/twiddle/src/java/org/apache/geronimo/twiddle/commands
                        ExitCommand.java
               modules/twiddle/src/java/org/apache/geronimo/twiddle/console
                        Console.java IOContext.java
  Added:       modules/twiddle/src/java/org/apache/geronimo/twiddle/util
                        HelpFormatter.java
  Log:
   o Hooked up minimal support for io in command context
  
  Revision  Changes    Path
  1.3       +7 -1      incubator-geronimo/modules/twiddle/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/project.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- project.xml	13 Aug 2003 10:55:51 -0000	1.2
  +++ project.xml	13 Aug 2003 15:18:47 -0000	1.3
  @@ -67,6 +67,12 @@
       </dependency>
       
       <dependency>
  +      <id>commons-beanutils</id>
  +      <version>1.6.1</version>
  +      <url>http://jakarta.apache.org/commons/beanutils.html</url>
  +    </dependency>
  +    
  +    <dependency>
         <id>commons-logging</id>
         <version>1.0.3</version>
         <url>http://jakarta.apache.org/commons/logging.html</url>
  
  
  
  1.4       +33 -4     incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/Twiddle.java
  
  Index: Twiddle.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/Twiddle.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Twiddle.java	13 Aug 2003 10:55:51 -0000	1.3
  +++ Twiddle.java	13 Aug 2003 15:18:47 -0000	1.4
  @@ -71,6 +71,8 @@
   import org.apache.geronimo.twiddle.command.CommandExecutor;
   import org.apache.geronimo.twiddle.command.CommandException;
   
  +import org.apache.geronimo.twiddle.console.IOContext;
  +
   /**
    * <em>Twiddle</em> is a command processor.
    *
  @@ -83,6 +85,9 @@
    */
   public class Twiddle
   {
  +    /** The input/output context. */
  +    protected IOContext io;
  +    
       /** The command container. */
       protected CommandContainer container;
       
  @@ -92,10 +97,34 @@
       /**
        * Construct a <code>Twiddle</code> command processor.
        */
  +    public Twiddle(final IOContext io)
  +    {
  +        if (io == null) {
  +            throw new NullArgumentException("io");
  +        }
  +        
  +        this.io = io;
  +        this.container = new CommandContainer();
  +        this.executor = new CommandExecutor(container);
  +    }
  +    
  +    /**
  +     * Construct a <code>Twiddle</code> command processor using system
  +     * defaults for the input/output context.
  +     */
       public Twiddle()
       {
  -        container = new CommandContainer();
  -        executor = new CommandExecutor(container);
  +        this(new IOContext(System.in, System.out, System.err));
  +    }
  +    
  +    /**
  +     * Get the input/output context.
  +     *
  +     * @return The input/output context.
  +     */
  +    public IOContext getIOContext()
  +    {
  +        return io;
       }
       
       /**
  @@ -175,7 +204,7 @@
           out.println("|_   _|_      _(_) __| | __| | | ___");
           out.println("  | | \\ \\ /\\ / / |/ _` |/ _` | |/ _ \\");
           out.println("  | |  \\ V  V /| | (_| | (_| | |  __/");
  -        out.println("  |_|   \\_/\\_/ |_|\\__,_|\\__,_|_|\\___|");
  +        out.print("  |_|   \\_/\\_/ |_|\\__,_|\\__,_|_|\\___|");
           out.flush();
           
           return writer.toString();
  
  
  
  1.2       +13 -16    incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/cli/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/cli/Main.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Main.java	13 Aug 2003 08:32:09 -0000	1.1
  +++ Main.java	13 Aug 2003 15:18:47 -0000	1.2
  @@ -57,6 +57,9 @@
   package org.apache.geronimo.twiddle.cli;
   
   import java.io.File;
  +import java.io.PrintWriter;
  +import java.io.OutputStreamWriter;
  +
   import java.net.URL;
   import java.net.URLDecoder;
   
  @@ -67,13 +70,14 @@
   import org.apache.commons.cli.PosixParser;
   import org.apache.commons.cli.OptionBuilder;
   import org.apache.commons.cli.ParseException;
  -import org.apache.commons.cli.HelpFormatter;
   
   import org.apache.geronimo.twiddle.Twiddle;
   
   import org.apache.geronimo.twiddle.config.Configuration;
   import org.apache.geronimo.twiddle.config.ConfigurationReader;
   
  +import org.apache.geronimo.twiddle.util.HelpFormatter;
  +
   /**
    * Command-line interface to <code>Twiddle</code>.
    *
  @@ -111,24 +115,17 @@
           // parse the command line arguments
           CommandLine line = parser.parse(options, args, true);
           
  -        // Dump parsed options
  -        System.out.println("Twiddle options:");
  -        Option[] opts = line.getOptions();
  -        for (int i=0; i<opts.length; i++) {
  -            System.out.println(opts[i]);
  -        }
  -        
           // Display command-line help and exit
           if (line.hasOption('h')) {
  -            System.out.println(Twiddle.getBanner());
  -            HelpFormatter formatter = new HelpFormatter();
  -            formatter.printHelp(
  +            PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out), true);
  +            HelpFormatter formatter = new HelpFormatter(out);
  +            
  +            formatter.print(
  +                Twiddle.getBanner(),
                   "twiddle [options] (<command> [options] [arguments])*",
  -                LINE_SEPARATOR + "Options:",
  -                options,
  -                LINE_SEPARATOR
  +                options
               );
  -            System.out.println();
  +            
               System.exit(0);
           }
           
  
  
  
  1.2       +44 -1     incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/AbstractCommand.java
  
  Index: AbstractCommand.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/AbstractCommand.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractCommand.java	13 Aug 2003 08:32:09 -0000	1.1
  +++ AbstractCommand.java	13 Aug 2003 15:18:47 -0000	1.2
  @@ -56,6 +56,9 @@
   
   package org.apache.geronimo.twiddle.command;
   
  +import java.io.PrintWriter;
  +import java.io.Reader;
  +
   import org.apache.geronimo.common.CloneableObject;
   import org.apache.geronimo.common.NullArgumentException;
   
  @@ -81,6 +84,11 @@
       /** The command context. */
       private CommandContext context;
       
  +    
  +    /////////////////////////////////////////////////////////////////////////
  +    //                              Command                                //
  +    /////////////////////////////////////////////////////////////////////////
  +    
       public void setCommandInfo(final CommandInfo info)
       {
           if (info == null) {
  @@ -113,6 +121,11 @@
           context = null;
       }
       
  +    
  +    /////////////////////////////////////////////////////////////////////////
  +    //                          Sub-class Helpers                          //
  +    /////////////////////////////////////////////////////////////////////////
  +    
       /**
        * Provides sub-classes with access to the command context.
        *
  @@ -127,5 +140,35 @@
           }
           
           return context;
  +    }
  +    
  +    /**
  +     * Provides sub-classes with the reader for the current context.
  +     *
  +     * @return The reader for the current context.
  +     */
  +    protected Reader getReader()
  +    {
  +        return getCommandContext().getIOContext().getReader();
  +    }
  +    
  +    /**
  +     * Provides sub-classes with the writer for the current context.
  +     *
  +     * @return The writer for the current context.
  +     */
  +    protected PrintWriter getWriter()
  +    {
  +        return getCommandContext().getIOContext().getWriter();
  +    }
  +    
  +    /**
  +     * Provides sub-classes with the error writer for the current context.
  +     *
  +     * @return The error writer for the current context.
  +     */
  +    protected PrintWriter getErrorWriter()
  +    {
  +        return getCommandContext().getIOContext().getErrorWriter();
       }
   }
  
  
  
  1.2       +2 -2      incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandContainer.java
  
  Index: CommandContainer.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandContainer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CommandContainer.java	13 Aug 2003 08:32:09 -0000	1.1
  +++ CommandContainer.java	13 Aug 2003 15:18:47 -0000	1.2
  @@ -63,7 +63,7 @@
   import org.apache.geronimo.common.NullArgumentException;
   
   /**
  - * ???
  + * A container for commands.
    *
    * @version <code>$Id$</code>
    * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  
  
  
  1.3       +4 -38     incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandContext.java
  
  Index: CommandContext.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandContext.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CommandContext.java	13 Aug 2003 11:48:57 -0000	1.2
  +++ CommandContext.java	13 Aug 2003 15:18:47 -0000	1.3
  @@ -66,46 +66,12 @@
    */
   public interface CommandContext
   {
  -    /////////////////////////////////////////////////////////////////////////
  -    //                          Environemnt Access                         //
  -    /////////////////////////////////////////////////////////////////////////
  -    
       /**
  -     * Get an enviornment attribute.
  +     * Get the command environemnt.
        *
  -     * @param name  The name of the attribute.
  -     * @return      The attribute value, or null if the attribute was not set.
  +     * @return The commands environment.
        */
  -    Object get(String name);
  -    
  -    /**
  -     * Set an environment attribute.
  -     *
  -     * @param name      The name of the attribute.
  -     * @param value     The value of the attribute.
  -     */
  -    Object set(String name, Object value);
  -    
  -    /**
  -     * Unset an enviornment attribute.
  -     *
  -     * @param name  The name of the attribute.
  -     * @return      The previous value of the attribute, or null if there was none.
  -     */
  -    Object unset(String name);
  -    
  -    /**
  -     * Check if an attribute is set.
  -     *
  -     * @param name  The name of the attribute.
  -     * @return      True if the attribute is set, else false.
  -     */
  -    boolean isSet(String name);
  -    
  -    
  -    /////////////////////////////////////////////////////////////////////////
  -    //                         Input/Output Access                         //
  -    /////////////////////////////////////////////////////////////////////////
  +    Environment getEnvironment();
       
       /**
        * Get the input/output context for the command.
  
  
  
  1.2       +16 -6     incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandExecutor.java
  
  Index: CommandExecutor.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandExecutor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CommandExecutor.java	13 Aug 2003 08:32:09 -0000	1.1
  +++ CommandExecutor.java	13 Aug 2003 15:18:47 -0000	1.2
  @@ -65,8 +65,10 @@
   import org.apache.geronimo.twiddle.command.CommandException;
   import org.apache.geronimo.twiddle.command.CommandNotFoundException;
   
  +import org.apache.geronimo.twiddle.console.IOContext;
  +
   /**
  - * ???
  + * Executes commands.
    *
    * @version <code>$Id$</code>
    * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  @@ -112,10 +114,18 @@
       protected int execute(final Command command, final String[] args) throws Exception
       {
           // Set the context
  -        //
  -        // TODO
  -        //
  -        // command.setCommandContext(ctx);
  +        CommandContext ctx = new CommandContext() {
  +            public Environment getEnvironment()
  +            {
  +                return null;
  +            }
  +            
  +            public IOContext getIOContext()
  +            {
  +                return new IOContext();
  +            }
  +        };
  +        command.setCommandContext(ctx);
           
           // Execute the command
           int result = Command.FAILURE;
  
  
  
  1.3       +5 -1      incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandFactory.java
  
  Index: CommandFactory.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/command/CommandFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CommandFactory.java	13 Aug 2003 11:48:57 -0000	1.2
  +++ CommandFactory.java	13 Aug 2003 15:18:47 -0000	1.3
  @@ -101,6 +101,10 @@
           ClassLoader cl = Thread.currentThread().getContextClassLoader();
           Command command = (Command)Beans.instantiate(cl, config.getCode());
           
  +        //
  +        // TODO: Use Commons BeanUtils
  +        //
  +        
           // Apply attributes
           BeanInfo info = Introspector.getBeanInfo(command.getClass());
           Attribute[] attrs = config.getAttribute();
  
  
  
  1.2       +13 -32    incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/commands/ExitCommand.java
  
  Index: ExitCommand.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/commands/ExitCommand.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ExitCommand.java	13 Aug 2003 08:32:09 -0000	1.1
  +++ ExitCommand.java	13 Aug 2003 15:18:48 -0000	1.2
  @@ -56,6 +56,8 @@
   
   package org.apache.geronimo.twiddle.commands;
   
  +import java.io.PrintWriter;
  +
   import org.apache.commons.cli.Options;
   import org.apache.commons.cli.Option;
   import org.apache.commons.cli.CommandLine;
  @@ -63,7 +65,6 @@
   import org.apache.commons.cli.PosixParser;
   import org.apache.commons.cli.OptionBuilder;
   import org.apache.commons.cli.ParseException;
  -import org.apache.commons.cli.HelpFormatter;
   
   import org.apache.geronimo.common.NullArgumentException;
   import org.apache.geronimo.common.UnreachableStatementException;
  @@ -73,6 +74,8 @@
   import org.apache.geronimo.twiddle.command.CommandContext;
   import org.apache.geronimo.twiddle.command.AbstractCommand;
   
  +import org.apache.geronimo.twiddle.util.HelpFormatter;
  +
   /**
    * Exit command... terminates the virtual machine.
    *
  @@ -88,11 +91,8 @@
               throw new NullArgumentException("args");
           }
           
  -        // Dump input arguments
  -        System.out.println("Exit arguments:");
  -        for (int i=0; i<args.length; i++) {
  -            System.out.println(args[i]);
  -        }
  +        // Get our output writer
  +        PrintWriter out = getWriter();
           
           // Create the Options
           Options options = new Options();
  @@ -110,42 +110,23 @@
           // Carse the command line arguments
           CommandLine line = parser.parse(options, args);
           
  -        // Dump parsed options
  -        System.out.println("Exit options:");
  -        Option[] opts = line.getOptions();
  -        for (int i=0; i<opts.length; i++) {
  -            System.out.println(opts[i]);
  -        }
  -        
  -        // Dump input arguments
  -        System.out.println("Unused arguments:");
  -        args = line.getArgs();
  -        for (int i=0; i<args.length; i++) {
  -            System.out.println(args[i]);
  -        }
  -        
           // Display help
           if (line.hasOption('h')) {
               CommandInfo info = getCommandInfo();
               if (info.hasDescription()) {
  -                System.out.println(info.getDescription());
  -                System.out.println();
  +                out.println(info.getDescription());
  +                out.println();
               }
               
  -            HelpFormatter formatter = new HelpFormatter();
  -            formatter.printHelp(
  -                info.getName() + " [options]",
  -                LINE_SEPARATOR + "Options:",
  -                options,
  -                LINE_SEPARATOR);
  -            System.out.println();
  +            HelpFormatter formatter = new HelpFormatter(out);
  +            formatter.print(info.getName() + " [options]", options);
               
               return Command.SUCCESS;
           }
           
           // Get the status code to exit with
           int statusCode = 0;
  -        if (line.hasOption('c') || line.hasOption("code")) {
  +        if (line.hasOption('c')) {
               String value = line.getOptionValue('c');
               statusCode = Integer.parseInt(value);
           }
  @@ -154,7 +135,7 @@
           // TODO: Parse out unused arguments too... may want to just use args here
           //
           
  -        System.out.println("Exiting with status code: " + statusCode);
  +        out.println("Exiting with status code: " + statusCode);
           System.exit(statusCode);
           
           throw new UnreachableStatementException();
  
  
  
  1.2       +5 -1      incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/console/Console.java
  
  Index: Console.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/console/Console.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Console.java	13 Aug 2003 10:54:37 -0000	1.1
  +++ Console.java	13 Aug 2003 15:18:48 -0000	1.2
  @@ -70,6 +70,10 @@
    */
   public interface Console
   {
  +    //
  +    // TODO: Refatcor line reading into subpackage/classes
  +    //
  +    
       /**
        * Set the input/output context.
        *
  
  
  
  1.2       +66 -2     incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/console/IOContext.java
  
  Index: IOContext.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/console/IOContext.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- IOContext.java	13 Aug 2003 10:54:37 -0000	1.1
  +++ IOContext.java	13 Aug 2003 15:18:48 -0000	1.2
  @@ -57,12 +57,17 @@
   package org.apache.geronimo.twiddle.console;
   
   import java.io.InputStream;
  +import java.io.Reader;
  +import java.io.InputStreamReader;
   import java.io.OutputStream;
  +import java.io.PrintWriter;
  +import java.io.OutputStreamWriter;
   
   import org.apache.geronimo.common.NullArgumentException;
   
   /**
  - * Abstraction of a collection of input and output streams.
  + * Abstraction of a collection of input and output streams
  + * as well as helper methods to access the corresponding reader/writers.
    *
    * @version <code>$Id$</code>
    * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  @@ -72,12 +77,21 @@
       /** The input stream. */
       protected InputStream input;
       
  +    /** The reader (or null if reader was not accessed). */
  +    protected Reader reader;
  +    
       /** The output stream. */
       protected OutputStream output;
       
  +    /** The writer (or null if writer was not accessed). */
  +    protected PrintWriter writer;
  +    
       /** The error output stream. */
       protected OutputStream errorOutput;
       
  +    /** The error writer (or null if writer was not accessed). */
  +    protected PrintWriter errorWriter;
  +    
       /**
        * Construct a <code>IOContext</code>.
        *
  @@ -114,6 +128,14 @@
       }
       
       /**
  +     * Construct a <code>IOContext</code> using system defaults.
  +     */
  +    public IOContext()
  +    {
  +        this(System.in, System.out, System.err);
  +    }
  +    
  +    /**
        * Get the input stream.
        *
        * @return The input stream.
  @@ -124,6 +146,20 @@
       }
       
       /**
  +     * Get the reader.
  +     *
  +     * @return The reader.
  +     */
  +    public Reader getReader()
  +    {
  +        if (reader == null) {
  +            reader = new InputStreamReader(input);
  +        }
  +        
  +        return reader;
  +    }
  +    
  +    /**
        * Get the output stream.
        *
        * @return The output stream.
  @@ -134,6 +170,20 @@
       }
       
       /**
  +     * Get the writer.
  +     *
  +     * @return The writer.
  +     */
  +    public PrintWriter getWriter()
  +    {
  +        if (writer == null) {
  +            writer = new PrintWriter(new OutputStreamWriter(output), true);
  +        }
  +        
  +        return writer;
  +    }
  +    
  +    /**
        * Get the error output stream.
        *
        * @return The error output stream.
  @@ -141,5 +191,19 @@
       public OutputStream getErrorOutputStream()
       {
           return errorOutput;
  +    }
  +    
  +    /**
  +     * Get the error writer.
  +     *
  +     * @return The error writer.
  +     */
  +    public PrintWriter getErrorWriter()
  +    {
  +        if (errorWriter == null) {
  +            errorWriter = new PrintWriter(new OutputStreamWriter(errorOutput), true);
  +        }
  +        
  +        return errorWriter;
       }
   }
  
  
  
  1.1                  incubator-geronimo/modules/twiddle/src/java/org/apache/geronimo/twiddle/util/HelpFormatter.java
  
  Index: HelpFormatter.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.twiddle.util;
  
  import java.io.PrintWriter;
  
  import org.apache.commons.cli.Options;
  
  import org.apache.geronimo.common.NullArgumentException;
  
  /**
   * A helper to handle command help output.
   *
   * @version <code>$Id: HelpFormatter.java,v 1.1 2003/08/13 15:18:48 jdillon Exp $</code>
   * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
   */
  public class HelpFormatter
      extends org.apache.commons.cli.HelpFormatter
  {
      /** Platform dependent line separator. */
      public static final String LINE_SEPARATOR = System.getProperty("line.separator");
      
      /** Terminal width. */
      public static final int TERMINAL_WIDTH = 80;
      
      /** The writer to print help to. */
      protected PrintWriter out;
      
      /**
       * Construct a <code>HelpFormatter</code>
       *
       * @param out   The writer to print to.
       */
      public HelpFormatter(final PrintWriter out)
      {
          if (out == null) {
              throw new NullArgumentException("out");
          }
          
          this.out = out;
      }
      
      /**
       * Print help.
       *
       * @param desc      The help desctiption, or null for none.
       * @param usage     The command usage/syntax.
       * @param header    The options header, or null for the default.
       * @param options   The command line options.
       * @param footer    The options footer, or null for the default.
       */
      public void print(final String desc, final String usage, String header,
                        final Options options, String footer)
      {
          if (usage == null) {
              throw new NullArgumentException("usage");
          }
          if (options == null) {
              throw new NullArgumentException("options");
          }
          
          if (desc != null) {
              out.println(desc);
              out.println();
          }
          
          if (header == null) {
              header = LINE_SEPARATOR + "Options:";
          }
          
          if (footer == null) {
              footer = LINE_SEPARATOR;
          }
          
          printHelp(out, TERMINAL_WIDTH, usage, header, options, 2, 4, footer);
          out.println();
          out.flush();
      }
      
      /**
       * Print help.
       *
       * @param desc      The help desctiption, or null for none.
       * @param usage     The command usage/syntax.
       * @param options   The command line options.
       */
      public void print(final String desc, final String usage, final Options options)
      {
          print(desc, usage, null, options, null);
      }
      
      /**
       * Print help.
       *
       * @param usage     The command usage/syntax.
       * @param options   The command line options.
       */
      public void print(final String usage, final Options options)
      {
          print(null, usage, null, options, null);
      }
  }