finish parsing and add error for empty config subcommand

This commit is contained in:
Divyansh Singh 2023-05-26 22:44:31 +05:30
parent f4a169fdf3
commit dbb778a4d6
2 changed files with 26 additions and 7 deletions

View file

@ -42,6 +42,9 @@ def parse_subparser_arguments(unparsed_arguments, subparsers):
for subparser_name, aliases in SUBPARSER_ALIASES.items() for subparser_name, aliases in SUBPARSER_ALIASES.items()
for alias in aliases for alias in aliases
} }
subcommand_parsers_mapping = {
'config': ['bootstrap'],
}
# If the "borg" action is used, skip all other subparsers. This avoids confusion like # If the "borg" action is used, skip all other subparsers. This avoids confusion like
# "borg list" triggering borgmatic's own list action. # "borg list" triggering borgmatic's own list action.
@ -70,7 +73,18 @@ def parse_subparser_arguments(unparsed_arguments, subparsers):
if item in subparsers: if item in subparsers:
remaining_arguments.remove(item) remaining_arguments.remove(item)
if canonical_name not in subcommand_parsers_mapping:
arguments[canonical_name] = parsed arguments[canonical_name] = parsed
else:
arguments[canonical_name] = None
for argument in arguments:
if arguments[argument] == None:
for subcommand in subcommand_parsers_mapping[argument]:
if subcommand not in arguments:
raise ValueError("Missing subcommand for {}. Expected one of {}".format(
argument, subcommand_parsers_mapping[argument]
))
# If no actions are explicitly requested, assume defaults. # If no actions are explicitly requested, assume defaults.
if not arguments and '--help' not in unparsed_arguments and '-h' not in unparsed_arguments: if not arguments and '--help' not in unparsed_arguments and '-h' not in unparsed_arguments:
@ -81,8 +95,9 @@ def parse_subparser_arguments(unparsed_arguments, subparsers):
remaining_arguments = list(unparsed_arguments) remaining_arguments = list(unparsed_arguments)
# Now ask each subparser, one by one, to greedily consume arguments. # Now ask each subparser, one by one, to greedily consume arguments, from last to first. This
for subparser_name, subparser in subparsers.items(): # allows subparsers to consume arguments before their parent subparsers do.
for subparser_name, subparser in reversed(subparsers.items()):
if subparser_name not in arguments.keys(): if subparser_name not in arguments.keys():
continue continue
@ -937,7 +952,7 @@ def make_parsers():
) )
borg_group.add_argument('-h', '--help', action='help', help='Show this help message and exit') borg_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')
return top_level_parser, subparsers return top_level_parser, subparsers, config_subparsers
def parse_arguments(*unparsed_arguments): def parse_arguments(*unparsed_arguments):
@ -945,10 +960,13 @@ def parse_arguments(*unparsed_arguments):
Given command-line arguments with which this script was invoked, parse the arguments and return Given command-line arguments with which this script was invoked, parse the arguments and return
them as a dict mapping from subparser name (or "global") to an argparse.Namespace instance. them as a dict mapping from subparser name (or "global") to an argparse.Namespace instance.
''' '''
top_level_parser, subparsers = make_parsers() top_level_parser, subparsers, config_subparsers = make_parsers()
subparser_choices = subparsers.choices.copy()
subparser_choices.update(config_subparsers.choices)
arguments, remaining_arguments = parse_subparser_arguments( arguments, remaining_arguments = parse_subparser_arguments(
unparsed_arguments, subparsers.choices unparsed_arguments, subparser_choices
) )
arguments['global'] = top_level_parser.parse_args(remaining_arguments) arguments['global'] = top_level_parser.parse_args(remaining_arguments)

View file

@ -616,6 +616,7 @@ def collect_configuration_run_summary_logs(configs, arguments):
if 'extract' in arguments or 'mount' in arguments: if 'extract' in arguments or 'mount' in arguments:
validate.guard_single_repository_selected(repository, configs) validate.guard_single_repository_selected(repository, configs)
if 'config' not in arguments:
validate.guard_configuration_contains_repository(repository, configs) validate.guard_configuration_contains_repository(repository, configs)
except ValueError as error: except ValueError as error:
yield from log_error_records(str(error)) yield from log_error_records(str(error))