| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ParserRules |
|
| 1.875;1.875 |
| 1 | /* | |
| 2 | Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. | |
| 3 | Licensed under the Academic Free License version 3.0 | |
| 4 | */ | |
| 5 | ||
| 6 | package joptsimple; | |
| 7 | ||
| 8 | import java.util.Iterator; | |
| 9 | import java.util.List; | |
| 10 | ||
| 11 | /** | |
| 12 | * <p>Can tell whether or not options are well-formed.</p> | |
| 13 | * | |
| 14 | * @since 1.0 | |
| 15 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
| 16 | * @version $Id: ParserRules.java,v 1.3 2008/04/08 03:20:18 pholser Exp $ | |
| 17 | */ | |
| 18 | class ParserRules { | |
| 19 | static final char HYPHEN_CHAR = '-'; | |
| 20 | 2 | static final String HYPHEN = String.valueOf( HYPHEN_CHAR ); |
| 21 | static final String DOUBLE_HYPHEN = "--"; | |
| 22 | static final String OPTION_TERMINATOR = DOUBLE_HYPHEN; | |
| 23 | static final String RESERVED_FOR_EXTENSIONS = "W"; | |
| 24 | ||
| 25 | 2 | protected ParserRules() { |
| 26 | 2 | throw new UnsupportedOperationException(); |
| 27 | } | |
| 28 | ||
| 29 | static boolean isShortOptionToken( String argument ) { | |
| 30 | 450 | return argument.startsWith( HYPHEN ) |
| 31 | && !HYPHEN.equals( argument ) | |
| 32 | && !isLongOptionToken( argument ); | |
| 33 | } | |
| 34 | ||
| 35 | static boolean isLongOptionToken( String argument ) { | |
| 36 | 860 | return argument.startsWith( DOUBLE_HYPHEN ) && !isOptionTerminator( argument ); |
| 37 | } | |
| 38 | ||
| 39 | static boolean isOptionTerminator( String argument ) { | |
| 40 | 576 | return OPTION_TERMINATOR.equals( argument ); |
| 41 | } | |
| 42 | ||
| 43 | static void checkLegalOption( String option ) { | |
| 44 | 748 | if ( option.startsWith( HYPHEN ) ) |
| 45 | 4 | throw new IllegalOptionSpecificationException( String.valueOf( option ) ); |
| 46 | ||
| 47 | 3134 | for ( int i = 0; i < option.length(); ++i ) |
| 48 | 2404 | checkLegalOptionCharacter( option.charAt( i ) ); |
| 49 | 730 | } |
| 50 | ||
| 51 | static void checkLegalOptions( List options ) { | |
| 52 | 560 | for ( Iterator iter = options.iterator(); iter.hasNext(); ) |
| 53 | 680 | checkLegalOption( (String) iter.next() ); |
| 54 | 540 | } |
| 55 | ||
| 56 | private static void checkLegalOptionCharacter( char option ) { | |
| 57 | 2404 | if ( !( Character.isLetterOrDigit( option ) || isAllowedPunctuation( option ) ) ) |
| 58 | 4 | throw new IllegalOptionSpecificationException( String.valueOf( option ) ); |
| 59 | 2400 | } |
| 60 | ||
| 61 | private static boolean isAllowedPunctuation( char option ) { | |
| 62 | 164 | String allowedPunctuation = "?." + HYPHEN_CHAR; |
| 63 | 164 | return allowedPunctuation.indexOf( option ) != -1; |
| 64 | } | |
| 65 | } |