@@ -110,6 +110,53 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
110110 # if not spec was found and we weren't baking, we're doomed
111111 sys .exit (ExitCodes .ARGUMENT_ERROR )
112112
113+ if parsed .command == "set-custom-alias" or parsed .command == "remove-custom-alias" :
114+ # Ensure both --command and --alias are provided
115+ # TODO: Can we use argparse for these
116+ if len (args ) == 4 :
117+ if args [0 ] != "--command" or args [2 ] != "--alias" :
118+ print ("Usage: linode-cli set-custom-alias --command [COMMAND] --alias [ALIAS]" , file = sys .stderr )
119+ sys .exit (ExitCodes .ARGUMENT_ERROR )
120+
121+ # Get the indexes for --command and --alias
122+ try :
123+ command_index = args .index ("--command" ) + 1
124+ alias_index = args .index ("--alias" ) + 1
125+ except ValueError :
126+ print ("Both --command and --alias arguments are required." , file = sys .stderr )
127+ sys .exit (ExitCodes .ARGUMENT_ERROR )
128+
129+ if command_index >= len (args ) or alias_index >= len (args ):
130+ print ("Both --command and --alias arguments must have valid values." , file = sys .stderr )
131+ sys .exit (ExitCodes .ARGUMENT_ERROR )
132+
133+ # Retrieve the command and alias from arguments
134+ command = args [command_index ]
135+ alias = args [alias_index ]
136+
137+ # Check if the command is valid
138+ if command not in cli .ops :
139+ print (f"Error: '{ command } ' is not a valid command." , file = sys .stderr )
140+ sys .exit (ExitCodes .ARGUMENT_ERROR )
141+
142+ # Set the alias if it does not already exist
143+ if parsed .command == "set-custom-alias" :
144+ if (alias , command ) not in cli .config .get_custom_aliases ().items ():
145+ cli .config .set_custom_alias (alias , command )
146+ print (f"Custom alias '{ alias } ' set for command '{ command } '" )
147+ else :
148+ print (f"Custom alias '{ alias } ' already set for command '{ command } '" )
149+
150+ # Remove the alias if it already exists
151+ if parsed .command == "remove-custom-alias" :
152+ if (alias , command ) in cli .config .get_custom_aliases ().items ():
153+ cli .config .remove_custom_alias (alias , command )
154+ print (f"Custom alias '{ alias } ' removed for command '{ command } '" )
155+ else :
156+ print (f"Custom alias '{ alias } ' does not exist for command '{ command } '" )
157+
158+ sys .exit (ExitCodes .SUCCESS )
159+
113160 if parsed .command == "register-plugin" :
114161 if parsed .action is None :
115162 print ("register-plugin requires a module name!" , file = sys .stderr )
@@ -219,6 +266,7 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
219266 parsed .command not in cli .ops
220267 and parsed .command not in plugins .available (cli .config )
221268 and parsed .command not in HELP_TOPICS
269+ and parsed .command not in cli .config .get_custom_aliases ().keys ()
222270 ):
223271 print (f"Unrecognized command { parsed .command } " , file = sys .stderr )
224272 sys .exit (ExitCodes .UNRECOGNIZED_COMMAND )
0 commit comments