Coverage Report - org.dishevelled.commandline.Usage
 
Classes in this File Line Coverage Branch Coverage Complexity
Usage
89%
35/39
88%
16/18
4.333
 
 1  
 /*
 2  
 
 3  
     dsh-commandline  Command line parser based on typed arguments.
 4  
     Copyright (c) 2004-2014 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  
  */
 39  
 public final class Usage
 40  
 {
 41  
 
 42  
     /**
 43  
      * Private no-arg constructor.
 44  
      */
 45  
     private Usage()
 46  0
     {
 47  
         // empty
 48  0
     }
 49  
 
 50  
 
 51  
     /**
 52  
      * Generate an usage string to the specified output stream.
 53  
      *
 54  
      * @param message message
 55  
      * @param cause cause
 56  
      * @param commandLine command line
 57  
      * @param arguments list of arguments
 58  
      * @param out output stream, must not be null
 59  
      */
 60  
     public static void usage(final String message,
 61  
                              final Throwable cause,
 62  
                              final CommandLine commandLine,
 63  
                              final ArgumentList arguments,
 64  
                              final OutputStream out)
 65  
     {
 66  8
         usage(null, message, cause, commandLine, arguments, out);
 67  7
     }
 68  
 
 69  
     /**
 70  
      * Generate an usage string to the specified output stream.
 71  
      *
 72  
      * @since 1.1
 73  
      * @param header header
 74  
      * @param message message
 75  
      * @param cause cause
 76  
      * @param commandLine command line
 77  
      * @param arguments list of arguments
 78  
      * @param out output stream, must not be null
 79  
      */
 80  
     public static void usage(final String header,
 81  
                              final String message,
 82  
                              final Throwable cause,
 83  
                              final CommandLine commandLine,
 84  
                              final ArgumentList arguments,
 85  
                              final OutputStream out)
 86  
     {
 87  8
         if (out == null)
 88  
         {
 89  1
             throw new IllegalArgumentException("out must not be null");
 90  
         }
 91  
 
 92  7
         PrintWriter pw = new PrintWriter(out, false);
 93  
 
 94  7
         if (header != null)
 95  
         {
 96  0
             pw.println(header);            
 97  0
             pw.print("\n");
 98  
         }
 99  
 
 100  7
         pw.println("usage:");
 101  
 
 102  7
         if (message != null)
 103  
         {
 104  6
             pw.println(message);
 105  6
             pw.print("\n");
 106  
         }
 107  
 
 108  7
         if (cause != null)
 109  
         {
 110  5
             cause.printStackTrace(pw);
 111  5
             pw.print("\n");
 112  
         }
 113  
 
 114  7
         if ((arguments != null) && (arguments.size() > 0))
 115  
         {
 116  1
             pw.println("arguments:");
 117  
 
 118  1
             for (Argument<?> a : arguments)
 119  
             {
 120  2
                 StringBuffer sb = new StringBuffer();
 121  2
                 sb.append("   -");
 122  2
                 sb.append(a.getShortName());
 123  2
                 sb.append(", --");
 124  2
                 sb.append(a.getLongName());
 125  
 
 126  2
                 if (!(a instanceof Switch))
 127  
                 {
 128  2
                     sb.append(" ");
 129  2
                     ParameterizedType parameterizedType = (ParameterizedType) a.getClass().getGenericSuperclass();
 130  2
                     Type[] types = parameterizedType.getActualTypeArguments();
 131  2
                     sb.append(Arrays.asList(types));
 132  
                 }
 133  
 
 134  2
                 sb.append("  ");
 135  2
                 sb.append(a.getDescription());
 136  
 
 137  2
                 if (a.isRequired())
 138  
                 {
 139  1
                     sb.append(" [required]");
 140  
                 }
 141  
                 else
 142  
                 {
 143  1
                     sb.append(" [optional]");
 144  
                 }
 145  
 
 146  2
                 pw.println(sb.toString());
 147  2
             }
 148  
         }
 149  
 
 150  7
         pw.flush();
 151  7
     }
 152  
 }