Commit d6de1018 authored by pgjones's avatar pgjones
Browse files

Add the routes command

This allows `quart routes` to be called to display all the registered
routes in the app. This follows Flask and is heavily influenced by the
Flask code.
parent a6ef68b6
......@@ -199,6 +199,7 @@ class QuartGroup(AppGroup):
if add_default_commands:
def get_command(self, ctx: click.Context, name: str) -> click.Command:
"""Return the relevant command given the context and name.
......@@ -264,6 +265,50 @@ def shell_command(info: ScriptInfo) -> None:
code.interact(banner=banner, local=context)
@click.command("routes", short_help="Show this app's routes.")
type=click.Choice({"endpoint", "methods", "rule", "match"}),
help="Order the routes by type, 'match' is the matching order when dispatching a request.",
@click.option("--all-methods", is_flag=True, help="Show HEAD and OPTIONS methods.")
def routes_command(info: ScriptInfo, sort: str, all_methods: bool) -> None:
app = info.load_app()
rules = list(app.url_map.iter_rules())
if len(rules) == 0:
click.echo("No routes were registered.")
ignored_methods = set() if all_methods else {"HEAD", "OPTIONS"}
if sort == "endpoint":
rules = sorted(rules, key=lambda rule: rule.endpoint)
elif sort == "rule":
rules = sorted(rules, key=lambda rule: rule.rule)
elif sort == "methods":
rules = sorted(rules, key=lambda rule: sorted(rule.methods))
headers = ("Endpoint", "Methods", "Websocket", "Rule")
rule_methods = [", ".join(sorted(rule.methods - ignored_methods)) for rule in rules]
widths = (
max(len(rule.endpoint) for rule in rules),
max(len(methods) for methods in rule_methods),
max(len(rule.rule) for rule in rules),
widths = [max(len(header), width) for header, width in zip(headers, widths)]
row = "{{0:<{0}}} | {{1:<{1}}} | {{2:<{2}}} | {{3:<{3}}}".format(*widths)
click.echo(row.format(*("-" * width for width in widths)))
for rule, methods in zip(rules, rule_methods):
click.echo(row.format(rule.endpoint, methods, str(rule.websocket), rule.rule).rstrip())
cli = QuartGroup(
Utility functions for Quart applications.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment