Timer#

Option#

Click Extra can measure the execution time of a CLI via a dedicated --time/--no-time option.

Here how to use the standalone decorator:

from time import sleep

from click_extra import command, echo, pass_context, timer_option

@command
@timer_option
def timer():
    sleep(0.2)
    echo("Hello world!")
$ timer --help
Usage: timer [OPTIONS]

Options:
  --time / --no-time  Measure and print elapsed execution time.
  --help              Show this message and exit.
$ timer --time
Hello world!
Execution time: 0.200 seconds.

Get start time#

You can get the timestamp of the CLI start from the context:

from click_extra import command, echo, pass_context, timer_option

@command
@timer_option
@pass_context
def timer_command(ctx):
    start_time = ctx.meta["click_extra.start_time"]
    echo(f"Start timestamp: {start_time}")
$ timer-command
Start timestamp: 202.101230971

click_extra.timer API#

classDiagram ExtraOption <|-- TimerOption

Command execution time measurement.

class click_extra.timer.TimerOption(param_decls=None, default=False, expose_value=False, help='Measure and print elapsed execution time.', **kwargs)[source]#

Bases: ExtraOption

A pre-configured option that is adding a --time/--no-time flag to print elapsed time at the end of CLI execution.

The start time is made available in the context in ctx.meta["click_extra.start_time"].

print_timer()[source]#

Compute and print elapsed execution time.

register_timer_on_close(ctx, param, value)[source]#

Callback setting up all timer’s machinery.

Computes and print the execution time at the end of the CLI, if option has been activated.

Return type:

None