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: 153.794789698
click_extra.timer
API¶
classDiagram ExtraOption <|-- TimerOption
Command execution time measurement.
- class click_extra.timer.TimerOption(param_decls=None, default=False, expose_value=False, is_eager=True, 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"]
.