-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineUtilities.java
More file actions
140 lines (123 loc) · 4.41 KB
/
CommandLineUtilities.java
File metadata and controls
140 lines (123 loc) · 4.41 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package cs475;
import java.io.*;
import java.util.*;
import org.apache.commons.cli.*;
/**
* Some utilities for wrapping the Apache Jakarta CLI for command line parsing.
* @author Mark Dredze
*
*/
public class CommandLineUtilities {
private static CommandLine _command_line = null;
private static Properties _properties = null;
/**
* This uses the apache Jakarta CLI to parse the command line.
* A single static instance of this class exists for global access by all parts
* of the program.
* To use this class, a list of options must be specified and passed to this method.
* Mandatory arguments should be encoded as strings.
* @param args The command line received by main
* @param manditory_args A list of strings that contain the names of mandatory arguments.
* @param specified_options A list of options to use for this program.
*/
public static void initCommandLineParameters(String[] args,
LinkedList<Option> specified_options, String[] manditory_args) {
Options options = new Options();
if (specified_options != null)
for (Option option : specified_options)
options.addOption(option);
Option option = null;
OptionBuilder.withArgName("file");
OptionBuilder.hasArg();
OptionBuilder
.withDescription("A file containing command line parameters as a Java properties file.");
option = OptionBuilder.create("parameter_file");
options.addOption(option);
CommandLineParser command_line_parser = new GnuParser();
CommandLineUtilities._properties = new Properties();
try {
CommandLineUtilities._command_line = command_line_parser.parse(
options, args);
} catch (ParseException e) {
System.out.println("***ERROR: " + e.getClass() + ": "
+ e.getMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("parameters:", options);
System.exit(0);
}
if (CommandLineUtilities.hasArg("parameter_file")) {
String parameter_file = CommandLineUtilities.getOptionValue("parameter_file");
// Read the property file.
try {
_properties.load(new FileInputStream(parameter_file));
} catch (IOException e) {
System.err.println("Problem reading parameter file: " + parameter_file);
}
}
boolean failed = false;
if (manditory_args != null) {
for (String arg : manditory_args) {
if (!CommandLineUtilities.hasArg(arg)) {
failed = true;
System.out.println("Missing argument: " + arg);
}
}
if (failed) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("parameters:", options);
System.exit(0);
}
}
}
public static boolean hasArg(String option) {
if (CommandLineUtilities._command_line.hasOption(option) ||
CommandLineUtilities._properties.containsKey(option))
return true;
return false;
}
/**
* This method is used when there is one argument for the option presented as a ";" separated list.
* @param option
* @return
*/
public static String[] getOptionValues(String option) {
// Try and parse the list on the ";" separator.
String arguments_to_parse = null;
if (CommandLineUtilities._command_line.hasOption(option))
arguments_to_parse = CommandLineUtilities._command_line.getOptionValue(option);
if (CommandLineUtilities._properties.containsKey(option)) {
arguments_to_parse = (String)CommandLineUtilities._properties.getProperty(option);
}
return arguments_to_parse.split(":");
}
public static String getOptionValue(String option) {
if (CommandLineUtilities._command_line.hasOption(option))
return CommandLineUtilities._command_line.getOptionValue(option);
if (CommandLineUtilities._properties.containsKey(option))
return (String)CommandLineUtilities._properties.getProperty(option);
return null;
}
public static int getOptionValueAsInt(String option) {
String value = CommandLineUtilities.getOptionValue(option);
if (value != null)
return Integer.parseInt(value);
return -1;
}
public static float getOptionValueAsFloat(String option) {
String value = CommandLineUtilities.getOptionValue(option);
if (value != null)
return Float.parseFloat(value);
return -1;
}
/**
* Adding this option and value make it appear as if it was in the parameter
* file.
* @param option
*/
public static void addCommandLineVariable(String option, String value) {
if (CommandLineUtilities._properties == null) {
CommandLineUtilities._properties = new Properties();
}
CommandLineUtilities._properties.put(option, value);
}
}