View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      // Defined outside option(...) for performance
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  }