Skip to content

Commit 9a8fd97

Browse files
Utilize argparse
1 parent 6954c83 commit 9a8fd97

File tree

2 files changed

+23
-39
lines changed

2 files changed

+23
-39
lines changed

linodecli/__init__.py

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -108,65 +108,33 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
108108
sys.exit(ExitCodes.ARGUMENT_ERROR)
109109

110110
if parsed.command in ("set-custom-alias", "remove-custom-alias"):
111-
# Ensure both --command and --alias are provided
112-
# TODO: Can we use argparse for these
113-
if len(args) == 4:
114-
if args[0] != "--command" or args[2] != "--alias":
115-
print(
116-
"Usage: linode-cli set-custom-alias --command "
117-
"[COMMAND] --alias [ALIAS]",
118-
file=sys.stderr,
119-
)
120-
sys.exit(ExitCodes.ARGUMENT_ERROR)
121-
122-
# Get the indexes for --command and --alias
123-
try:
124-
command_index = args.index("--command") + 1
125-
alias_index = args.index("--alias") + 1
126-
except ValueError:
111+
if not parsed.target_command or not parsed.alias:
127112
print(
128-
"Both --command and --alias arguments are required.",
113+
"Both --target-command and --alias must be provided.",
129114
file=sys.stderr,
130115
)
131116
sys.exit(ExitCodes.ARGUMENT_ERROR)
132117

133-
if command_index >= len(args) or alias_index >= len(args):
134-
print(
135-
"Both --command and --alias arguments must have valid values.",
136-
file=sys.stderr,
137-
)
138-
sys.exit(ExitCodes.ARGUMENT_ERROR)
118+
command = parsed.target_command
119+
alias = parsed.alias
139120

140-
# Retrieve the command and alias from arguments
141-
command = args[command_index]
142-
alias = args[alias_index]
143-
144-
# Check if the command is valid
145121
if command not in cli.ops:
146-
print(
147-
f"Error: '{command}' is not a valid command.", file=sys.stderr
148-
)
122+
print(f"Error: '{command}' is not a valid command.", file=sys.stderr)
149123
sys.exit(ExitCodes.ARGUMENT_ERROR)
150124

151-
# Set the alias if it does not already exist
152125
if parsed.command == "set-custom-alias":
153126
if (alias, command) not in cli.config.get_custom_aliases().items():
154127
cli.config.set_custom_alias(alias, command)
155128
print(f"Custom alias '{alias}' set for command '{command}'")
156129
else:
157-
print(
158-
f"Custom alias '{alias}' already set for command '{command}'"
159-
)
130+
print(f"Custom alias '{alias}' already set for command '{command}'")
160131

161-
# Remove the alias if it already exists
162132
if parsed.command == "remove-custom-alias":
163133
if (alias, command) in cli.config.get_custom_aliases().items():
164134
cli.config.remove_custom_alias(alias, command)
165135
print(f"Custom alias '{alias}' removed for command '{command}'")
166136
else:
167-
print(
168-
f"Custom alias '{alias}' does not exist for command '{command}'"
169-
)
137+
print(f"Custom alias '{alias}' does not exist for command '{command}'")
170138

171139
sys.exit(ExitCodes.SUCCESS)
172140

linodecli/arg_helpers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ def register_args(parser: ArgumentParser) -> ArgumentParser:
6767
help="Prints version information and exits.",
6868
)
6969

70+
parser.add_argument(
71+
"--target-command",
72+
"-t",
73+
nargs="?",
74+
type=str,
75+
help="The command to set or remove an alias for.",
76+
)
77+
78+
parser.add_argument(
79+
"--alias",
80+
"-a",
81+
nargs="?",
82+
type=str,
83+
help="The alias to set or remove.",
84+
)
85+
7086
# Register shared argument groups
7187
register_output_args_shared(parser)
7288
register_pagination_args_shared(parser)

0 commit comments

Comments
 (0)