jfabot/commandii

Commandline tools

1.0.1 2017-09-04 05:35 UTC

This package is auto-updated.

Last update: 2022-10-22 22:47:30 UTC


README

Commandii is a tool library for the commandline which can:

  • Show and colorize messages in ANSI colors
  • Process commandline arguments and parameters

Known issues

At the moment there are no known issues. If you find one, please let me know through https://bitbucket.org/jfabot/commandii/issues/new

Usage

CommandLine

To use the commandline component in your code you need to include `use Commandii\CommandLine' To retrieve the used commandline commands and arguments:

$commandLine = new \Commandii\CommandLine;
$commandline->parseArgs($args)

This will return an array with all of the values, example: application command --argument1=value --argument2 Will return:

[0] => command
[argument1] => value
[argument2] => 1

If there is no value for the argument it will automatically be filled with a 1.

StatusMessages

If you want to colorize the progress of a process, you can include use Commandii\StatusMessages The StatusMessages component exists of the following functions:

dot($type, $character, $verboseMessage)

Show a progress-dot, default ., or the given alternative with $character.

$statusMessages = new StatusMessages;
$statusMessages->dot(StatusMessages::TYPE_NOTICE, 'n', 'This is a notice');

success($spacer)

Shows a SUCCESS message in green. By default there is no spacer in front.

$statusMessages = new StatusMessages;
$statusMessages->success();

warning($spacer)

Shows a WARNING message in orange. By default there is no spacer in front.

$statusMessages = new StatusMessages;
$statusMessages->warning();

error($spacer)

Shows a ERROR message in red. By default there is no spacer in front.

$statusMessages = new StatusMessages;
$statusMessages->error();

skipped($spacer)

Shows a SKIPPED message in cyan. By default there is no spacer in front.

$statusMessages = new StatusMessages;
$statusMessages->skipped();

warningText($message)

Shows a given message in orange.

$statusMessages = new StatusMessages;
$statusMessages->warningText('This is the message');

errorText($message)

Shows a given message in red.

$statusMessages = new StatusMessages;
$statusMessages->errorText('This is the message');

infoText($message, $type)

Shows a given message in the given type. If no type was given the text is in white.

$statusMessages = new StatusMessages;
$statusMessages->infoText('This is the message', StatusMessages::TYPE_INFO);

message($message, $linefeed, $verboseOnly)

The message function is used to show a message and has 2 extra arguments to control the behaviour. If linefeed is true, at the end of the message a end-of-line character is added. The second argument verboseOnly checks if the code is running in verbose mode. If so, the message will be shown, otherwise it will be discarded.

You can use this function to show a message like: Starting process

To do this

$statusMessages = new StatusMessages;
$statusMessages->message('Starting process', false, false);

This is handy when you need to show a progressbar after the text. You can use this in combination with the dot function to give feedback to the user in a compact or verbose format.

newLine()

$statusMessages = new StatusMessages;
$statusMessages->newLine();

question($question, $timeout = 5)

Shows a question that can be answered with y or n. The timeout is in seconds.

$statusMessages = new StatusMessages;
$statusMessages->question('This is the question', 3);

enableColors()

Enables the use of ANSI colors

$statusMessages = new StatusMessages;
$statusMessages->enableColors();

disableColors()

Disables the use of ANSI colors

$statusMessages = new StatusMessages;
$statusMessages->disableColors();

setVerbosePointer($verbosePointer)

You can set a variable to be watched. The value of the variable triggers if the component is using verbose or not.

resetVerbosePointer()

Clear the verbose variable

Logcompatible output

If you need to strip the output from the color tags you can use the function AnsiColors::colorize($text, $linebreak, $flatten) By setting flatten to true, the color tags are stripped and a flat output will be returned.