/
OptionParserFactory.java
66 lines (53 loc) · 2.4 KB
/
OptionParserFactory.java
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
/* Copyright 2009 SpringSource Inc. All Rights Reserved. */
package org.hyperic.hq.hqapi1.tools;
import java.util.Arrays;
import org.springframework.beans.factory.FactoryBean;
import joptsimple.OptionParser;
/**
* Factory responsible for generating {@link OptionParser}s and initializing
* with command line options accepted by all commands (i.e. connection
* parameters and help)
*
* @author Jennifer Hickey
*
*/
public class OptionParserFactory implements FactoryBean<OptionParser> {
static final String OPT_HOST = "host";
static final String OPT_PORT = "port";
static final String OPT_PORT_SSL = "portSSL";
static final String OPT_PORT_DEFAULTED = "portDefaulted";
static final String OPT_USER = "user";
static final String OPT_PASS = "password";
static final String[] OPT_SECURE = {"s", "secure"};
public static final String[] OPT_HELP = {"h","help"};
public static final String SYSTEM_PROP_PREFIX = "scripting.client.";
static final String OPT_FILE = "file";
static final String OPT_PROPERTIES = "properties";
public OptionParser getObject() throws Exception {
OptionParser parser = new OptionParser();
parser.accepts(OPT_HOST, "The HQ server host").
withRequiredArg().ofType(String.class);
parser.accepts(OPT_PORT, "The HQ server port. Defaults to 7080").
withOptionalArg().ofType(Integer.class);
parser.accepts(OPT_USER, "The user to connect as").
withRequiredArg().ofType(String.class);
parser.accepts(OPT_PASS, "The password for the given user").
withRequiredArg().ofType(String.class);
parser.acceptsAll(Arrays.asList(OPT_SECURE), "Connect using SSL");
parser.acceptsAll(Arrays.asList(OPT_HELP), "Show this message");
parser.accepts(OPT_FILE, "If specified, use the given file for " +
"commands that take XML input. If " +
"not specified, stdin will be used.").
withRequiredArg().ofType(String.class);
parser.accepts(OPT_PROPERTIES, "Specify the file to read for connection " +
"properties. Defaults to ~/.hq/client.properties")
.withRequiredArg().ofType(String.class);
return parser;
}
public Class<OptionParser> getObjectType() {
return OptionParser.class;
}
public boolean isSingleton() {
return false;
}
}