GithubHelp home page GithubHelp logo

python-repository-hub / cleo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from python-poetry/cleo

0.0 0.0 0.0 763 KB

Cleo allows you to create beautiful and testable command-line interfaces.

License: MIT License

Python 100.00%

cleo's Introduction

Cleo

Cleo Build status

Create beautiful and testable command-line interfaces.

Cleo is mostly a higher level wrapper for CliKit, so a lot of the components and utilities comes from it. Refer to its documentation for more information.

Resources

Usage

To make a command that greets you from the command line, create greet_command.py and add the following to it:

You also need to create the file to run at the command line which creates an Application and adds commands to it:

Test the new command by running the following

This will print the following to the command line:

Hello John

You can also use the --yell option to make everything uppercase:

This prints:

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 :

The signature being in the following form:

The signature can span multiple lines.

Coloring the Output

Whenever you output text, you can surround the text with tags to color its output. For example:

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:

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:

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
clikit.VERBOSITY_NORMAL The default verbosity level (none)
clikit.VERBOSITY_VERBOSE Increased verbosity of messages -v
clikit.VERBOSITY_VERY_VERBOSE Informative non essential messages -vv
clikit.VERBOSITY_DEBUG Debug messages -vvv

It is possible to print a message in a command for only a specific verbosity level. For example:

There are also more semantic methods you can use to test for each of the verbosity levels:

You can also pass the verbosity flag directly to line().

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:

You now have access to a last_name argument in your command:

The command can now be used in either of the following ways:

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:

To use this, just specify as many names as you want:

You can access the names argument as a list:

There are 3 argument variants you can use:

Mode Notation Value
clikit.ARGUMENT_REQUIRED none (just write the argument name) The argument is required
clikit.ARGUMENT_OPTIONAL argument? The argument is optional and therefore can be omitted
clikit.ARGUMENT_MULTI_VALUED 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:

If you want to set a default value, you can it like so:

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:

Next, use this in the command to print the message multiple times:

Now, when you run the task, you can optionally specify a --iterations flag:

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:

There are 4 option variants you can use:

Option Notation Value
clikit.OPTION_MULTI_VALUED --option=* This option accepts multiple values (e.g. --dir=/foo --dir=/bar)
clikit.OPTION_NO_VALUE --option Do not accept input for this option (e.g. --yell)
clikit.OPTION_REQUIRED_VALUE --option= This value is required (e.g. --iterations=5), the option itself is still optional
clikit.OPTION_OPTIONAL_VALUE --option=? This option may or may not have a value (e.g. --yell or --yell=loud)

You can combine them like this:

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:

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:

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:

If you want to suppress the output of the executed command, you can use the call_silent() method instead.

Autocompletion

Cleo supports automatic (tab) completion in bash, zsh and fish.

To activate support for autocompletion, pass a complete keyword when initializing your application:

Now, register completion for your application by running one of the following in a terminal, replacing [program] with the command you use to run your application:

cleo's People

Contributors

romibuzi avatar sdispater avatar sohierdane avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.