Skip to content

Commit 2a16733

Browse files
Sort options (#708)
Co-authored-by: Zhiwei Liang <[email protected]> Co-authored-by: Zhiwei Liang <[email protected]>
1 parent be6e62f commit 2a16733

File tree

13 files changed

+122
-19
lines changed

13 files changed

+122
-19
lines changed

linodecli/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .cli import CLI
2121
from .completion import get_completions
2222
from .configuration import ENV_TOKEN_NAME
23+
from .help_formatter import SortingHelpFormatter
2324
from .help_pages import (
2425
HELP_TOPICS,
2526
print_help_action,
@@ -64,6 +65,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
6465
parser = argparse.ArgumentParser(
6566
"linode-cli",
6667
add_help=False,
68+
formatter_class=SortingHelpFormatter,
6769
description="The Linode Command Line Interface.\n\nAliases: lin, linode",
6870
)
6971
parsed, args = register_args(parser).parse_known_args()

linodecli/baked/operation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from linodecli.baked.response import OpenAPIResponse
3030
from linodecli.baked.util import unescape_arg_segment
3131
from linodecli.exit_codes import ExitCodes
32+
from linodecli.help_formatter import SortingHelpFormatter
3233
from linodecli.output.output_handler import OutputHandler
3334
from linodecli.overrides import OUTPUT_OVERRIDES
3435

@@ -831,6 +832,7 @@ def parse_args(self, args: Any) -> argparse.Namespace:
831832
# build an argparse
832833
parser = argparse.ArgumentParser(
833834
prog=f"linode-cli {self.command} {self.action}",
835+
formatter_class=SortingHelpFormatter,
834836
description=self.summary,
835837
)
836838
for param in self.params:

linodecli/help_formatter.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Contains sorting formatter for help menu.
3+
"""
4+
5+
from argparse import HelpFormatter
6+
from operator import attrgetter
7+
8+
9+
class SortingHelpFormatter(HelpFormatter):
10+
"""
11+
The formatter class for help menu.
12+
"""
13+
14+
def add_arguments(self, actions):
15+
actions = sorted(actions, key=attrgetter("option_strings"))
16+
super().add_arguments(actions)

linodecli/plugins/firewall-editor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from rich.table import Table
1616

1717
from linodecli.exit_codes import ExitCodes
18+
from linodecli.help_formatter import SortingHelpFormatter
1819
from linodecli.plugins import inherit_plugin_args
1920

2021
BOLD = "\033[1m"
@@ -581,8 +582,13 @@ def call(args, context):
581582
Invokes the Interactive Firewall Plugin
582583
"""
583584
parser = inherit_plugin_args(
584-
argparse.ArgumentParser("firewall-editor", add_help=True)
585+
argparse.ArgumentParser(
586+
"firewall-editor",
587+
add_help=True,
588+
formatter_class=SortingHelpFormatter,
589+
)
585590
)
591+
586592
parser.add_argument("firewall_id", help="The ID of the firewall to edit.")
587593

588594
parsed = parser.parse_args(args)

linodecli/plugins/get-kubeconfig.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import yaml
1515

1616
from linodecli.exit_codes import ExitCodes
17+
from linodecli.help_formatter import SortingHelpFormatter
1718

1819
PLUGIN_BASE = "linode-cli get-kubeconfig"
1920

@@ -22,7 +23,9 @@ def call(args, context):
2223
"""
2324
The entrypoint for this plugin
2425
"""
25-
parser = argparse.ArgumentParser(PLUGIN_BASE, add_help=True)
26+
parser = argparse.ArgumentParser(
27+
PLUGIN_BASE, add_help=True, formatter_class=SortingHelpFormatter
28+
)
2629

2730
group = parser.add_mutually_exclusive_group()
2831

linodecli/plugins/image-upload.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import requests
1616

1717
from linodecli.exit_codes import ExitCodes
18+
from linodecli.help_formatter import SortingHelpFormatter
1819
from linodecli.plugins import inherit_plugin_args
1920

2021
PLUGIN_BASE = "linode-cli image-upload"
@@ -68,7 +69,9 @@ def call(args, context):
6869
The entrypoint for this plugin
6970
"""
7071
parser = inherit_plugin_args(
71-
argparse.ArgumentParser(PLUGIN_BASE, add_help=True)
72+
argparse.ArgumentParser(
73+
PLUGIN_BASE, add_help=True, formatter_class=SortingHelpFormatter
74+
)
7275
)
7376

7477
parser.add_argument(

linodecli/plugins/metadata.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from rich.table import Table
1818

1919
from linodecli.exit_codes import ExitCodes
20+
from linodecli.help_formatter import SortingHelpFormatter
2021
from linodecli.helpers import register_debug_arg
2122

2223
PLUGIN_BASE = "linode-cli metadata"
@@ -184,7 +185,9 @@ def get_metadata_parser():
184185
"""
185186
Builds argparser for Metadata plug-in
186187
"""
187-
parser = ArgumentParser(PLUGIN_BASE, add_help=False)
188+
parser = ArgumentParser(
189+
PLUGIN_BASE, add_help=False, formatter_class=SortingHelpFormatter
190+
)
188191

189192
register_debug_arg(parser)
190193

linodecli/plugins/obj/__init__.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from linodecli.configuration import _do_get_request
2121
from linodecli.configuration.helpers import _default_text_input
2222
from linodecli.exit_codes import ExitCodes
23+
from linodecli.help_formatter import SortingHelpFormatter
2324
from linodecli.plugins import PluginContext, inherit_plugin_args
2425
from linodecli.plugins.obj.buckets import create_bucket, delete_bucket
2526
from linodecli.plugins.obj.config import (
@@ -68,7 +69,11 @@ def generate_url(get_client, args, **kwargs): # pylint: disable=unused-argument
6869
"""
6970
Generates a URL to an object
7071
"""
71-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " signurl"))
72+
parser = inherit_plugin_args(
73+
ArgumentParser(
74+
PLUGIN_BASE + " signurl", formatter_class=SortingHelpFormatter
75+
)
76+
)
7277

7378
parser.add_argument(
7479
"bucket",
@@ -119,7 +124,11 @@ def set_acl(get_client, args, **kwargs): # pylint: disable=unused-argument
119124
"""
120125
Modify Access Control List for a Bucket or Objects
121126
"""
122-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " setacl"))
127+
parser = inherit_plugin_args(
128+
ArgumentParser(
129+
PLUGIN_BASE + " setacl", formatter_class=SortingHelpFormatter
130+
)
131+
)
123132

124133
parser.add_argument(
125134
"bucket", metavar="BUCKET", type=str, help="The bucket to modify."
@@ -183,7 +192,11 @@ def show_usage(get_client, args, **kwargs): # pylint: disable=unused-argument
183192
"""
184193
Shows space used by all buckets in this cluster, and total space
185194
"""
186-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " du"))
195+
parser = inherit_plugin_args(
196+
ArgumentParser(
197+
PLUGIN_BASE + " du", formatter_class=SortingHelpFormatter
198+
)
199+
)
187200

188201
parser.add_argument(
189202
"bucket",
@@ -290,7 +303,11 @@ def get_obj_args_parser():
290303
"""
291304
Initialize and return the argument parser for the obj plug-in.
292305
"""
293-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE, add_help=False))
306+
parser = inherit_plugin_args(
307+
ArgumentParser(
308+
PLUGIN_BASE, add_help=False, formatter_class=SortingHelpFormatter
309+
)
310+
)
294311

295312
parser.add_argument(
296313
"command",

linodecli/plugins/obj/buckets.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from argparse import ArgumentParser
77

88
from linodecli.exit_codes import ExitCodes
9+
from linodecli.help_formatter import SortingHelpFormatter
910
from linodecli.plugins import inherit_plugin_args
1011
from linodecli.plugins.obj.config import PLUGIN_BASE
1112
from linodecli.plugins.obj.helpers import _delete_all_objects
@@ -17,7 +18,11 @@ def create_bucket(
1718
"""
1819
Creates a new bucket
1920
"""
20-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " mb"))
21+
parser = inherit_plugin_args(
22+
ArgumentParser(
23+
PLUGIN_BASE + " mb", formatter_class=SortingHelpFormatter
24+
)
25+
)
2126

2227
parser.add_argument(
2328
"name",
@@ -41,7 +46,11 @@ def delete_bucket(
4146
"""
4247
Deletes a bucket
4348
"""
44-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " rb"))
49+
parser = inherit_plugin_args(
50+
ArgumentParser(
51+
PLUGIN_BASE + " rb", formatter_class=SortingHelpFormatter
52+
)
53+
)
4554

4655
parser.add_argument(
4756
"name",

linodecli/plugins/obj/list.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from rich import print as rprint
99

1010
from linodecli.exit_codes import ExitCodes
11+
from linodecli.help_formatter import SortingHelpFormatter
1112
from linodecli.helpers import register_pagination_args_shared
1213
from linodecli.plugins import inherit_plugin_args
1314
from linodecli.plugins.obj.config import PLUGIN_BASE
@@ -31,7 +32,12 @@ def list_objects_or_buckets(
3132
"""
3233
Lists buckets or objects
3334
"""
34-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " ls"))
35+
parser = inherit_plugin_args(
36+
ArgumentParser(
37+
PLUGIN_BASE + " ls", formatter_class=SortingHelpFormatter
38+
)
39+
)
40+
3541
register_pagination_args_shared(parser)
3642

3743
parser.add_argument(
@@ -130,7 +136,12 @@ def list_all_objects(
130136
Lists all objects in all buckets
131137
"""
132138
# this is for printing help when --help is in the args
133-
parser = inherit_plugin_args(ArgumentParser(PLUGIN_BASE + " la"))
139+
parser = inherit_plugin_args(
140+
ArgumentParser(
141+
PLUGIN_BASE + " la", formatter_class=SortingHelpFormatter
142+
)
143+
)
144+
134145
register_pagination_args_shared(parser)
135146

136147
parsed = parser.parse_args(args)

0 commit comments

Comments
 (0)