1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.finder;
18
19 import org.apache.commons.cli2.*;
20 import org.apache.commons.cli2.builder.*;
21 import org.apache.commons.cli2.commandline.Parser;
22
23 public class Find {
24
25 public static void main(String[] args) throws OptionException {
26 new Find().find(args);
27 }
28
29 public void find(String[] args) throws OptionException {
30 GroupBuilder gbuilder = new GroupBuilder();
31
32 Option type = optionWithArg("type", "True if the file is of the specified type.");
33 Option empty = option("empty", "True if the current file or directory is empty.");
34 Option regex = optionWithArg("regex", "True if the whole path of the file matches pattern using regular expression.");
35 Option name = optionWithArg("name", "True if the last component of the pathname being examined matches pattern.");
36 Option path = optionWithArg("path", "True if the pathname being examined matches pattern.");
37
38 Group options =
39 gbuilder
40 .withName("options")
41 .withOption(type)
42 .withOption(empty)
43 .withOption(regex)
44 .withOption(name)
45 .withOption(path)
46 .create();
47
48 Parser parser = new Parser();
49 parser.setGroup(options);
50 CommandLine cl = parser.parse(args);
51
52 if(cl.hasOption(type)) {
53 System.out.println("USE TYPE: " + cl.getValue(type));
54 }
55 }
56
57
58 private DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
59 private ArgumentBuilder abuilder = new ArgumentBuilder();
60 private Option option(String name, String description) {
61 Option type =
62 obuilder
63 .withShortName(name)
64 .withDescription(description)
65 .create();
66 return type;
67 }
68 private Option optionWithArg(String name, String description) {
69 Option type =
70 obuilder
71 .withShortName(name)
72 .withDescription(description)
73 .withArgument(
74 abuilder
75 .withName(name + "-flag")
76 .withMinimum(1)
77 .withMaximum(1)
78 .create()
79 )
80 .create();
81 return type;
82 }
83
84 }