diff options
author | Jose M. Guisado <jguisado@soleta.eu> | 2023-10-06 10:57:53 +0200 |
---|---|---|
committer | Jose M. Guisado <jguisado@soleta.eu> | 2023-10-06 11:30:43 +0200 |
commit | d2596294456da9f5ffa809a67a2d68fa21ce1fb3 (patch) | |
tree | ba2231fa68b458b56a9a7d3bd832dd4fb3cc38bc /cli/cli.py | |
parent | 35b4b1422a97dafd6c8ebb364409a4fcab7535ab (diff) |
cli: better error handling for subcommand args
Some subcommands need following arguments (e.g.: ogcli list ...), check
if following arguments are present. If no required following argument is
detected then print an error message, show help and exit with status
code 1.
Diffstat (limited to 'cli/cli.py')
-rw-r--r-- | cli/cli.py | 63 |
1 files changed, 54 insertions, 9 deletions
@@ -73,8 +73,13 @@ class OgCLI(): 'servers'] parser = argparse.ArgumentParser(prog='ogcli list') parser.add_argument('item', choices=choices) - parsed_args = parser.parse_args([args[0]]) + if not args: + print('Missing list subcommand', file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) + + parsed_args = parser.parse_args([args[0]]) if parsed_args.item == 'clients': OgClient.list_clients(self.rest) elif parsed_args.item == 'client': @@ -96,8 +101,13 @@ class OgCLI(): choices = ['modes', 'mode', 'server'] parser = argparse.ArgumentParser(prog='ogcli set') parser.add_argument('item', choices=choices) - parsed_args = parser.parse_args([args[0]]) + if not args: + print('Missing set subcommand', file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) + + parsed_args = parser.parse_args([args[0]]) if parsed_args.item in ['modes', 'mode']: OgModes.set_modes(self.rest, args[1:]) elif parsed_args.item == 'server': @@ -107,8 +117,13 @@ class OgCLI(): choices = ['reboot', 'refresh', 'poweroff', 'wol'] parser = argparse.ArgumentParser(prog='ogcli send') parser.add_argument('send_obj', choices=choices) - parsed_args = parser.parse_args([args[0]]) + if not args: + print('Missing send subcommand', file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) + + parsed_args = parser.parse_args([args[0]]) if parsed_args.send_obj == 'wol': OgWol.send_wol(self.rest, args[1:]) elif parsed_args.send_obj == 'poweroff': @@ -122,8 +137,13 @@ class OgCLI(): choices = ['image'] parser = argparse.ArgumentParser(prog='ogcli restore') parser.add_argument('send_obj', choices=choices) - parsed_args = parser.parse_args([args[0]]) + if not args: + print('Missing restore subcommand', file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) + + parsed_args = parser.parse_args([args[0]]) if parsed_args.send_obj == 'image': OgImage.restore_image(self.rest, args[1:]) @@ -131,8 +151,13 @@ class OgCLI(): choices = ['image'] parser = argparse.ArgumentParser(prog='ogcli create') parser.add_argument('create_obj', choices=choices) - parsed_args = parser.parse_args([args[0]]) + if not args: + print('Missing create subcommand', file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) + + parsed_args = parser.parse_args([args[0]]) if parsed_args.create_obj == 'image': OgImage.create_image(self.rest, args[1:]) @@ -140,8 +165,13 @@ class OgCLI(): choices = ['disk'] parser = argparse.ArgumentParser(prog='ogcli setup') parser.add_argument('setup_obj', choices=choices) - parsed_args = parser.parse_args([args[0]]) + if not args: + print('Missing setup subcommand', file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) + + parsed_args = parser.parse_args([args[0]]) if parsed_args.setup_obj == 'disk': OgDisk.setup_disk(self.rest, args[1:]) @@ -149,8 +179,13 @@ class OgCLI(): choices = ['image'] parser = argparse.ArgumentParser(prog='ogcli update') parser.add_argument('update_obj', choices=choices) - parsed_args = parser.parse_args([args[0]]) + if not args: + print('Missing update subcommand', file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) + + parsed_args = parser.parse_args([args[0]]) if parsed_args.update_obj == 'image': OgImage.update_image(self.rest, args[1:]) @@ -158,8 +193,13 @@ class OgCLI(): choices = ['server'] parser = argparse.ArgumentParser(prog='ogcli delete') parser.add_argument('delete_obj', choices=choices) - parsed_args = parser.parse_args([args[0]]) + if not args: + print('Missing delete subcommand', file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) + + parsed_args = parser.parse_args([args[0]]) if parsed_args.delete_obj == 'server': OgServer.delete_server(self.rest, args[1:]) @@ -167,7 +207,12 @@ class OgCLI(): choices = ['server'] parser = argparse.ArgumentParser(prog='ogcli add') parser.add_argument('add_obj', choices=choices) - parsed_args = parser.parse_args([args[0]]) + if not args: + print('Missing add subcommand', file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) + + parsed_args = parser.parse_args([args[0]]) if parsed_args.add_obj == 'server': OgServer.add_server(self.rest, args[1:]) |