import argparse from app.guide.profile_types import types as profile_types from app.guide.quality_types import types as quality_types # class args: pass class _NoAction(argparse.Action): def __init__(self, **kwargs): kwargs.setdefault('default', argparse.SUPPRESS) kwargs.setdefault('nargs', 0) super(_NoAction, self).__init__(**kwargs) def __call__(self, parser, namespace, values, option_string=None): pass def _add_choices_argument(parser, variable_name, help_text, choices: dict): parser.register('action', 'none', _NoAction) parser.add_argument(variable_name, help=help_text, metavar=variable_name.upper(), choices=choices.keys()) group = parser.add_argument_group(title=f'Choices for {variable_name.upper()}') for choice,choice_help in choices.items(): group.add_argument(choice, help=choice_help, action='none') def setup_and_parse_args(args_override=None): parent_p = argparse.ArgumentParser(add_help=False) parent_p.add_argument('--base-uri', help='The base URL for your Sonarr/Radarr instance, for example `http://localhost:8989`. Required if not doing --preview.') parent_p.add_argument('--api-key', help='Your API key. Required if not doing --preview.') parent_p.add_argument('--preview', help='Only display the processed markdown results and nothing else.', action='store_true', default=False) parent_p.add_argument('--debug', help='Display additional logs useful for development/debug purposes', action='store_true', default=False) parent_p.add_argument('--config', help='The configuration YAML file to use. If not specified, the script will look for `trash.yml` in the same directory as the `trash.py` script.') parser = argparse.ArgumentParser(description='Automatically mirror TRaSH guides to your Sonarr/Radarr instance.') subparsers = parser.add_subparsers(description='Operations specific to different parts of the TRaSH guides', dest='subcommand') # Subcommands for 'profile' profile_p = subparsers.add_parser('profile', help='Pages of the guide that define profiles', parents=[parent_p]) _add_choices_argument(profile_p, 'type', 'The specific guide type/page to pull data from.', {type: data.get('cmd_help') for type, data in profile_types.items()}) profile_p.add_argument('--tags', help='Tags to assign to the profiles that are created or updated. These tags will replace any existing tags when updating profiles.', nargs='+') profile_p.add_argument('--strict-negative-scores', help='Any negative scores get added to the list of "Must Not Contain" items', action='store_true') # Subcommands for 'quality' quality_p = subparsers.add_parser('quality', help='Pages in the guide that provide quality definitions', parents=[parent_p]) _add_choices_argument(quality_p, 'type', 'The specific guide type/page to pull data from.', {type: data.get('cmd_help') for type, data in quality_types.items()}) quality_p.add_argument('--preferred-percentage', help='A percentage value that determines the preferred quality, when needed. Default is 100. Value is interpolated between the min (0%%) and max (100%%) value for each table row.', type=int, default=100, metavar='[0-100]') args = parser.parse_args(args=args_override) if not args.subcommand: parser.print_help() exit(1) return args