@Contract @PerLookup public abstract class CLICommand extends Object implements org.glassfish.hk2.api.PostConstruct
A command is executed with a list of arguments using the execute method. The implementation of the execute method in this class saves the arguments in the protected argv field, then calls the following protected methods in order: prepare, parse, validate, and executeCommand. A subclass must implement the prepare method to initialize the metadata that specified the valid options for the command, and the executeCommand method to actually perform the command. The parse and validate method may also be overridden if needed. Or, the subclass may override the execute method and provide the complete implementation for the command, including option parsing.
Modifier and Type | Field and Description |
---|---|
protected String[] |
argv
The command line arguments for this execution.
|
protected static String |
ASADMIN |
protected CommandModel |
commandModel
The metadata describing the command's options and operands.
|
static int |
CONNECTION_ERROR |
protected Environment |
env
The environment for the command.
|
static int |
ERROR |
static int |
INVALID_COMMAND_ERROR |
protected org.jline.reader.LineReader |
lineReader |
protected static Logger |
logger |
protected StringBuilder |
metadataErrors |
protected String |
name
The name of the command.
|
protected List<String> |
operands
The operands parsed from the command line.
|
protected ParameterMap |
options
The options parsed from the command line.
|
protected Map<String,String> |
passwords
The passwords read from the password file.
|
protected ProgramOptions |
programOpts
The program options for the command.
|
static int |
SUCCESS |
protected org.jline.terminal.Terminal |
terminal |
static int |
WARNING |
Modifier | Constructor and Description |
---|---|
protected |
CLICommand()
Constructor used by subclasses when instantiated by HK2.
|
protected |
CLICommand(String name,
ProgramOptions programOpts,
Environment env)
Constructor used by subclasses to save the name, program options,
and environment information into corresponding protected fields.
|
Modifier and Type | Method and Description |
---|---|
protected void |
buildLineReader() |
protected void |
buildTerminal() |
protected boolean |
checkHelp()
Check if the current request is a help request, either because
--help was specified as a program option or a command option.
|
protected void |
closeTerminal() |
int |
execute(String... argv)
Execute this command with the given arguemnts.
|
int |
execute(org.jline.terminal.Terminal terminal,
String... argv) |
protected abstract int |
executeCommand()
Execute the command using the options in options and the
operands in operands.
|
BufferedReader |
expandManPage(Reader r)
Return a man page for this command that has the tokens substituted
|
protected boolean |
getBooleanOption(String name)
Get a boolean option value, that might come from the command line
or from the environment.
|
String |
getBriefCommandUsage() |
static CLICommand |
getCommand(CLIContainer cLIContainer,
String name) |
static CLICommand |
getCommand(org.glassfish.hk2.api.ServiceLocator serviceLocator,
String name)
Get a CLICommand object representing the named command.
|
static String |
getCommandScope() |
String |
getCommandUsage()
Get the usage text for the command.
|
BufferedReader |
getManPage()
Return a BufferedReader for the man page for this command,
or null if not found.
|
String |
getName()
Return the name of this command.
|
protected CommandModel.ParamModel |
getOperandModel()
Get the ParamModel that corresponds to the operand
(primary parameter).
|
protected String |
getOption(String name)
Get an option value, that might come from the command line
or from the environment.
|
protected List<String> |
getOptions(String name)
Get option values, that might come from the command line
or from the environment.
|
protected char[] |
getPassword(CommandModel.ParamModel opt,
String defaultPassword,
boolean create)
Get a password for the given option.
|
protected char[] |
getPassword(String paramname,
String localizedPrompt,
String localizedPromptConfirm,
boolean create) |
ProgramOptions |
getProgramOptions()
Returns the program options associated with this command.
|
protected Map<String,String> |
getSystemProperties()
Return all the system properties and properties set
in asenv.conf.
|
protected String |
getSystemProperty(String name)
Return the named system property, or property
set in asenv.conf.
|
String |
getUsage()
Get the usage text for the subcommand.
|
protected void |
initializeLogger()
Initialize the state of the logger based on any program options.
|
protected void |
initializePasswords()
Initialise the passwords field based on the password
file specified in the program options, and initialise the
program option's password if available in the password file.
|
protected void |
inject()
Inject this instance with the final values of all the command
parameters.
|
protected static boolean |
ok(String s) |
protected void |
parse()
The parse method sets the options and operands fields
based on the content of the command line arguments.
|
void |
postConstruct()
Initialise the logger after being instantiated by HK2.
|
protected void |
prepare()
The prepare method must ensure that the commandModel field is set.
|
protected void |
prevalidate()
The prevalidate method supplies missing options from
the environment.
|
protected void |
printExceptionStackTrace(Throwable e)
Prints the exception message with level as FINER.
|
protected void |
processProgramOptions()
If the program options haven't already been set, parse them
on the command line and remove them from the command line.
|
static String |
quote(String value)
Quote a value, if the value contains any special characters.
|
protected char[] |
readPassword(String prompt)
Display the given prompt and read a password without echoing it.
|
static void |
setCommandScope(String ctx) |
String |
toString() |
protected Collection<CommandModel.ParamModel> |
usageOptions()
Subclasses can override this method to supply additional
or different options that should be part of the usage text.
|
protected void |
validate()
The validate method can be used by a subclass to validate
that the type and quantity of parameters and operands matches
the requirements for this command.
|
public static final int ERROR
public static final int CONNECTION_ERROR
public static final int INVALID_COMMAND_ERROR
public static final int SUCCESS
public static final int WARNING
protected org.jline.terminal.Terminal terminal
protected org.jline.reader.LineReader lineReader
protected static final String ASADMIN
protected static final Logger logger
protected String name
@Inject protected ProgramOptions programOpts
@Inject protected Environment env
protected String[] argv
protected CommandModel commandModel
protected StringBuilder metadataErrors
protected ParameterMap options
protected List<String> operands
protected CLICommand()
protected CLICommand(String name, ProgramOptions programOpts, Environment env)
name
- programOpts
- env
- public static CLICommand getCommand(org.glassfish.hk2.api.ServiceLocator serviceLocator, String name) throws CommandException
serviceLocator
- name
- The name of the commandCommandException
public static CLICommand getCommand(CLIContainer cLIContainer, String name) throws CommandException
CommandException
public void postConstruct()
postConstruct
in interface org.glassfish.hk2.api.PostConstruct
public int execute(org.jline.terminal.Terminal terminal, String... argv) throws CommandException
CommandException
public int execute(String... argv) throws CommandException
argv
- Arguments to execute command withCommandException
- if execution of the command failsCommandValidationException
- if there's something wrong
with the options or argumentspublic String getName()
public static String getCommandScope()
public static void setCommandScope(String ctx)
public ProgramOptions getProgramOptions()
public BufferedReader getManPage()
public BufferedReader expandManPage(Reader r)
r
- public String getUsage()
protected Collection<CommandModel.ParamModel> usageOptions()
public String getCommandUsage()
public String getBriefCommandUsage()
public static String quote(String value)
value
- value to be quotedprotected void processProgramOptions() throws CommandException
CommandException
protected void initializeLogger()
protected void initializePasswords() throws CommandException
CommandException
protected void prepare() throws CommandException
CommandException
protected void parse() throws CommandException
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong
with the options or argumentsprotected boolean checkHelp() throws CommandException
CommandException
protected void prevalidate() throws CommandException
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong
with the options or argumentsprotected void inject() throws CommandException
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong
with the options or argumentsprotected void validate() throws CommandException
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong
with the options or argumentsprotected abstract int executeCommand() throws CommandException
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong
with the options or argumentsprotected char[] getPassword(String paramname, String localizedPrompt, String localizedPromptConfirm, boolean create) throws CommandValidationException
CommandValidationException
protected char[] getPassword(CommandModel.ParamModel opt, String defaultPassword, boolean create) throws CommandValidationException
opt
- defaultPassword
- create
- CommandValidationException
protected char[] readPassword(String prompt)
prompt
- protected CommandModel.ParamModel getOperandModel()
protected String getOption(String name)
name
- protected List<String> getOptions(String name)
name
- protected boolean getBooleanOption(String name)
name
- protected String getSystemProperty(String name)
name
- protected Map<String,String> getSystemProperties()
protected void printExceptionStackTrace(Throwable e)
e
- the exception object to printprotected static boolean ok(String s)
protected void buildTerminal()
protected void buildLineReader()
protected void closeTerminal()
Copyright © 2020. All rights reserved.