1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package mk.fsgrep.util;
26
27 import java.io.File;
28 import java.util.regex.*;
29
30 import mk.fsgrep.util.output.OutputDestination;
31 import mk.fsgrep.util.output.ConsoleOutput;
32 import mk.fsgrep.util.output.FileOutput;
33 import mk.fsgrep.util.output.NullOutput;
34
35 /***
36 * A simple bean that holds all the invokation parameters.
37 * It contains logic to instantiate itself from the command line arguments.
38 *
39 * @author Murali Krishnan
40 *
41 */
42 public class Args {
43
44
45
46
47 public static final String KEY_DEFAULT_DIR = "DefaultDir";
48 public static final String KEY_DEFAULT_SUFFIX = "DefaultSuffix";
49 public static final String KEY_RESULT_FILE = "ResultFile";
50
51 private static final Pattern RE_CYGWIN = Pattern.compile("^/((cygdrive/)?(//w))/", Pattern.CASE_INSENSITIVE);
52
53
54
55
56
57
58
59
60
61
62 private String _root = ".";
63 private String _suffix = "java";
64 private boolean _caseSensitive = true;
65 private String _pattern = "";
66 private boolean _showUsage = false;
67 private boolean _showVersion = false;
68 private boolean _listFileMatches = false;
69 private boolean _listFilesWithoutMatch = false;
70 private boolean _searchFileNames = false;
71 private boolean _quiet = false;
72 private String _resultFile = null;
73 private boolean _searchingComments = false;
74 private boolean _usingGui = false;
75
76
77
78
79
80
81 public Args(String[] args) {
82 setDefaults(AppProperties.readDefaults());
83
84 parse(args);
85 }
86
87
88 /***
89 * Principly for testing.
90 *
91 */
92 public Args() {
93 }
94
95
96
97
98
99 public String getRoot() {return _root;}
100 public String getSuffix() {return _suffix;}
101 public boolean isCaseSensitive() {return _caseSensitive;}
102 public String getPattern() {return _pattern;}
103 public boolean getShowUsage() {return _showUsage;}
104 public boolean getShowVersion() {return _showVersion;}
105 public boolean isListFileMatches() {return _listFileMatches;}
106 public boolean isListFilesWithoutMatch() {return _listFilesWithoutMatch;}
107 public boolean isSearchFileNames() {return _searchFileNames;}
108 public boolean isQuiet() {return _quiet;}
109 public String getResultFile() {return _resultFile;}
110 public boolean isSearchingComments() {return _searchingComments;}
111 public boolean isUsingGui() {return _usingGui;}
112
113
114
115
116
117
118 public void setRoot(String val) {_root=val;}
119 public void setSuffix(String val) {_suffix=val;}
120 public void setCaseSensitive(boolean val) {_caseSensitive=val;}
121 public void setPattern(String val) {_pattern=val;}
122 public void setShowUsage(boolean val) {_showUsage=val;}
123 public void setShowVersion(boolean val) {_showVersion=val;}
124 public void setListFileMatches(boolean val) {_listFileMatches=val;}
125 public void setListFilesWithoutMatch(boolean val) {_listFilesWithoutMatch=val;}
126 public void setSearchFileNames(boolean val) {_searchFileNames=val;}
127 public void setQuiet(boolean val) {_quiet=val;}
128 public void setResultFile(String val) {_resultFile=val;}
129 public void setSearchingComments(boolean val) {_searchingComments=val;}
130 public void setUsingGui(boolean val) {_usingGui=val;}
131
132
133
134
135
136 protected void setDefaults(AppProperties props)
137 {
138 String defaultDir = props.getProperty(KEY_DEFAULT_DIR);
139 if (defaultDir != null) {
140 setRoot(defaultDir);
141 }
142
143 String defaultSuffix = props.getProperty(KEY_DEFAULT_SUFFIX);
144 if (defaultSuffix != null) {
145 setSuffix(defaultSuffix);
146 }
147
148 String file = props.getProperty(KEY_RESULT_FILE);
149 if (file != null) {
150 setResultFile(file);
151 }
152 }
153
154
155 protected void parse(String[] args) {
156 for (int i = 0; i < args.length; i++) {
157 String arg = args[i];
158
159 if ("-d".equals(arg) || "--dir".equals(arg)) {
160 String root = args[++i];
161
162 {
163
164
165 File file = new File(root);
166 if (!file.exists()) {
167 Matcher matcher = RE_CYGWIN.matcher(root);
168 if (matcher.find()) {
169 String windowsName = matcher.replaceFirst("$3:/");
170 file = new File(windowsName);
171 if (file.exists()) {
172 root = windowsName;
173 }
174 }
175 }
176 }
177
178 setRoot(root);
179 } else if ("-s".equals(arg) || "--suffix".equals(arg)) {
180 setSuffix(args[++i]);
181 } else if ("-t".equals(arg) || "--all-text-files".equals(arg)) {
182 setSuffix("*");
183 } else if ("-f".equals(arg) || "--result-file".equals(arg)) {
184 String name = args[++i];
185 if ("-".equals(name)) {
186
187 name = null;
188 }
189 setResultFile(name);
190 } else if ("-i".equals(arg) || "--ignore-case".equals(arg)) {
191 setCaseSensitive(false);
192 } else if ("-l".equals(arg) || "--files-with-matches".equals(arg)){
193 setListFileMatches(true);
194 } else if ("-L".equals(arg) || "--files-without-match".equals(arg)){
195 setListFileMatches(true);
196 setListFilesWithoutMatch(true);
197 } else if ("-n".equals(arg) || "--search-file-names".equals(arg)) {
198 setSearchFileNames(true);
199 setListFileMatches(true);
200 } else if ("-C".equals(arg) || "--search-comments".equals(arg)) {
201 setSearchingComments(true);
202 } else if ("-G".equals(arg) || "--gui".equals(arg)) {
203 setUsingGui(true);
204 } else if ("-q".equals(arg) || "--quiet".equals(arg) || "--silent".equals(arg)) {
205 setQuiet(true);
206 } else if ("-h".equals(arg) || "--help".equals(arg)) {
207 setShowUsage(true);
208 } else if ("-V".equals(arg) || "--version".equals(arg)) {
209 setShowVersion(true);
210 } else if (arg.startsWith("-")) {
211 System.err.println("Unrecognized flag '" + arg + "'");
212 setShowUsage(true);
213 } else {
214 if ("".equals(getPattern())) {
215 setPattern(arg);
216 } else {
217 setPattern(getPattern() + "//s+" + arg);
218 }
219 }
220 }
221 }
222
223
224
225
226
227 public boolean launchGui() {
228 boolean result = isUsingGui() || "".equals(getPattern());
229
230 return result;
231 }
232
233
234 public OutputDestination createReportOutput() {
235 OutputDestination result = new NullOutput();
236
237 if (!isQuiet()) {
238 result = new ConsoleOutput();
239 }
240
241 return result;
242 }
243
244
245 public OutputDestination createResultOutput() {
246 OutputDestination result = new ConsoleOutput();
247
248 if (getResultFile() != null) {
249 result = new FileOutput(getResultFile());
250 }
251
252 return result;
253 }
254
255
256 public OutputDestination createFileOutput() {
257 OutputDestination result = new NullOutput();
258
259 if (getResultFile() != null) {
260 result = new FileOutput(getResultFile());
261 }
262
263 return result;
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 }