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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
Usage
#####
To make a command that greets you from the command line,
create ``greet_command.py`` and add the following to it:
.. code-block:: python
from cleo import Command
class GreetCommand(Command):
"""
Greets someone
greet
{name? : Who do you want to greet?}
{--y|yell : If set, the task will yell in uppercase letters}
"""
def handle(self):
name = self.argument('name')
if name:
text = 'Hello {}'.format(name)
else:
text = 'Hello'
if self.option('yell'):
text = text.upper()
self.line(text)
You also need to create the file to run at the command line which creates
an ``Application`` and adds commands to it:
.. code-block:: python
#!/usr/bin/env python
from greet_command import GreetCommand
from cleo import Application
application = Application()
application.add(GreetCommand())
if __name__ == '__main__':
application.run()
Test the new command by running the following
.. code-block:: bash
$ python application.py greet John
This will print the following to the command line:
.. code-block:: text
Hello John
You can also use the ``--yell`` option to make everything uppercase:
.. code-block:: bash
$ python application.py greet John --yell
This prints:
.. code-block:: text
HELLO JOHN
As you may have already seen, Cleo uses the command docstring to determine
the command definition.
The docstring must be in the following form :
.. code-block:: python
"""
Command description
Command signature
"""
The signature being in the following form:
.. code-block:: python
"""
command:name {argument : Argument description} {--option : Option description}
"""
The signature can span multiple lines.
.. code-block:: python
"""
command:name
{argument : Argument description}
{--option : Option description}
"""
Coloring the Output
===================
Whenever you output text, you can surround the text with tags to color its
output. For example:
.. code-block:: python
# blue text
self.line("<info>foo</info>")
# green text
self.line("<comment>foo</comment>")
# cyan text
self.line("<question>foo</question>")
# bold red text
self.line("<error>foo</error>")
# cyan text
self.line("<c1>foo</c1>")
# bold text
self.line("<c2>foo</c2>")
# bold text
self.line("<b>foo</b>")
The closing tag can be replaced by ``</>``, which revokes all formatting options established by the last opened tag.
It is possible to define your own styles using the ``add_style()`` method:
.. code-block:: python
self.add_style('fire', fg='red', bg='yellow', options=['bold', 'blink'])
self.line('<fire>foo</fire>')
Available foreground and background colors are: ``black``, ``red``, ``green``,
``yellow``, ``blue``, ``magenta``, ``cyan`` and ``white``.
And available options are: ``bold``, ``underscore``, ``blink``, ``reverse`` and ``conceal``.
You can also set these colors and options inside the tag name:
.. code-block:: python
# green text
self.line('<fg=green>foo</>')
# black text on a cyan background
self.line('<fg=black;bg=cyan>foo</>')
# bold text on a yellow background
self.line('<bg=yellow;options=bold>foo</>')
Verbosity Levels
================
Cleo has four verbosity levels. These are defined in the ``Output`` class:
======================================= ================================== ======================
Mode Meaning Console option
======================================= ================================== ======================
``NA`` Do not output any messages ``-q`` or ``--quiet``
``Verbosity.NORMAL`` The default verbosity level (none)
``Verbosity.VERBOSE`` Increased verbosity of messages ``-v``
``Verbosity.VERY_VERBOSE`` Informative non essential messages ``-vv``
``Verbosity.DEBUG`` Debug messages ``-vvv``
======================================= ================================== ======================
It is possible to print a message in a command for only a specific verbosity
level. For example:
.. code-block:: python
if Verbosity.VERBOSE <= self.io.verbosity:
self.line(...)
There are also more semantic methods you can use to test for each of the
verbosity levels:
.. code-block:: python
if self.output.is_quiet():
# ...
if self.output.is_verbose():
# ...
You can also pass the verbosity flag directly to `line()`.
.. code-block:: python
self.line("", verbosity=Verbosity.VERBOSE)
When the quiet level is used, all output is suppressed.
Using Arguments
===============
The most interesting part of the commands are the arguments and options that
you can make available. Arguments are the strings - separated by spaces - that
come after the command name itself. They are ordered, and can be optional
or required. For example, add an optional ``last_name`` argument to the command
and make the ``name`` argument required:
.. code-block:: python
class GreetCommand(Command):
"""
Greets someone
greet
{name : Who do you want to greet?}
{last_name? : Your last name?}
{--y|yell : If set, the task will yell in uppercase letters}
"""
You now have access to a ``last_name`` argument in your command:
.. code-block:: python
last_name = self.argument('last_name')
if last_name:
text += ' {}'.format(last_name)
The command can now be used in either of the following ways:
.. code-block:: bash
$ python application.py greet John
$ python application.py greet John Doe
It is also possible to let an argument take a list of values (imagine you want
to greet all your friends). For this it must be specified at the end of the
argument list:
.. code-block:: python
class GreetCommand(Command):
"""
Greets someone
greet
{names* : Who do you want to greet?}
{--y|yell : If set, the task will yell in uppercase letters}
"""
To use this, just specify as many names as you want:
.. code-block:: bash
$ python application.py demo:greet John Jane
You can access the ``names`` argument as a list:
.. code-block:: python
names = self.argument('names')
if names:
text += ' {}'.format(', '.join(names))
There are 3 argument variants you can use:
================================ ==================================== ===============================================================================================================
Mode Notation Value
================================ ==================================== ===============================================================================================================
``Required`` none (just write the argument name) The argument is required
``Optional`` ``argument?`` The argument is optional and therefore can be omitted
``List`` ``argument*`` The argument can contain an indefinite number of arguments and must be used at the end of the argument list
================================ ==================================== ===============================================================================================================
You can combine them like this:
.. code-block:: python
class GreetCommand(Command):
"""
Greets someone
greet
{names?* : Who do you want to greet?}
{--y|yell : If set, the task will yell in uppercase letters}
"""
If you want to set a default value, you can it like so:
.. code-block:: text
argument=default
The argument will then be considered optional.
Using Options
=============
Unlike arguments, options are not ordered (meaning you can specify them in any
order) and are specified with two dashes (e.g. ``--yell`` - you can also
declare a one-letter shortcut that you can call with a single dash like
``-y``). Options are *always* optional, and can be setup to accept a value
(e.g. ``--dir=src``) or simply as a boolean flag without a value (e.g.
``--yell``).
.. tip::
It is also possible to make an option *optionally* accept a value (so that
``--yell`` or ``--yell=loud`` work). Options can also be configured to
accept a list of values.
For example, add a new option to the command that can be used to specify
how many times in a row the message should be printed:
.. code-block:: python
class GreetCommand(Command):
"""
Greets someone
greet
{name? : Who do you want to greet?}
{--y|yell : If set, the task will yell in uppercase letters}
{--iterations=1 : How many times should the message be printed?}
"""
Next, use this in the command to print the message multiple times:
.. code-block:: python
for _ in range(0, self.option('iterations')):
self.line(text)
Now, when you run the task, you can optionally specify a ``--iterations``
flag:
.. code-block:: bash
$ python application.py demo:greet John
$ python application.py demo:greet John --iterations=5
The first example will only print once, since ``iterations`` is empty and
defaults to ``1``. The second example will print five times.
Recall that options don't care about their order. So, either of the following
will work:
.. code-block:: bash
$ python application.py demo:greet John --iterations=5 --yell
$ python application.py demo:greet John --yell --iterations=5
There are 4 option variants you can use:
================================ =================================== ======================================================================================
Option Notation Value
================================ =================================== ======================================================================================
``List`` ``--option=*`` This option accepts multiple values (e.g. ``--dir=/foo --dir=/bar``)
``Flag`` ``--option`` Do not accept input for this option (e.g. ``--yell``)
``Requires value`` ``--option=`` This value is required (e.g. ``--iterations=5``), the option itself is still optional
``Optional value`` ``--option=?`` This option may or may not have a value (e.g. ``--yell`` or ``--yell=loud``)
================================ =================================== ======================================================================================
You can combine them like this:
.. code-block:: python
class GreetCommand(Command):
"""
Greets someone
greet
{name? : Who do you want to greet?}
{--y|yell : If set, the task will yell in uppercase letters}
{--iterations=?*1 : How many times should the message be printed?}
"""
Helpers
=======
Cleo also contains a set of "helpers" - different small
tools capable of helping you with different tasks:
* :doc:`helpers/question_helper`: interactively ask the user for information
* :doc:`helpers/progress_bar`: shows a progress bar
* :doc:`helpers/table`: displays tabular data as a table
Testing Commands
================
Cleo provides several tools to help you test your commands. The most
useful one is the ``CommandTester`` class.
It uses a special IO class to ease testing without a real
console:
.. code-block:: python
import pytest
from cleo import Application
from cleo.testers.command_tester import CommandTester
def test_execute(self):
application = Application()
application.add(GreetCommand())
command = application.find('demo:greet')
command_tester = CommandTester(command)
command_tester.execute()
assert "..." == command_tester.io.fetch_output()
The ``CommandTester.io.fetch_output()`` method returns what would have been displayed
during a normal call from the console. ``CommandTester.io.fetch_error()`` is also available
to get what you have been written to the stderr.
You can test sending arguments and options to the command by passing them
as a string to the ``CommandTester.execute()`` method:
.. code-block:: python
import pytest
from cleo import Application
from cleo.testers.command_tester import CommandTester
def test_execute(self):
application = Application()
application.add(GreetCommand())
command = application.find('demo:greet')
command_tester = CommandTester(command)
command_tester.execute("John")
assert "John" in command_tester.io.fetch_output()
Testing with user inputs
------------------------
To test user inputs, you pass it to ``execute()``.
.. code-block:: python
command_tester = CommandTester(command)
command_tester.execute(inputs="123\nfoo\nbar")
.. tip::
You can also test a whole console application by using the ``ApplicationTester`` class.
Calling an existing Command
===========================
If a command depends on another one being run before it, instead of asking the
user to remember the order of execution, you can call it directly yourself.
This is also useful if you want to create a "meta" command that just runs a
bunch of other commands.
Calling a command from another one is straightforward:
.. code-block:: python
def handle(self):
return_code = self.call('demo:greet', "John --yell")
# ...
.. tip::
If you want to suppress the output of the executed command,
you can use the ``call_silent()`` method instead.
Overwrite the current line
==========================
If you want to overwrite the current line, you can use the ``overwrite()`` method.
.. code-block:: python
def handle(self):
self.write('Processing...')
# do some work
self.overwrite('Done!')
.. warning::
``overwrite()`` will only work in combination with the ``write()`` method which does not
add a new line.
.. note::
``overwrite()`` does not automatically add a new line so you must call ``line('')`` if necessary.
Autocompletion
==============
Cleo supports automatic (tab) completion in ``bash``, ``zsh`` and ``fish``.
By default, your application will have a ``completions`` command. To register these completions for your application, run one of the following in a terminal (replacing ``[program]`` with the command you use to run your application):
.. code-block:: bash
# Bash
[program] completions bash | sudo tee /etc/bash_completion.d/[program].bash-completion
# Bash - macOS/Homebrew (requires `brew install bash-completion`)
[program] completions bash > $(brew --prefix)/etc/bash_completion.d/[program].bash-completion
# Zsh
mkdir ~/.zfunc
echo "fpath+=~/.zfunc" >> ~/.zshrc
[program] completions zsh > ~/.zfunc/_[program]
# Zsh - macOS/Homebrew
[program] completions zsh > $(brew --prefix)/share/zsh/site-functions/_[program]
# Fish
[program] completions fish > ~/.config/fish/completions/[program].fish
|