Tutorial 2: Advanced

It’s the little things

If you haven’t gone through the regular Tutorial yet, you should definitely look there first.

The following are little things that will help with making that app more complete.

Setting the application version

You probably noticed that there’s a version output flag in the help/usage output:

-v, --version                   Show the version and exit.

If you leave this as-is, it will only ever show the version of es_client, so let’s see how to change this to be our own version.

Where’s my version?

Most PEP compliant releases of a project will have a __version__ defined somewhere. By default, Click will attempt to guess the version from that value. It does so successfully with es_client in our example script.

@click.version_option(None, '-v', '--version', prog_name="cli_example")

If Click guesses wrong, you can try to tell it which package to check:

@click.version_option(None, '-v', '--version', pacakge_name='es_client', prog_name="cli_example")

And if that still doesn’t work, you can manually specify a version:

@click.version_option('X.Y.Z', '-v', '--version', prog_name="cli_example")

or directly reference your __version__:

from es_client import __version__
# ...
@click.version_option(__version__, '-v', '--version', prog_name="cli_example")

With regard to prog_name, the documentation says, “The name of the CLI to show in the message. If not provided, it will be detected from the command.”

If I leave prog_name unset and run the version output, I would see:

run_script.py, version X.Y.Z

But with it set, I see:

cli_example, version X.Y.Z

But you can also format the output of this using message. According to the documentation, “The message to show. The values %(prog)s, %(package)s, and %(version)s are available. Defaults to "%(prog)s, version %(version)s".”

So if I set:

@click.version_option(
   None, '-v', '--version', prog_name="cli_example",
   message='%(prog)s from %(package)s, version %(version)s')

I would see:

python run_script.py -v                                                                                                  ─╯
cli_example from es_client, version X.Y.Z

Importing es_client into your own project

It’s all well and good to test against the es_client code, but wouldn’t you rather make use of it in your own code?

Include es_client as a dependency

If you’re following PEP conventions, your project probably has a pyproject.toml file. Inside that file will be a header labeled [project], and under that section will be a subsection titled dependencies followed by a list of modules your project depends on. This is where you need to list es_client as a dependency:

dependencies = [
    ...
    "es_client==X.Y.Z"
    ...
]

You will probably need to do something to make sure it’s imported into your virtualenv while you are coding and testing. Having it installed allows IDEs and similar coding environments to help with documentation and code completion. Installing dependencies can be accomplished by running:

pip install -U .

If run from the root of your project, this will install all dependencies in pyproject.toml.

Import into your code

Once es_client is available to your code, you can import it or any of its classes, submodules, functions and constants. This pattern is visible in the example script at the top of the page:

from es_client.helpers.config import (
   context_settings, generate_configdict, get_client, get_config,
   options_from_dict)
from es_client.defaults import OPTION_DEFAULTS, SHOW_EVERYTHING
from es_client.helpers.logging import configure_logging

“Secret Borrowing”

“Good artists borrow. Great artists steal.” (Attributed to Pablo Picasso)

It’s completely acceptable and appropriate to copy the example script and use it as the basis for your own application. Why re-invent the wheel when you have a working wheel that you only need to tweak a bit?

Watch This Space

More advanced tutorials will follow!