JArgs命令行选项解析->Java套件

项目简介和意图

这个小的工程是为java开发者提供的,使用命令行方便的,结构紧凑的解析器工具。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
public class OptionTest {
    private static void printUsage() {
        System.err
                .println("Usage: OptionTest [-d,--debug] [{-v,--verbose}] [{--alt}] [{--name} a_name]\n"
                        + "                  [{-s,--size} a_number] [{-f,--fraction} a_float] [a_nother]");
    }
 
    public static void main(String[] args) {
        // First, you must create a CmdLineParser, and add to it the
        // appropriate Options.
        // To start with, we add the Options -d, -v, -s, and -f, with aliases
        // --debug, --verbose, --size, and --fraction respectively.
        // The -d and -v options have no associated value -- they are either
        // present, or they are not. The -s and -f options take integer and
        // double-precision floating-point values respectively.
        CmdLineParser parser = new CmdLineParser();
        CmdLineParser.Option debug = parser.addBooleanOption('d', "debug");
        CmdLineParser.Option verbose = parser.addBooleanOption('v', "verbose");
        CmdLineParser.Option size = parser.addIntegerOption('s', "size");
        CmdLineParser.Option fraction = parser.addDoubleOption('f', "fraction");
 
        // Options may have just a long form with no corresponding short form.
        // Here, we add --alt and --name options.
        CmdLineParser.Option alt = parser.addBooleanOption("alt");
        CmdLineParser.Option name = parser.addStringOption('n', "name");
 
        // Next, you must parse the user-provided command line arguments, and
        // catch any errors therein.
        // Options may appear on the command line in any order, and may even
        // appear after some or all of the non-option arguments.
        // If the user needs to specify non-option arguments that start with a
        // minus, then they may indicate the end of the parsable options with
        // -- , like this:
        // prog -f 20 -- -10 -fred
        // The -f 20 will be parsed as the fraction option, with the value 20.
        // The -10 and -fred arguments will be regarded as non-option
        // arguments, and passed through getRemainingArgs as unparsed Strings.
        // Short boolean options may be specified separately (-d -v) or
        // together (-dv).
        // Options with values may be given on the command line as -f 1.0 or
        // --fraction=1.0.
        try {
            parser.parse(args);
        } catch (CmdLineParser.OptionException e) {
            System.err.println(e.getMessage());
            printUsage();
            System.exit(2);
        }
        // For options that may be specified only zero or one time, the value
        // of that option may be extracted as shown below. If the options
        // were not specified, the corresponding values will be null.
 
        Boolean debugValue = (Boolean) parser.getOptionValue(debug);
        String nameValue = (String) parser.getOptionValue(name);
        // Alternatively, you may specify a default value. This will be
        // returned (instead of null) when the command line argument is
        // missing.
 
        Boolean altValue = (Boolean) parser.getOptionValue(alt, Boolean.FALSE);
        Integer sizeValue = (Integer) parser.getOptionValue(size, new Integer(
                42));
 
        // If your application requires it, options may be specified more than
        // once. In this case, you may get all the values specified by the
        // user, as a Vector:
 
        Vector fractionValues = parser.getOptionValues(fraction);
 
        // Alternatively, you may make the loop explicit:
 
        int verbosity = 0;
        while (true) {
            Boolean verboseValue = (Boolean) parser.getOptionValue(verbose);
            if (verboseValue == null) {
                break;
            } else {
                verbosity++;
            }
        }
        // The remaining command-line arguments -- those that do not start
        // with a minus sign -- can be captured like this:
 
        String[] otherArgs = parser.getRemainingArgs();
 
        // For testing purposes, we just print out the option values and
        // remaining command-line arguments. In a real program, of course,
        // one would pass them to a function that does something more useful.
 
        System.out.println("debug: " + debugValue);
        System.out.println("alt: " + altValue);
        System.out.println("size: " + sizeValue);
        System.out.println("name: " + nameValue);
        System.out.println("verbosity: " + verbosity);
        Enumeration e = fractionValues.elements();
        while (e.hasMoreElements()) {
            System.out.println("fraction: " + (Double) e.nextElement());
        }
        System.out.println("remaining args: ");
        for (int i = 0; i < otherArgs.length; ++i) {
            System.out.println(otherArgs[i]);
        }
        System.exit(0);
    }
}

 使用JArgs的项目有JBoss,Columba等。

 

总结一下就是这是一个java的命令解析的小的工程,可以方便的解析各种输入的参数信息。在YUICompressor项目中应用到了这个小的套件。

 

posted @   skyme  阅读(2742)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示