Show all registered routes with endpoints and methods.
(sort: str, all_methods: bool)
| 1067 | @click.option("--all-methods", is_flag=True, help="Show HEAD and OPTIONS methods.") |
| 1068 | @with_appcontext |
| 1069 | def routes_command(sort: str, all_methods: bool) -> None: |
| 1070 | """Show all registered routes with endpoints and methods.""" |
| 1071 | rules = list(current_app.url_map.iter_rules()) |
| 1072 | |
| 1073 | if not rules: |
| 1074 | click.echo("No routes were registered.") |
| 1075 | return |
| 1076 | |
| 1077 | ignored_methods = set() if all_methods else {"HEAD", "OPTIONS"} |
| 1078 | host_matching = current_app.url_map.host_matching |
| 1079 | has_domain = any(rule.host if host_matching else rule.subdomain for rule in rules) |
| 1080 | rows = [] |
| 1081 | |
| 1082 | for rule in rules: |
| 1083 | row = [ |
| 1084 | rule.endpoint, |
| 1085 | ", ".join(sorted((rule.methods or set()) - ignored_methods)), |
| 1086 | ] |
| 1087 | |
| 1088 | if has_domain: |
| 1089 | row.append((rule.host if host_matching else rule.subdomain) or "") |
| 1090 | |
| 1091 | row.append(rule.rule) |
| 1092 | rows.append(row) |
| 1093 | |
| 1094 | headers = ["Endpoint", "Methods"] |
| 1095 | sorts = ["endpoint", "methods"] |
| 1096 | |
| 1097 | if has_domain: |
| 1098 | headers.append("Host" if host_matching else "Subdomain") |
| 1099 | sorts.append("domain") |
| 1100 | |
| 1101 | headers.append("Rule") |
| 1102 | sorts.append("rule") |
| 1103 | |
| 1104 | try: |
| 1105 | rows.sort(key=itemgetter(sorts.index(sort))) |
| 1106 | except ValueError: |
| 1107 | pass |
| 1108 | |
| 1109 | rows.insert(0, headers) |
| 1110 | widths = [max(len(row[i]) for row in rows) for i in range(len(headers))] |
| 1111 | rows.insert(1, ["-" * w for w in widths]) |
| 1112 | template = " ".join(f"{{{i}:<{w}}}" for i, w in enumerate(widths)) |
| 1113 | |
| 1114 | for row in rows: |
| 1115 | click.echo(template.format(*row)) |
| 1116 | |
| 1117 | |
| 1118 | cli = FlaskGroup( |