WebFioriAPI ReferenceLearnDownloadContributeLearn{{result.parent_page}} > {{result.title}}{{result.title}}Classes{{result.class_name}}{{result.summary}}Methods{{result.name}}{{result.summary}}

namespace \webfiori\framework\cli

abstract class CLICommand

Class Attributes Summary

const COLORSAn associative array that contains color codes and names.

Class Methods Summary

public function __construct(string$commandName, array$args, string$description)Creates new instance of the class.public function addArg(string$name, array$options)Add command argument.public function addArgs(array$arr)Adds multiple arguments to the command.public function clear(int$numberOfCols, boolean$beforeCursor)Clears the output before or after cursor position.public function clearConsole()Clears the whole content of the console.public function clearLine()Clears the line at which the cursor is in and move it back to the start of the line.public function confirm(string$confirmTxt, boolean|null$default)Asks the user to conform something.public function error(string$message)Display a message that represents an error.public function excCommand()Execute the command.public abstract function exec()Execute the command.public static function formatOutput(string$string, array$formatOptions)Formats an output string.public function getArgInfo(string$argName)Returns an associative array that contains one argument information.public function getArgValue(string$optionName)Returns the value of command option from CLI given its name.public function getArgs()Returns an associative array that contains command args.public function getDescription()Returns the description of the command.public function getInput(string$prompt, string$default, callable$validator)Take an input value from the user.public function getInputStream()Returns the stream at which the command is sing to read inputs.public function getName()Returns the name of the command.public function getOutputStream()Returns the stream at which the command is using to send output.public function hasArg(string$argName)Checks if the command has a specific command line argument or not.public function info(string$message)Display a message that represents extra information.public function isArgProvided(string$argName)Checks if an argument is provided in the CLI or not.public function moveCursorDown(int$lines)Moves the cursor down by specific number of lines.public function moveCursorLeft(int$numberOfCols)Moves the cursor to the left by specific number of columns.public function moveCursorRight(int$numberOfCols)Moves the cursor to the right by specific number of columns.public function moveCursorTo(int$line, int$col)Moves the cursor to specific position in the terminal.public function moveCursorUp(int$lines)Moves the cursor up by specific number of lines.public function printList(array$array)Prints an array as a list of items.public function println(string$str, mixed$_)Print out a string and terminates the current line by writing the line separator string.public function prints(string$str, mixed$_)Print out a string.public function read(unkown_type$bytes )Reads a string of bytes from input stream.public function readln()Reads one line from input stream.public function select(string$prompt, array$choices, int$defaultIndex)Ask the user to select one of multiple values.public function setArgValue(string$argName, string$argValue)Sets the value of an argument.public function setDescription(string$str)Sets the description of the command.public function setInputStream(InputStream$stream)Sets the stream at which the command will read input from.public function setName(string$name)Sets the name of the command.public function setOutputStream(OutputStream$stream)Sets the stream at which the command will send output to.public function success(string$message)Display a message that represents a success status.public function warning(string$message)Display a message that represents a warning.

Class Attributes Details

const COLORSAn associative array that contains color codes and names.

Class Methods Details

public function __construct(string$commandName, array$args, string$description)Creates new instance of the class.

Parameters:

  • string $commandName A string that represents the name of the command such as '-v' or 'help'. If not provided, the value 'new-command' is used.
  • array $args An associative array of sub-associative arrays of arguments (or options) which can be supplied to the command when running it. The key of each sub array is argument name. Each sub-array can have the following indices as argument options:
    • optional: A boolean. if set to true, it means that the argument is optional and can be ignored when running the command.
    • default: An optional default value for the argument to use if it is not provided and is optional.
    • description: A description of the argument which will be shown if the command 'help' is executed.
    • values: A set of values that the argument can have. If provided, only the values on the list will be allowed. Note that if null or empty string is in the array, it will be ignored. Also, if boolean values are provided, true will be converted to the string 'y' and false will be converted to the string 'n'.
  • string $description A string that describes what does the job do. The description will appear when the command 'help' is executed.
public function addArg(string$name, array$options)Add command argument. An argument is a string that comes after the name of the command. The value of an argument can be set using equal sign. For example, if command name is 'do-it' and one argument has the name 'what-to-do', then the full CLI command would be "do-it what-to-do=say-hi". An argument can be also treated as an option.

Parameters:

  • string $name The name of the argument. It must be non-empty string and does not contain spaces. Note that if the argument is already added and the developer is trying to add it again, the new options array will override the existing options array.
  • array $options An optional array of options. Available options are:
    • optional: A boolean. if set to true, it means that the argument is optional and can be ignored when running the command.
    • default: An optional default value for the argument to use if it is not provided and is optional.
    • description: A description of the argument which will be shown if the command 'help' is executed.
    • values: A set of values that the argument can have. If provided, only the values on the list will be allowed. Note that if null or empty string is in the array, it will be ignored. Also, if boolean values are provided, true will be converted to the string 'y' and false will be converted to the string 'n'.
Returns: booleanIf the argument is added, the method will return true. Other than that, the method will return false.
public function addArgs(array$arr)Adds multiple arguments to the command.

Parameters:

  • array $arr An associative array of sub associative arrays. The key of each sub array is argument name. Each sub-array can have the following indices:
    • optional: A boolean. if set to true, it means that the argument is optional and can be ignored when running the command.
    • default: An optional default value for the argument to use if it is not provided and is optional.
    • description: A description of the argument which will be shown if the command 'help' is executed.
    • values: A set of values that the argument can have. If provided, only the values on the list will be allowed. Note that if null or empty string is in the array, it will be ignored. Also, if boolean values are provided, true will be converted to the string 'y' and false will be converted to the string 'n'.
public function clear(int$numberOfCols, boolean$beforeCursor)Clears the output before or after cursor position. This method will replace the visible characters with spaces. Note that support for this operation depends on terminal support for ANSI escape codes.

Parameters:

  • int $numberOfCols Number of columns to clear. The columns that will be cleared are before and after cursor position. They don't include the character at which the cursor is currently pointing to.
  • boolean $beforeCursor If set to true, the characters which are before the cursor will be cleared. Default is true.
public function clearConsole()Clears the whole content of the console. Note that support for this operation depends on terminal support for ANSI escape codes.public function clearLine()Clears the line at which the cursor is in and move it back to the start of the line. Note that support for this operation depends on terminal support for ANSI escape codes.public function confirm(string$confirmTxt, boolean|null$default)Asks the user to conform something. This method will display the question and wait for the user to confirm the action by entering 'y' or 'n' in the terminal. If the user give something other than 'Y' or 'n', it will shows an error and ask him to confirm again. If a default answer is provided, it will appear in upper case in the terminal. For example, if default is set to true, at the end of the prompt, the string that shows the options would be like '(Y/n)'.

Parameters:

  • string $confirmTxt The text of the question which will be asked.
  • boolean|null $default Default answer to use if empty input is given. It can be true for 'y' and false for 'n'. Default value is null which means no default will be used.
Returns: booleanIf the user choose 'y', the method will return true. If he choose 'n', the method will return false.
public function error(string$message)Display a message that represents an error. The message will be prefixed with the string 'Error:' in red.

Parameters:

  • string $message The message that will be shown.
public function excCommand()Execute the command. This method should not be called manually by the developer.Returns: intIf the command is executed, the method will return 0. Other than that, it will return a number which depends on the return value of the method 'CLICommand::exec()'.public abstract function exec()Execute the command. The implementation of this method should contain the code that will run when the command is executed.Returns: intThe developer should implement this method in a way it returns 0 or null if the command is executed successfully and return -1 if the command did not execute successfully.public static function formatOutput(string$string, array$formatOptions)Formats an output string. This method is used to add colors to the output string or make it bold or underlined. The returned value of this method can be sent to any output stream using the method 'fprintf()'. Note that the support for colors and formatting will depend on the terminal configuration. In addition, if the constant NO_COLOR is defined or is set in the environment, the returned string will be returned as is.

Parameters:

  • string $string The string that will be formatted.
  • array $formatOptions An associative array of formatting options. Supported options are:
    • color: The foreground color of the output text. Supported colors are:
      • white
      • black
      • red
      • light-red
      • green
      • light-green
      • yellow
      • light-yellow
      • gray
      • blue
      • light-blue
    • bg-color: The background color of the output text. Supported colors are the same as the supported colors by the 'color' option.
    • bold: A boolean. If set to true, the text will be bold.
    • underline: A boolean. If set to true, the text will be underlined.
    • reverse: A boolean. If set to true, the foreground color and background color will be reversed (invert the foreground and background colors).
    • blink: A boolean. If set to true, the text will blink.
Returns: stringThe string after applying the formatting to it.
public function getArgInfo(string$argName)Returns an associative array that contains one argument information.

Parameters:

  • string $argName The name of the argument.
Returns: arrayIf the argument exist, the method will return an associative array. The returned array will possibly have the following indices:
  • optional: A booleean which is set to true if the argument is optional.
  • description: The description of the argument. Appears when help command is executed.
  • default: A default value for the argument. It will be not set if no default value for the argument is provided.
  • values: A set of values at which the argument can have.
  • provided: Set to true if the argument is provided in command line interface.
  • val: The value of the argument taken from the command line interface.
If the argument does not exist, the returned array will be empty.
public function getArgValue(string$optionName)Returns the value of command option from CLI given its name.

Parameters:

  • string $optionName The name of the option.
Returns: string|nullIf the value of the option is set, the method will return its value as string. If it is not set, the method will return null.
public function getArgs()Returns an associative array that contains command args. Returns: arrayAn associative array. The indices of the array are the names of the arguments and the values are sub-associative arrays. the sub arrays will have the following indices:
  • optional
  • description
  • default
    • Note that the last index might not be set.
public function getDescription()Returns the description of the command. The description of the command is a string that describes what does the command do and it will appear in CLI if the command 'help' is executed.Returns: stringThe description of the command. Default return value is '<NO DESCRIPTION>'public function getInput(string$prompt, string$default, callable$validator)Take an input value from the user.

Parameters:

  • string $prompt The string that will be shown to the user. The string must be non-empty.
  • string $default An optional default value to use in case the user hit "Enter" without entering any value. If null is passed, no default value will be set.
  • callable $validator A callback that can be used to validate user input. The callback accepts one parameter which is the value that the user has given. If the value is valid, the callback must return true. If the callback returns anything else, it means the value which is given by the user is invalid and this method will ask the user to enter the value again.
Returns: stringThe method will return the value which was taken from the user.
public function getInputStream()Returns the stream at which the command is sing to read inputs. Returns: null|InputStreamIf the stream is set, it will be returned as an object. Other than that, the method will return null.public function getName()Returns the name of the command. The name of the command is a string which is used to call the command from CLI.Returns: stringThe name of the command (such as 'v' or 'help'). Default return value is 'new-command'.public function getOutputStream()Returns the stream at which the command is using to send output. Returns: null|OutputStreamIf the stream is set, it will be returned as an object. Other than that, the method will return null.public function hasArg(string$argName)Checks if the command has a specific command line argument or not.

Parameters:

  • string $argName The name of the command line argument.
Returns: booleanIf the argument is added to the command, the method will return true. If no argument which has the given name does exist, the method will return false.
public function info(string$message)Display a message that represents extra information. The message will be prefixed with the string 'Info:' in blue.

Parameters:

  • string $message The message that will be shown.
public function isArgProvided(string$argName)Checks if an argument is provided in the CLI or not. The method will not check if the argument has a value or not.

Parameters:

  • string $argName The name of the command line argument.
Returns: booleanIf the argument is provided, the method will return true. Other than that, the method will return false.
public function moveCursorDown(int$lines)Moves the cursor down by specific number of lines. Note that support for this operation depends on terminal support for ANSI escape codes.

Parameters:

  • int $lines The number of lines the cursor will be moved. Default value is 1.
public function moveCursorLeft(int$numberOfCols)Moves the cursor to the left by specific number of columns. Note that support for this operation depends on terminal support for ANSI escape codes.

Parameters:

  • int $numberOfCols The number of columns the cursor will be moved. Default value is 1.
public function moveCursorRight(int$numberOfCols)Moves the cursor to the right by specific number of columns. Note that support for this operation depends on terminal support for ANSI escape codes.

Parameters:

  • int $numberOfCols The number of columns the cursor will be moved. Default value is 1.
public function moveCursorTo(int$line, int$col)Moves the cursor to specific position in the terminal. If no arguments are supplied to the method, it will move the cursor to the upper-left corner of the screen (line 0, column 0). Note that support for this operation depends on terminal support for ANSI escape codes.

Parameters:

  • int $line The number of line at which the cursor will be moved to. If not specified, 0 is used.
  • int $col The number of column at which the cursor will be moved to. If not specified, 0 is used.
public function moveCursorUp(int$lines)Moves the cursor up by specific number of lines. Note that support for this operation depends on terminal support for ANSI escape codes.

Parameters:

  • int $lines The number of lines the cursor will be moved. Default value is 1.
public function printList(array$array)Prints an array as a list of items. This method is useful if the developer would like to print out a list of multiple items. Each item will be prefixed with a number that represents its index in the array.

Parameters:

  • array $array The array that will be printed.
public function println(string$str, mixed$_)Print out a string and terminates the current line by writing the line separator string. This method will work like the function fprintf(). The difference is that it will print out to the stream at which was specified by the method CLICommand::setOutputStream() and the text can have formatting options. Note that support for output formatting depends on terminal support for ANSI escape codes.

Parameters:

  • string $str The string that will be printed.
  • mixed $_ One or more extra arguments that can be supplied to the method. The last argument can be an array that contains text formatting options. for available options, check the method CLICommand::formatOutput().
public function prints(string$str, mixed$_)Print out a string. This method works exactly like the function 'fprintf()'. The only difference is that the method will print out the output to the stream that was specified using the method CLICommand::setOutputStream() and the method accepts formatting options as last argument to format the output. Note that support for output formatting depends on terminal support for ANSI escape codes.

Parameters:

  • string $str The string that will be printed.
  • mixed $_ One or more extra arguments that can be supplied to the method. The last argument can be an array that contains text formatting options. for available options, check the method CLICommand::formatOutput().
public function read(unkown_type$bytes )Reads a string of bytes from input stream. This method is used to read specific number of characters from input stream.

Parameters:

  • unkown_type $bytes
Returns: stringThe method will return the string which was given as input in the input stream.
public function readln()Reads one line from input stream. The method will continue to read from input stream till it finds end of line character "\n".Returns: stringThe method will return the string which was taken from input stream without the end of line character.public function select(string$prompt, array$choices, int$defaultIndex)Ask the user to select one of multiple values. This method will display a prompt and wait for the user to select the a value from a set of values. If the user give something other than the listed values, it will shows an error and ask him to select again again. The user can select an answer by typing its text or its number which will appear in the terminal.

Parameters:

  • string $prompt The text that will be shown for the user.
  • array $choices An indexed array of values to select from.
  • int $defaultIndex The index of the default value in case no value is selected and the user hit enter.
Returns: stringThe method will return the value which is selected by the user.
public function setArgValue(string$argName, string$argValue)Sets the value of an argument. This method is useful in writing test cases for the commands.

Parameters:

  • string $argName The name of the argument.
  • string $argValue The value to set.
Returns: booleanIf the value of the argument is set, the method will return true. If not set, the method will return false. The value of the attribute will be not set in the following cases:
  • If the argument can have a specific set of values and the given value is not one of them.
  • The given value is empty string or null.
public function setDescription(string$str)Sets the description of the command. The description of the command is a string that describes what does the command do and it will appear in CLI if the command 'help' is executed.

Parameters:

  • string $str A string that describes the command. It must be non-empty string.
Returns: booleanIf the description of the command is set, the method will return true. Other than that, the method will return false.
public function setInputStream(InputStream$stream)Sets the stream at which the command will read input from.

Parameters:

  • InputStream $stream An instance that implements an input stream.
public function setName(string$name)Sets the name of the command. The name of the command is a string which is used to call the command from CLI.

Parameters:

  • string $name The name of the command (such as 'v' or 'help'). It must be non-empty string and does not contain spaces.
Returns: booleanIf the name of the command is set, the method will return true. Other than that, the method will return false.
public function setOutputStream(OutputStream$stream)Sets the stream at which the command will send output to.

Parameters:

  • OutputStream $stream An instance that implements output stream.
public function success(string$message)Display a message that represents a success status. The message will be prefixed with the string "Success:" in green.

Parameters:

  • string $message The message that will be displayed.
public function warning(string$message)Display a message that represents a warning. The message will be prefixed with the string 'Warning:' in red.

Parameters:

  • string $message The message that will be shown.
mdi-menuAll Classesmdi-chevron-leftAnchorBrCodeSnippetHTMLDocHTMLListHTMLNodeHTMLTableHeadNodeInputJsCodeLabelListItemOrderedListParagraphRadioGroupTableCellTableRowUnorderedListInvalidNodeNameExceptionTemplateNotFoundExceptionCaseConverterJsonJsonConverterJsonIJsonTypesPropertyAPIFilterAbstractWebServiceManagerInfoServiceParamTypesRequestRequestParameterResponseUriWebServicesManagerAccessAutoLoaderConfigConfigControllerDBEAbstractWebServiceExtendedWebServicesManagerFileLoggerPrivilegePrivilegesGroupThemeThemeLoaderUploadFileUploaderUserUtilWebFioriAppLanguageErrorBoxHTTPCodeViewMessageBoxServerErrViewStarterPageWebPageDatabaseSessionStorageDefaultSessionStorageMSSQLSessionDataTableMSSQLSessionsTableMySQLSessionDataTableMySQLSessionsTableSessionSessionOperationsSessionStorageSessionsManagerRouterRouterUriAbstractMiddlewareMiddlewareManagerEmailMessageSMTPAccountSMTPServerClassLoaderExceptionFileExceptionInitializationExceptionInvalidCRONExprExceptionMissingLangExceptionNoSuchThemeExceptionRoutingExceptionSMTPExceptionSessionExceptionUIExceptionAbstractJobCronCronEmailCronJobJobArgumentCronLoginViewCronTasksViewCronViewCronLoginServiceCronLogoutServiceCronServicesManagerForceCronExecutionServiceGetJobsServiceCLICLICommandInputStreamOutputStreamStdInStdOutClassWriterLangClassWriterQueryClassWriterServiceHolderThemeClassWriterWebServiceWriterCreateCLIClassHelperCreateCronJobCreateMiddlewareCreateTableCreateTableObjCreateThemeHelperCreateWebServiceAddCommandCreateCommandCronCommandHelpCommandListCronCommandListRoutesCommandListThemesCommandRunSQLQueryCommandSettingsCommandTestRouteCommandUpdateSettingsCommandUpdateTableCommandVersionCommandAbstractQueryColumnColumnFactoryConditionConnectionConnectionInfoDatabaseDatabaseExceptionDateTimeValidatorEntityMapperExpressionForeignKeyJoinTableResultSetSelectExpressionTableWhereExpressionMySQLColumnMySQLConnectionMySQLQueryMySQLTableMSSQLColumnMSSQLConnectionMSSQLQueryMSSQLTableAbstractCollectionComparableLinkedListNodeQueueStack
Powered By: WebFiori Framework, Vue and VuetifyAll Rights Reserved © 2018 - 2024