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 | 0 | public class Find { |
24 | |
|
25 | |
public static void main(String[] args) throws OptionException { |
26 | 0 | new Find().find(args); |
27 | 0 | } |
28 | |
|
29 | |
public void find(String[] args) throws OptionException { |
30 | 0 | GroupBuilder gbuilder = new GroupBuilder(); |
31 | |
|
32 | 0 | Option type = optionWithArg("type", "True if the file is of the specified type."); |
33 | 0 | Option empty = option("empty", "True if the current file or directory is empty."); |
34 | 0 | Option regex = optionWithArg("regex", "True if the whole path of the file matches pattern using regular expression."); |
35 | 0 | Option name = optionWithArg("name", "True if the last component of the pathname being examined matches pattern."); |
36 | 0 | Option path = optionWithArg("path", "True if the pathname being examined matches pattern."); |
37 | |
|
38 | 0 | 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 | 0 | Parser parser = new Parser(); |
49 | 0 | parser.setGroup(options); |
50 | 0 | CommandLine cl = parser.parse(args); |
51 | |
|
52 | 0 | if(cl.hasOption(type)) { |
53 | 0 | System.out.println("USE TYPE: " + cl.getValue(type)); |
54 | |
} |
55 | 0 | } |
56 | |
|
57 | |
|
58 | 0 | private DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); |
59 | 0 | private ArgumentBuilder abuilder = new ArgumentBuilder(); |
60 | |
private Option option(String name, String description) { |
61 | 0 | Option type = |
62 | |
obuilder |
63 | |
.withShortName(name) |
64 | |
.withDescription(description) |
65 | |
.create(); |
66 | 0 | return type; |
67 | |
} |
68 | |
private Option optionWithArg(String name, String description) { |
69 | 0 | 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 | 0 | return type; |
82 | |
} |
83 | |
|
84 | |
} |