KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > CommandLine


1 /**
2  * com.mckoi.util.CommandLine 03 Aug 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 import java.util.Vector JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 /**
31  * Used to parse a command-line.
32  *
33  * @author Tobias Downer
34  */

35
36 public class CommandLine {
37
38   /**
39    * The command line arguments.
40    */

41   private String JavaDoc[] args;
42
43   /**
44    * Constructs the command line parser from the String[] array passed as the
45    * argument to the application.
46    */

47   public CommandLine(String JavaDoc[] args) {
48     if (args == null) {
49       args = new String JavaDoc[0];
50     }
51     this.args = args;
52   }
53
54   /**
55    * Returns true if the switch is in the command line.
56    * eg. command_line.containsSwitch("--help");
57    */

58   public boolean containsSwitch(String JavaDoc switch_str) {
59     for (int i = 0; i < args.length; ++i) {
60       if (args[i].equals(switch_str)) {
61         return true;
62       }
63     }
64     return false;
65   }
66
67   /**
68    * Given a comma deliminated list, this scans for one of the switches in the
69    * list. eg. command_line.containsSwitchFrom("--help,-help,-h");
70    */

71   public boolean containsSwitchFrom(String JavaDoc switch_str) {
72     StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(switch_str, ",");
73     while (tok.hasMoreElements()) {
74       String JavaDoc elem = tok.nextElement().toString();
75       if (containsSwitch(elem)) {
76         return true;
77       }
78     }
79     return false;
80   }
81
82   /**
83    * Returns true if the command line contains a switch starting with the
84    * given string. eg. command_line.containsSwitchStart("-he"); will match
85    * "-hello", "-help", "-her", etc
86    */

87   public boolean containsSwitchStart(String JavaDoc switch_str) {
88     for (int i = 0; i < args.length; ++i) {
89       if (args[i].startsWith(switch_str)) {
90         return true;
91       }
92     }
93     return false;
94   }
95
96   /**
97    * Returns a list of all switches on the command line that start with the
98    * given string. eg. command_line.allSwitchesStartingWith("-D"); will
99    * return matches for switches "-Dname=toby", "-Dog", "-Dvalue=over", etc.
100    */

101   public String JavaDoc[] allSwitchesStartingWith(String JavaDoc switch_str) {
102     Vector JavaDoc list = new Vector JavaDoc();
103     for (int i = 0; i < args.length; ++i) {
104       if (args[i].startsWith(switch_str)) {
105         list.addElement(args[i]);
106       }
107     }
108     return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
109   }
110
111   /**
112    * Returns the contents of a switch variable if the switch is found on the
113    * command line. A switch variable is of the form '-[switch] [variable]'.
114    * eg. 'command.exe -url "http://www.mckoi.com/database/"'.
115    * <p>
116    * Returns 'null' if the argument was not found.
117    */

118   public String JavaDoc switchArgument(String JavaDoc switch_str) {
119     for (int i = 0; i < args.length - 1; ++i) {
120       if (args[i].equals(switch_str)) {
121         return args[i + 1];
122       }
123     }
124     return null;
125   }
126
127   /**
128    * Returns the contents of a switch variable if the switch is found on the
129    * command line. A switch variable is of the form '-[switch] [variable]'.
130    * eg. 'command.exe -url "http://www.mckoi.com/database/"'.
131    * <p>
132    * Returns def if the argument was not found.
133    */

134   public String JavaDoc switchArgument(String JavaDoc switch_str, String JavaDoc def) {
135     String JavaDoc arg = switchArgument(switch_str);
136     if (arg == null) {
137       return def;
138     }
139     return arg;
140   }
141
142   /**
143    * Returns the contents of a set of arguments found after a switch on the
144    * command line. For example, switchArguments("-create", 3) would try and
145    * find the '-create' switch and return the first 3 arguments after it if
146    * it can.
147    * <p>
148    * Returns null if no match is found.
149    */

150   public String JavaDoc[] switchArguments(String JavaDoc switch_str, int arg_count) {
151     for (int i = 0; i < args.length - 1; ++i) {
152       if (args[i].equals(switch_str)) {
153         if (i + arg_count < args.length) {
154           String JavaDoc[] ret_list = new String JavaDoc[arg_count];
155           for (int n = 0; n < arg_count; ++n) {
156             ret_list[n] = args[i + n + 1];
157           }
158           return ret_list;
159         }
160       }
161     }
162     return null;
163   }
164
165
166 }
167
Popular Tags