Download the PHP package spaceboy/nette-cli without Composer
On this page you can find all versions of the php package spaceboy/nette-cli. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package nette-cli
NetteCli
Simple tool for easy CLI apps creation in Nette framework
Installation
The best way to install into Nette web application is the easiest one. Open console, go to your app directory and execute following command:
My first CLI application
I strongly recommend you to create a dedicated space for CLI applications in app root directory. For example bin
for apps operated from command line and cron
for apps runned from cron.
After installation copy (or link) file nette-cli.php
from vendor/spaceboy/nette-cli/bin
directory to folder dedicated for CLI scripts (e.g. bin
).
Create a PHP
file, f.e. command.php
in bin
directory:
Or create template script by simple running nette-cli.php
in bin
directory:
In command.php
, we at first must create app namespace and include required files and namespaces.
Then we can create our first app:
That's all, folks. Try it in command line:
As we've registered command
named "hello", it's worker function is called and executed.
As we've registered argument
named "name" and set that argument required for command "hello" (Command->withArgumentRequired([arg-name])
), our app will not run without typing argument in command line.
As we've registered also shortcut
argument name (Argument->setShortcut()
), we can run our app wit less writing:
As we've set required format of argument "name" (Argument->setFormat()
) as string:2..25
(string with length at least 2 chars and 25 chars max), our app will not run with too short or too long name. Try it yourself.
As we've registered also option
named "strong" in application (Cli->registerOption('strong')
) and enabled this option in command "hello" (Command->withOption(strong)
), we can use it:
Cli
public methods:
-
setName(string $name): Cli
Sets application name displayed during each command execution.
-
setDescription(string $description): Cli
Sets application description displayed when application is run without any command/argument (help), lists of commands, arguments and options follow.
-
registerArgument(Argument $argument): Cli
Registers argument (see Argument). Only registered arguments can be referrenced by commands.
-
registerOption(Argument $option): Cli
Registers option (see Argument, as option has type Argument). Only registered arguments can be referrenced by commands.
-
registerCommand(Command $command): Cli
Registers executable command (see Command).
-
run(string $arguments = null)
Runs whole application. When you for some reason (e.g. during testing) need manipulate arguments from command line, use
$arguments
argument. Example: -
error(string $message): void
Static method; displays error message (
$message
) end exits script.
Argument
public methods:
-
create(string $name): Argument
Static method, creates an instance of
Argument
. All other methods can be chained. -
setDescription(string $description): Argument
Sets argument description (displayed when user looks for help, so try to be acurate).
-
setShortcut(string $shortcut): Argument
Sets one char shortcut for argument name. Try to find and predicable and intuitive char, or just don't use shortcut.
-
setFormat(string $format): Argument
Sets required Nette validation type for argument. Can save you lot of validations in the command worker function body.
Command
public methods:
-
create(string $name)
Static method, creates an instance of
Command
. All other methods can be chained. -
setDescription(string $description): Command
Sets argument description (displayed when user looks for help, so try to be acurate).
-
withArgumentRequired(string $argumentName): Command
Sets required argument for command worker function. Only registered argument names can be used.
-
withArgumentOptional(string $argumentName): Command
Sets optional argument for command worker function. Only registered argument names can be used.
-
withOption(string $optionName): Command
Sets optional boolean argument (option) for command worker function. Only registered option names can be used.
-
setWorker(callable $worker): Command
Sets executive function for command. Function arguments must be:
- Registered in Cli (using
Cli->registerArgument()
orCli->registerOption()
) AND declared in Command definition (usingCommand->withArgumentRequired()
,Command->withArgumentOptional()
orCommand->withOption()
) (Arguments passed from command line)
OR
- Declared by typehint (Nette application classes/objects)
Example:
- Registered in Cli (using
Class Format
Class Format
is an simple helper for easier command line text formatting.
Methods:
-
reset(): string
Return string which resets text/background color settings to standard.
-
color(string ...$color): string
Return string which (after echoing on console) sets text/background color for next output. At the end, don't forget to reset settings to default (using
reset
method)! -
bold(string $text): string
Return string which is (after echoing on console) displayed bold.
-
dim(string $text): string
Return string which is (after echoing on console) displayed dim.
-
underlined(string $text): string
Return string which is (after echoing on console) displayed underlined.
-
blink(string $text): string
Return string which is (after echoing on console) displayed blinking.
-
reverse(string $text): string
Return string which is (after echoing on console) displayed in reverse (text and background colors are switched).
-
hidden(string $text): string
Return string which is (after echoing on console) displayed hidden (useful for passwords etc.).
-
bell(): string
Return string which (after echoing on console) makes beep sound (like good old telex bell).
-
backspace(): string
Return string which (after echoing on console) moves cursor one position left.
-
tab(): string
Return string which (after echoing on console) moves cursor to the next tab stop (or the end of line, when there ane no more tab stops).
-
getConsoleColumns: int
Return console width (in characters).
-
getConsoleLines: int
Return console heights (in lines).
Color table:
text color code | background color code | color |
---|---|---|
DEFAULT_COLOR | BG_DEFAULT | default console color |
BLACK | BG_BLACK | black |
RED | BG_RED | red |
GREEN | BG_GREEN | green |
YELLOW | BG_YELLOW | yellow |
BLUE | BG_BLUE | blue |
MAGENTA | BG_MAGENTA | magenta |
CYAN | BG_CYAN | cyan |
LIGHT_GRAY | BG_LIGHT_GRAY | light gray |
DARK_GRAY | BG_DARK_GRAY | dark gray |
LIGHT_RED | BG_LIGHT_RED | light red |
LIGHT_GREEN | BG_LIGHT_GREEN | light green |
LIGHT_YELLOW | BG_LIGHT_YELLOW | light yellow |
LIGHT_BLUE | BG_LIGHT_BLUE | light blue |
LIGHT_MAGENTA | BG_LIGHT_MAGENTA | light magenta |
LIGHT_CYAN | BG_LIGHT_CYAN | light cyan |
WHITE | BG_WHITE | white |
Using helpers
When things gonna be complicated, you should need to share some argument(s) or worker functions(s) between two or more scripts (e.g. between cli
script and cron
script).
Feel free to use helpers. You can set both static and dynamic methods as command worker function as well as closure. Just don't forget that those methods must be public.
All versions of nette-cli with dependencies
nette/bootstrap Version ^3.0
nette/utils Version ^3.0
nette/di Version ^3.0