Coverage Report - org.dishevelled.commandline.CommandLineParser
 
Classes in this File Line Coverage Branch Coverage Complexity
CommandLineParser
92%
24/26
100%
14/14
7
 
 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  
 /**
 27  
  * Command line parser.
 28  
  *
 29  
  * @author  Michael Heuer
 30  
  */
 31  
 public final class CommandLineParser
 32  
 {
 33  
 
 34  
     /**
 35  
      * Private no-arg constructor.
 36  
      */
 37  
     private CommandLineParser()
 38  0
     {
 39  
         // empty
 40  0
     }
 41  
 
 42  
 
 43  
     /**
 44  
      * Parse the specified command line according to the specified
 45  
      * list of arguments.
 46  
      *
 47  
      * @param commandLine command line, must not be null
 48  
      * @param arguments list of arguments, must not be null
 49  
      * @throws CommandLineParseException if an error occurs
 50  
      */
 51  
     public static void parse(final CommandLine commandLine, final ArgumentList arguments)
 52  
         throws CommandLineParseException
 53  
     {
 54  464
         if (commandLine == null)
 55  
         {
 56  1
             throw new IllegalArgumentException("commandLine must not be null");
 57  
         }
 58  463
         if (arguments == null)
 59  
         {
 60  1
             throw new IllegalArgumentException("arguments must not be null");
 61  
         }
 62  
 
 63  
         try
 64  
         {
 65  1297
             while (commandLine.hasNext())
 66  
             {
 67  915
                 String s = commandLine.next();
 68  
 
 69  915
                 for (Argument<?> a : arguments)
 70  
                 {
 71  915
                     a.visit(s, commandLine);
 72  835
                 }
 73  835
             }
 74  
         }
 75  80
         catch (Exception e)
 76  
         {
 77  80
             throw new CommandLineParseException(e);
 78  382
         }
 79  
 
 80  382
         for (Argument<?> a : arguments)
 81  
         {
 82  382
             if (a.isRequired() && !(a.wasFound()))
 83  
             {
 84  27
                 StringBuffer sb = new StringBuffer();
 85  27
                 sb.append("required argument -");
 86  27
                 sb.append(a.getShortName());
 87  27
                 sb.append(", --");
 88  27
                 sb.append(a.getLongName());
 89  27
                 sb.append(" not found");
 90  
 
 91  27
                 throw new CommandLineParseException(sb.toString());
 92  
             }
 93  355
         }
 94  355
     }
 95  
 }