ogcli: improve handling of subcommand errors

If no subcommand or an invalid one is specified print an error message
followed by ogcli --help. Also exit with status code 1.
master
Jose M. Guisado 2023-10-06 10:49:10 +02:00
parent b1fc9cabb4
commit c224c596f4
1 changed files with 8 additions and 2 deletions

10
ogcli
View File

@ -42,9 +42,15 @@ class CLI():
if not attr.startswith('_')]) if not attr.startswith('_')])
args = parser.parse_args(sys.argv[1:2]) args = parser.parse_args(sys.argv[1:2])
if args.command is None:
print('Missing subcommand', file=sys.stderr)
parser.print_help(file=sys.stderr)
sys.exit(1)
if not hasattr(self.ogcli, args.command): if not hasattr(self.ogcli, args.command):
parser.print_help() print('Invalid subcommand', file=sys.stderr)
sys.exit('Unknown command') parser.print_help(file=sys.stderr)
sys.exit(1)
# Call the command with the same name. # Call the command with the same name.
getattr(self.ogcli, args.command)(sys.argv[2:]) getattr(self.ogcli, args.command)(sys.argv[2:])