Coverage Report - org.dishevelled.commandline.Usage
 
Classes in this File Line Coverage Branch Coverage Complexity
Usage
94%
33/35
93%
15/16
5.5
 
 1  
 /*
 2  
 
 3  
     dsh-commandline  Command line parser based on typed arguments.
 4  
     Copyright (c) 2004-2012 held jointly by the individual authors.
 5  
 
 6  
     This library is free software; you can redistribute it and/or modify it
 7  
     under the terms of the GNU Lesser General Public License as published
 8  
     by the Free Software Foundation; either version 3 of the License, or (at
 9  
     your option) any later version.
 10  
 
 11  
     This library is distributed in the hope that it will be useful, but WITHOUT
 12  
     ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
 13  
     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 14  
     License for more details.
 15  
 
 16  
     You should have received a copy of the GNU Lesser General Public License
 17  
     along with this library;  if not, write to the Free Software Foundation,
 18  
     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 19  
 
 20  
     > http://www.fsf.org/licensing/licenses/lgpl.html
 21  
     > http://www.opensource.org/licenses/lgpl-license.php
 22  
 
 23  
 */
 24  
 package org.dishevelled.commandline;
 25  
 
 26  
 import java.io.PrintWriter;
 27  
 import java.io.OutputStream;
 28  
 
 29  
 import java.util.Arrays;
 30  
 
 31  
 import java.lang.reflect.ParameterizedType;
 32  
 import java.lang.reflect.Type;
 33  
 
 34  
 /**
 35  
  * Usage string.
 36  
  *
 37  
  * @author  Michael Heuer
 38  
  * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
 39  
  */
 40  
 public final class Usage
 41  
 {
 42  
 
 43  
     /**
 44  
      * Private no-arg constructor.
 45  
      */
 46  
     private Usage()
 47  0
     {
 48  
         // empty
 49  0
     }
 50  
 
 51  
 
 52  
     /**
 53  
      * Generate an usage string to the specified output stream.
 54  
      *
 55  
      * @param message message
 56  
      * @param cause cause
 57  
      * @param commandLine command line
 58  
      * @param arguments list of arguments
 59  
      * @param out output stream, must not be null
 60  
      */
 61  
     public static void usage(final String message,
 62  
                              final Throwable cause,
 63  
                              final CommandLine commandLine,
 64  
                              final ArgumentList arguments,
 65  
                              final OutputStream out)
 66  
     {
 67  8
         if (out == null)
 68  
         {
 69  1
             throw new IllegalArgumentException("out must not be null");
 70  
         }
 71  
 
 72  7
         PrintWriter pw = new PrintWriter(out, false);
 73  7
         pw.println("usage:");
 74  
 
 75  7
         if (message != null)
 76  
         {
 77  6
             pw.println(message);
 78  6
             pw.print("\n");
 79  
         }
 80  
 
 81  7
         if (cause != null)
 82  
         {
 83  5
             cause.printStackTrace(pw);
 84  5
             pw.print("\n");
 85  
         }
 86  
 
 87  7
         if ((arguments != null) && (arguments.size() > 0))
 88  
         {
 89  1
             pw.println("arguments:");
 90  
 
 91  1
             for (Argument<?> a : arguments)
 92  
             {
 93  2
                 StringBuffer sb = new StringBuffer();
 94  2
                 sb.append("   -");
 95  2
                 sb.append(a.getShortName());
 96  2
                 sb.append(", --");
 97  2
                 sb.append(a.getLongName());
 98  
 
 99  2
                 if (!(a instanceof Switch))
 100  
                 {
 101  2
                     sb.append(" ");
 102  2
                     ParameterizedType parameterizedType = (ParameterizedType) a.getClass().getGenericSuperclass();
 103  2
                     Type[] types = parameterizedType.getActualTypeArguments();
 104  2
                     sb.append(Arrays.asList(types));
 105  
                 }
 106  
 
 107  2
                 sb.append("  ");
 108  2
                 sb.append(a.getDescription());
 109  
 
 110  2
                 if (a.isRequired())
 111  
                 {
 112  1
                     sb.append(" [required]");
 113  
                 }
 114  
                 else
 115  
                 {
 116  1
                     sb.append(" [optional]");
 117  
                 }
 118  
 
 119  2
                 pw.println(sb.toString());
 120  2
                 pw.print("\n");
 121  2
             }
 122  
         }
 123  
 
 124  7
         pw.flush();
 125  7
     }
 126  
 }