Statistics
| Revision:

root / tmp / org.txm.libs.args4j / src / org / kohsuke / args4j / package.html @ 2398

History | View | Annotate | Download (2.9 kB)

1
<html>
2
<body>
3
        <p>Core classes of the Args4J command line parser.</p>
4

    
5
        <h1>What is Args4J</h1>
6
        <p>Args4J is a command line parser. As such a parser its job is to
7
                parse the String-array passed to the main() method and transfer the
8
                values to a java object, which includes type conversion. The entry
9
                point into this parsing is the CmdLineParser class with its parse()
10
                Method.</p>
11
        <p>Args4J must know the mapping between the flag from the command
12
                line and the target for the value. There are multiple ways for Args4J:
13
        
14
        <ul>
15
                <li>place an @Option or @Argument annotation at a setter or on a
16
                        field</li>
17
                <li>provide a XML configuration file</li>
18
                <li>make all fields public available</li>
19
        </ul>
20
        depending on what you want, you have to do a configuration step before
21
        starting the parsing.
22
        </p>
23

    
24
        <h1>Examples</h1>
25

    
26
        <p>
27
                <tt>java Main -text newText</tt>
28
        </p>
29

    
30
        <p>
31
                The standard use case is having a bean class and providing the
32
                annotations. This feature is available since the first Args4J release:
33
                <!-- TODO: 
34
Javadoc stops when reaching the at-sign. When I load the package.html directly into Firefox,
35
all is fine. But when creating the javadoc it stops copying the content at that sign.
36
(a) is a compromise here ...
37
Does anyone know how to handle that?
38
-->
39
        <pre>
40
public class Bean {
41
    (a)Option(name="-text")
42
    String text;
43
}
44
public class Main {
45
    public static void main(String[] args) throws CmdLineException {
46
        Object bean = new Bean();
47
        CmdLineParser parser = new CmdLineParser(bean);
48
        parser.parse(args);
49
    }
50
}
51
</pre>
52

    
53
        <p>An easy way for initializing fields and not touching the bean
54
                source code is using the FieldParser. The FieldParser scans all fields
55
                of the bean class (including inheritance) and makes them public
56
                available as options with a '-' prefix in the name. This feature is
57
                available since Args4J release 2.0.16:
58
        <pre>
59
public class Bean {
60
    String text;
61
}
62
public class Main {
63
    public static void main(String[] args) throws CmdLineException {
64
        Object bean = new Bean();
65
        CmdLineParser parser = new CmdLineParser(null);
66
        new FieldParser().parse(parser, bean);
67
        parser.parse(args);
68
    }
69
}
70
</pre>
71

    
72
        <p>While the FieldParser is easier to use, the XmlParser supports
73
                more features. That said it supports all features which are available
74
                via annotations: usage text, specifying handlers and more. You have to
75
                provide an XML InputSource or an URL to the XML file. This feature is
76
                available since Args4J release 2.0.16:
77
        <pre>
78
public class Bean {
79
    String text;
80
}
81
public class Main {
82
    public static void main(String[] args) throws CmdLineException {
83
        Object bean = new Bean();
84
        CmdLineParser parser = new CmdLineParser(null);
85
        new XmlParser().parse(getClass().getResource(bean.getClass().getName() + ".xml"), parser, bean);
86
        parser.parse(args);
87
    }
88
}
89
&lt;args>
90
  &lt;option field="text" name="-text" usage="Output text"/>
91
&lt;/args>
92
</pre>
93
        </p>
94

    
95

    
96
</body>
97
</html>