from prompt_toolkit.shortcuts import ProgressBar
from prompt_toolkit.formatted_text import HTML
import time
title = HTML('''Downloading <style bg="yellow" fg="black">
4 files...</style>''')
label = HTML('<ansired>some file</ansired>: ')
with ProgressBar(title=title) as pb:
for i in pb(range(800), label=label):
time.sleep(.01)
import os
def get_env_vars(ctx, args, incomplete):
return [k for k in os.environ.keys() if incomplete in k]
@click.command()
@click.argument("envvar", type=click.STRING,
autocompletion=get_env_vars)
def cmd1(envvar):
click.echo(f"Environment variable: {envvar}")
click.echo(f"Value: {os.environ[envvar]}")
partial completion
Валидация
from yarl import URL
class UrlType(click.ParamType):
name = "url"
def convert(self, val, param, ctx):
try:
return URL(val)
except (TypeError, ValueError):
self.fail(f"{val!r} is not an URL ", param, ctx)
URL = URLType()