1 37 38 package net.sourceforge.cruisecontrol.util; 39 40 43 public final class MainArgs { 44 private MainArgs() { } 45 46 public static final int NOT_FOUND = -1; 47 48 public static int parseInt(String [] args, String argName, int defaultIfNoParam, int defaultIfNoValue) { 49 String intString = parseArgument(args, 50 argName, 51 Integer.toString(defaultIfNoParam), 52 Integer.toString(defaultIfNoValue)); 53 try { 54 return Integer.parseInt(intString); 55 } catch (NumberFormatException e) { 56 throw new IllegalArgumentException ( 57 "-" + argName + " parameter, specified as '" + intString + "', requires integer argument"); 58 } 59 } 60 61 81 public static String parseArgument(String [] args, String argName, 82 String defaultIfNoParam, String defaultIfNoValue) { 83 int argIndex = findIndex(args, argName); 84 if (argIndex == NOT_FOUND) { 85 return defaultIfNoParam; 86 } 87 if (argIndex == args.length - 1 || args[argIndex + 1].charAt(0) == '-' ) { 92 return defaultIfNoValue; 93 } 94 return args[argIndex + 1]; 95 } 96 97 public static int findIndex(String [] args, String argName) { 98 99 String searchString = "-" + argName; 100 for (int i = 0; i < args.length; i++) { 101 if (args[i].equals(searchString)) { 102 return i; 103 } 104 } 105 return NOT_FOUND; 106 } 107 108 public static boolean argumentPresent(String [] args, String argName) { 109 return findIndex(args, argName) != NOT_FOUND; 110 } 111 } 112 | Popular Tags |