diff --git a/linodecli/__init__.py b/linodecli/__init__.py index 3fa192e8c..41cdc791a 100755 --- a/linodecli/__init__.py +++ b/linodecli/__init__.py @@ -19,6 +19,7 @@ from .cli import CLI from .completion import get_completions from .configuration import ENV_TOKEN_NAME +from .help_formatter import SortingHelpFormatter from .help_pages import ( HELP_TOPICS, print_help_action, @@ -61,6 +62,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements parser = argparse.ArgumentParser( "linode-cli", add_help=False, + formatter_class=SortingHelpFormatter, description="The Linode Command Line Interface.\n\nAliases: lin, linode", ) parsed, args = register_args(parser).parse_known_args() diff --git a/linodecli/baked/operation.py b/linodecli/baked/operation.py index a14b2a1e9..486632025 100644 --- a/linodecli/baked/operation.py +++ b/linodecli/baked/operation.py @@ -25,6 +25,7 @@ ) from linodecli.baked.response import OpenAPIResponse from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter from linodecli.output.output_handler import OutputHandler from linodecli.overrides import OUTPUT_OVERRIDES @@ -822,6 +823,7 @@ def parse_args(self, args: Any) -> argparse.Namespace: # build an argparse parser = argparse.ArgumentParser( prog=f"linode-cli {self.command} {self.action}", + formatter_class=SortingHelpFormatter, description=self.summary, ) for param in self.params: diff --git a/linodecli/help_formatter.py b/linodecli/help_formatter.py new file mode 100644 index 000000000..fb51e635d --- /dev/null +++ b/linodecli/help_formatter.py @@ -0,0 +1,15 @@ +""" +Contains sorting formatter for help menu. +""" + +from argparse import HelpFormatter +from operator import attrgetter + +class SortingHelpFormatter(HelpFormatter): + """ + The formatter class for help menu. + """ + + def add_arguments(self, actions): + actions = sorted(actions, key=attrgetter('option_strings')) + super().add_arguments(actions) diff --git a/linodecli/plugins/firewall-editor.py b/linodecli/plugins/firewall-editor.py index f7c1f494d..d9e53b5af 100644 --- a/linodecli/plugins/firewall-editor.py +++ b/linodecli/plugins/firewall-editor.py @@ -15,6 +15,7 @@ from rich.table import Table from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter from linodecli.plugins import inherit_plugin_args BOLD = "\033[1m" @@ -581,8 +582,13 @@ def call(args, context): Invokes the Interactive Firewall Plugin """ parser = inherit_plugin_args( - argparse.ArgumentParser("firewall-editor", add_help=True) + argparse.ArgumentParser( + "firewall-editor", + add_help=True, + formatter_class=SortingHelpFormatter + ) ) + parser.add_argument("firewall_id", help="The ID of the firewall to edit.") parsed = parser.parse_args(args) diff --git a/linodecli/plugins/get-kubeconfig.py b/linodecli/plugins/get-kubeconfig.py index 1652b999f..207ebbef0 100644 --- a/linodecli/plugins/get-kubeconfig.py +++ b/linodecli/plugins/get-kubeconfig.py @@ -14,6 +14,7 @@ import yaml from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter PLUGIN_BASE = "linode-cli get-kubeconfig" @@ -22,7 +23,11 @@ def call(args, context): """ The entrypoint for this plugin """ - parser = argparse.ArgumentParser(PLUGIN_BASE, add_help=True) + parser = argparse.ArgumentParser( + PLUGIN_BASE, + add_help=True, + formatter_class=SortingHelpFormatter + ) group = parser.add_mutually_exclusive_group() diff --git a/linodecli/plugins/image-upload.py b/linodecli/plugins/image-upload.py index 19b1068c5..6ef01e02e 100644 --- a/linodecli/plugins/image-upload.py +++ b/linodecli/plugins/image-upload.py @@ -15,6 +15,7 @@ import requests from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter from linodecli.plugins import inherit_plugin_args PLUGIN_BASE = "linode-cli image-upload" @@ -68,7 +69,11 @@ def call(args, context): The entrypoint for this plugin """ parser = inherit_plugin_args( - argparse.ArgumentParser(PLUGIN_BASE, add_help=True) + argparse.ArgumentParser( + PLUGIN_BASE, + add_help=True, + formatter_class=SortingHelpFormatter + ) ) parser.add_argument( diff --git a/linodecli/plugins/metadata.py b/linodecli/plugins/metadata.py index 8d13e1403..f9805ef45 100644 --- a/linodecli/plugins/metadata.py +++ b/linodecli/plugins/metadata.py @@ -17,6 +17,7 @@ from rich.table import Table from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter from linodecli.helpers import register_debug_arg PLUGIN_BASE = "linode-cli metadata" @@ -184,7 +185,11 @@ def get_metadata_parser(): """ Builds argparser for Metadata plug-in """ - parser = ArgumentParser(PLUGIN_BASE, add_help=False) + parser = ArgumentParser( + PLUGIN_BASE, + add_help=False, + formatter_class=SortingHelpFormatter + ) register_debug_arg(parser) diff --git a/linodecli/plugins/obj/__init__.py b/linodecli/plugins/obj/__init__.py index 9fd981b65..642993308 100644 --- a/linodecli/plugins/obj/__init__.py +++ b/linodecli/plugins/obj/__init__.py @@ -20,6 +20,7 @@ from linodecli.configuration import _do_get_request from linodecli.configuration.helpers import _default_text_input from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter from linodecli.plugins import PluginContext, inherit_plugin_args from linodecli.plugins.obj.buckets import create_bucket, delete_bucket from linodecli.plugins.obj.config import ( @@ -68,7 +69,11 @@ def generate_url(get_client, args, **kwargs): # pylint: disable=unused-argument """ Generates a URL to an object """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " signurl")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " signurl", + formatter_class=SortingHelpFormatter) + ) parser.add_argument( "bucket", @@ -119,7 +124,12 @@ def set_acl(get_client, args, **kwargs): # pylint: disable=unused-argument """ Modify Access Control List for a Bucket or Objects """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " setacl")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " setacl", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "bucket", metavar="BUCKET", type=str, help="The bucket to modify." @@ -182,7 +192,12 @@ def show_usage(get_client, args, **kwargs): # pylint: disable=unused-argument """ Shows space used by all buckets in this cluster, and total space """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " du")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " du", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "bucket", @@ -287,7 +302,13 @@ def get_obj_args_parser(): """ Initialize and return the argument parser for the obj plug-in. """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE, add_help=False)) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE, + add_help=False, + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "command", diff --git a/linodecli/plugins/obj/buckets.py b/linodecli/plugins/obj/buckets.py index b21b7129f..37ebc4d8c 100644 --- a/linodecli/plugins/obj/buckets.py +++ b/linodecli/plugins/obj/buckets.py @@ -6,6 +6,7 @@ from argparse import ArgumentParser from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter from linodecli.plugins import inherit_plugin_args from linodecli.plugins.obj.config import PLUGIN_BASE from linodecli.plugins.obj.helpers import _delete_all_objects @@ -17,7 +18,12 @@ def create_bucket( """ Creates a new bucket """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " mb")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " mb", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "name", @@ -41,7 +47,12 @@ def delete_bucket( """ Deletes a bucket """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " rb")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " rb", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "name", diff --git a/linodecli/plugins/obj/list.py b/linodecli/plugins/obj/list.py index 6a6184da7..89fce8152 100644 --- a/linodecli/plugins/obj/list.py +++ b/linodecli/plugins/obj/list.py @@ -8,6 +8,7 @@ from rich import print as rprint from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter from linodecli.helpers import register_pagination_args_shared from linodecli.plugins import inherit_plugin_args from linodecli.plugins.obj.config import PLUGIN_BASE @@ -31,7 +32,13 @@ def list_objects_or_buckets( """ Lists buckets or objects """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " ls")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " ls", + formatter_class=SortingHelpFormatter + ) + ) + register_pagination_args_shared(parser) parser.add_argument( @@ -130,7 +137,13 @@ def list_all_objects( Lists all objects in all buckets """ # this is for printing help when --help is in the args - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " la")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " la", + formatter_class=SortingHelpFormatter + ) + ) + register_pagination_args_shared(parser) parsed = parser.parse_args(args) diff --git a/linodecli/plugins/obj/objects.py b/linodecli/plugins/obj/objects.py index f3e2513cf..e12bc9273 100644 --- a/linodecli/plugins/obj/objects.py +++ b/linodecli/plugins/obj/objects.py @@ -17,6 +17,7 @@ pass from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter from linodecli.helpers import expand_globs from linodecli.plugins import inherit_plugin_args from linodecli.plugins.obj.config import ( @@ -36,7 +37,12 @@ def upload_object( """ Uploads an object to object storage """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " put")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " put", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "file", metavar="FILE", type=str, nargs="+", help="The files to upload." @@ -126,7 +132,12 @@ def get_object( """ Retrieves an uploaded object and writes it to a file """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " get")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " get", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "bucket", metavar="BUCKET", type=str, help="The bucket the file is in." @@ -201,7 +212,12 @@ def delete_object( """ Removes a file from a bucket """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " del")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " del", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "bucket", metavar="BUCKET", type=str, help="The bucket to delete from." diff --git a/linodecli/plugins/obj/website.py b/linodecli/plugins/obj/website.py index 9d55be173..54e600f92 100644 --- a/linodecli/plugins/obj/website.py +++ b/linodecli/plugins/obj/website.py @@ -4,6 +4,7 @@ from argparse import ArgumentParser +from linodecli.help_formatter import SortingHelpFormatter from linodecli.plugins import inherit_plugin_args from linodecli.plugins.obj.config import BASE_WEBSITE_TEMPLATE, PLUGIN_BASE @@ -14,7 +15,12 @@ def enable_static_site( """ Turns a bucket into a static website """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " ws-create")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " ws-create", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "bucket", @@ -74,7 +80,12 @@ def static_site_info( """ Returns info about a configured static site """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " ws-info")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " ws-info", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "bucket", @@ -109,7 +120,12 @@ def disable_static_site( """ Disables static site for a bucket """ - parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " du")) + parser = inherit_plugin_args( + ArgumentParser( + PLUGIN_BASE + " du", + formatter_class=SortingHelpFormatter + ) + ) parser.add_argument( "bucket", diff --git a/linodecli/plugins/ssh.py b/linodecli/plugins/ssh.py index 2ed53cbdb..2536e1cf0 100644 --- a/linodecli/plugins/ssh.py +++ b/linodecli/plugins/ssh.py @@ -16,6 +16,7 @@ from typing import Any, Dict, Optional, Tuple from linodecli.exit_codes import ExitCodes +from linodecli.help_formatter import SortingHelpFormatter from linodecli.plugins import inherit_plugin_args @@ -33,7 +34,11 @@ def call(args, context): # pylint: disable=too-many-branches sys.exit(ExitCodes.REQUEST_FAILED) parser = inherit_plugin_args( - argparse.ArgumentParser("linode-cli ssh", add_help=True) + argparse.ArgumentParser( + "linode-cli ssh", + add_help=True, + formatter_class=SortingHelpFormatter + ) ) parser.add_argument(