KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sizewhere > Main


1 package sizewhere;
2
3 import java.io.File JavaDoc;
4
5 import org.apache.commons.cli.CommandLine;
6 import org.apache.commons.cli.CommandLineParser;
7 import org.apache.commons.cli.GnuParser;
8 import org.apache.commons.cli.HelpFormatter;
9 import org.apache.commons.cli.Option;
10 import org.apache.commons.cli.OptionBuilder;
11 import org.apache.commons.cli.Options;
12 import org.apache.commons.cli.ParseException;
13
14 public class Main {
15     private static Options getOptions() {
16         Option dir = OptionBuilder.withArgName( "dir" )
17             .hasArg()
18             .withDescription( "give total size of files in given dir" )
19             .create( "dir" );
20         Option name = OptionBuilder.withArgName( "name" )
21             .hasArg()
22             .withDescription( "give total size of files with given name" )
23             .create( "name" );
24         Options options = new Options();
25
26         options.addOption(dir);
27         options.addOption(name);
28         
29         return options;
30     }
31     
32     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
33       Options options = getOptions();
34       try {
35         
36         CommandLineParser parser = new GnuParser();
37
38         CommandLine line = parser.parse( options, args );
39         File JavaDoc dir = new File JavaDoc(line.getOptionValue("dir", "."));
40         String JavaDoc name = line.getOptionValue("name", "jar");
41         System.out.println("total size of files in "+dir+" containing "+name+": "+SizeWhere.totalSize(dir, name));
42       } catch( ParseException exp ) {
43           // oops, something went wrong
44
System.err.println( "Parsing failed. Reason: " + exp.getMessage() );
45           
46         HelpFormatter formatter = new HelpFormatter();
47         formatter.printHelp( "sizewhere", options );
48       }
49     }
50             
51 }
52
Popular Tags