Click
Click builds command-line interfaces from decorated functions. @click.command turns a function into a command, and each @click.option adds a flag with its default and help text, which Click collects into a generated help page. Commands nest inside groups to any depth, and subcommands can load lazily at runtime. An option marked with prompt asks for its value when the flag is missing.
- Downloads / mo
- 1.1B
- Stars
- 17.8K
- Last release
- 8.5.1
- License
- BSD-3-Clause
Strengths
- Shares context across subcommands. Let subcommands use data from their parent command.
- Prompts for missing options. Accept supplied options or ask interactively for missing values.
Limitations
- Fixed help layout. Click limits how far help pages can be customized, so that separately written Click commands still work together.
- Completion needs an entry point. Shell completion works only for a script installed through an entry point, not one started with the
pythoncommand.
Quickstart
import click @click.command() @click.option('--count', default=1, help='Number of greetings.') @click.option('--name', prompt='Your name', help='The person to greet.') def hello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for x in range(count): click.echo(f"Hello {name}!") if __name__ == '__main__': hello()
Click examples
@mypyc_attr(patchable=True) def _out(message: Optional[str] = None, nl: bool = True, **styles: Any) -> None: if message is not None: if "bold" not in styles: styles["bold"] = True message = style(message, **styles) echo(message, nl=nl, err=True)
@click.command("routes", short_help="Show the routes for the app.") @click.option( "--sort", "-s", type=click.Choice(("endpoint", "methods", "domain", "rule", "match")), default="endpoint", help=( "Method to sort routes by. 'match' is the order that Flask will match routes" " when dispatching a request." ), ) @click.option("--all-methods", is_flag=True, help="Show HEAD and OPTIONS methods.") @with_appcontext def routes_command(sort: str, all_methods: bool) -> None: """Show all registered routes with endpoints and methods.""" rules = list(current_app.url_map.iter_rules()) if not rules: click.echo("No routes were registered.") return ignored_methods = set() if all_methods else {"HEAD", "OPTIONS"} host_matching = current_app.url_map.host_matching has_domain = any(rule.host if host_matching else rule.subdomain for rule in rules) rows = [] for rule in rules: row = [ rule.endpoint, ", ".join(sorted((rule.methods or set()) - ignored_methods)), ] if has_domain: row.append((rule.host if host_matching else rule.subdomain) or "") row.append(rule.rule) rows.append(row) headers = ["Endpoint", "Methods"] sorts = ["endpoint", "methods"] if has_domain: headers.append("Host" if host_matching else "Subdomain") sorts.append("domain") headers.append("Rule") sorts.append("rule") try: rows.sort(key=itemgetter(sorts.index(sort))) except ValueError: pass rows.insert(0, headers) widths = [max(len(row[i]) for row in rows) for i in range(len(headers))] rows.insert(1, ["-" * w for w in widths]) template = " ".join(f"{{{i}:<{w}}}" for i, w in enumerate(widths)) for row in rows: click.echo(template.format(*row))