|
| 1 | +package bitpal; |
| 2 | + |
| 3 | +import gnu.getopt.Getopt; |
| 4 | +import gnu.getopt.LongOpt; |
| 5 | + |
| 6 | + |
| 7 | +import java.math.BigInteger; |
| 8 | + |
| 9 | +/** |
| 10 | + * 使用gnu.getopt 处理命令行参数, |
| 11 | + * |
| 12 | + * <dependency> |
| 13 | + * <groupId>gnu.getopt</groupId> |
| 14 | + * <artifactId>java-getopt</artifactId> |
| 15 | + * <version>1.0.13</version> |
| 16 | + * </dependency> |
| 17 | + * |
| 18 | + * Created by zhangjikai on 16-3-14. |
| 19 | + */ |
| 20 | +public class GnuGetOpt { |
| 21 | + |
| 22 | + |
| 23 | + public static void printHelp() { |
| 24 | + StringBuilder builder = new StringBuilder(); |
| 25 | + builder.append("Options:\n\n"); |
| 26 | + builder.append(String.format("%-30s", "-M,--match <arg>:")); |
| 27 | + builder.append("Specify the match score which should be positive or zero. Default is 2.\n\n"); |
| 28 | + builder.append(String.format("%-30s", "-I,--mismatch <arg>: ")); |
| 29 | + builder.append("Specify the mismatch score which should be negative. Default is -3.\n\n"); |
| 30 | + builder.append(String.format("%-30s", "-G,--gap <arg>: ")); |
| 31 | + builder.append("Specify the gap score which should be negative. Default is -5.\n\n"); |
| 32 | + builder.append(String.format("%-30s", "-d,--directory <arg>: ")); |
| 33 | + builder.append("Specify the directory where to place generated source files.\n\n"); |
| 34 | + builder.append(String.format("%-30s", "-h,--help: ")); |
| 35 | + builder.append("Display help Information.\n\n"); |
| 36 | + builder.append(String.format("%-30s", "-n,--name <arg>: ")); |
| 37 | + builder.append("Set the name of generated source file.\n\n"); |
| 38 | + builder.append(String.format("%-30s", "-t,--type <arg>: ")); |
| 39 | + builder.append("Set the type of generated files. Valid values are: cpu, mic, avx2. Default is cpu.\n\n"); |
| 40 | + System.out.println(builder.toString()); |
| 41 | + } |
| 42 | + |
| 43 | + public static void handleArgs(String[] args) { |
| 44 | + LongOpt longOpts[] = new LongOpt[7]; |
| 45 | + longOpts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'); |
| 46 | + longOpts[1] = new LongOpt("directory", LongOpt.REQUIRED_ARGUMENT, null, 'd'); |
| 47 | + longOpts[2] = new LongOpt("name", LongOpt.REQUIRED_ARGUMENT, null, 'n'); |
| 48 | + longOpts[3] = new LongOpt("match", LongOpt.REQUIRED_ARGUMENT, null, 'M'); |
| 49 | + longOpts[4] = new LongOpt("mismatch", LongOpt.REQUIRED_ARGUMENT, null, 'I'); |
| 50 | + longOpts[5] = new LongOpt("gap", LongOpt.REQUIRED_ARGUMENT, null, 'G'); |
| 51 | + longOpts[6] = new LongOpt("type", LongOpt.REQUIRED_ARGUMENT, null, 't'); |
| 52 | + |
| 53 | + int c; |
| 54 | + int num; |
| 55 | + String arg; |
| 56 | + Getopt opt = new Getopt("bitpal", args, "M:I:G:d:hn:t", longOpts); |
| 57 | + |
| 58 | + while ((c = opt.getopt()) != -1) { |
| 59 | + switch (c) { |
| 60 | + case 'M': |
| 61 | + arg = opt.getOptarg(); |
| 62 | + try { |
| 63 | + num = Integer.parseInt(arg); |
| 64 | + } catch (Exception e) { |
| 65 | + System.err.println("-M: You should input an integer."); |
| 66 | + System.exit(-1); |
| 67 | + } |
| 68 | + break; |
| 69 | + |
| 70 | + case 'I': |
| 71 | + arg = opt.getOptarg(); |
| 72 | + try { |
| 73 | + num = Integer.parseInt(arg); |
| 74 | + } catch (Exception e) { |
| 75 | + System.err.println("-I: You should input an integer."); |
| 76 | + System.exit(-1); |
| 77 | + } |
| 78 | + break; |
| 79 | + |
| 80 | + case 'G': |
| 81 | + arg = opt.getOptarg(); |
| 82 | + try { |
| 83 | + num = Integer.parseInt(arg); |
| 84 | + } catch (Exception e) { |
| 85 | + System.err.println("-G: You should input an integer."); |
| 86 | + System.exit(-1); |
| 87 | + } |
| 88 | + break; |
| 89 | + |
| 90 | + case 'd': |
| 91 | + arg = opt.getOptarg(); |
| 92 | + break; |
| 93 | + case 'h': |
| 94 | + printHelp(); |
| 95 | + break; |
| 96 | + case 'n': |
| 97 | + arg = opt.getOptarg(); |
| 98 | + break; |
| 99 | + case 't': |
| 100 | + arg = opt.getOptarg(); |
| 101 | + if (!"cpumicavx2".contains(arg)) { |
| 102 | + System.err.println("-t: Valid values are {cpu, mic, avx2}"); |
| 103 | + System.exit(-1); |
| 104 | + } |
| 105 | + break; |
| 106 | + case '?': |
| 107 | + System.err.println("The option '" + (char) opt.getOptopt() + "' is not valid"); |
| 108 | + System.exit(-1); |
| 109 | + break; |
| 110 | + default: |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public static void main(String[] args) { |
| 117 | + |
| 118 | + handleArgs(args); |
| 119 | + |
| 120 | + } |
| 121 | +} |
0 commit comments