codekingpro/portable-devtools
114k
1from textwrap import dedent2from httpie.cli.argparser import HTTPieManagerArgumentParser3from httpie.cli.options import Qualifiers, ARGPARSE_QUALIFIER_MAP, map_qualifiers, parser_to_parser_spec4from httpie import __version__5 6CLI_SESSION_UPGRADE_FLAGS = [7 {8 'flags': ['--bind-cookies'],9 'action': 'store_true',10 'default': False,11 'help': 'Bind domainless cookies to the host that session belongs.'12 }13]14 15COMMANDS = {16 'cli': {17 'help': 'Manage HTTPie for Terminal',18 'export-args': [19 'Export available options for the CLI',20 {21 'flags': ['-f', '--format'],22 'choices': ['json'],23 'help': 'Format to export in.',24 'default': 'json'25 }26 ],27 'check-updates': [28 'Check for updates'29 ],30 'sessions': {31 'help': 'Manage HTTPie sessions',32 'upgrade': [33 'Upgrade the given HTTPie session with the latest '34 'layout. A list of changes between different session versions '35 'can be found in the official documentation.',36 {37 'dest': 'hostname',38 'metavar': 'HOSTNAME',39 'help': 'The host this session belongs.'40 },41 {42 'dest': 'session',43 'metavar': 'SESSION_NAME_OR_PATH',44 'help': 'The name or the path for the session that will be upgraded.'45 },46 *CLI_SESSION_UPGRADE_FLAGS47 ],48 'upgrade-all': [49 'Upgrade all named sessions with the latest layout. A list of '50 'changes between different session versions can be found in the official '51 'documentation.',52 *CLI_SESSION_UPGRADE_FLAGS53 ],54 }55 }56}57 58 59COMMANDS['plugins'] = COMMANDS['cli']['plugins'] = {60 'help': 'Manage HTTPie plugins.',61 'install': [62 'Install the given targets from PyPI '63 'or from a local paths.',64 {65 'dest': 'targets',66 'metavar': 'TARGET',67 'nargs': Qualifiers.ONE_OR_MORE,68 'help': 'targets to install'69 }70 ],71 'upgrade': [72 'Upgrade the given plugins',73 {74 'dest': 'targets',75 'metavar': 'TARGET',76 'nargs': Qualifiers.ONE_OR_MORE,77 'help': 'targets to upgrade'78 }79 ],80 'uninstall': [81 'Uninstall the given HTTPie plugins.',82 {83 'dest': 'targets',84 'metavar': 'TARGET',85 'nargs': Qualifiers.ONE_OR_MORE,86 'help': 'targets to install'87 }88 ],89 'list': [90 'List all installed HTTPie plugins.'91 ],92}93 94 95def missing_subcommand(*args) -> str:96 base = COMMANDS97 for arg in args:98 base = base[arg]99 100 assert isinstance(base, dict)101 subcommands = ', '.join(map(repr, base.keys()))102 return f'Please specify one of these: {subcommands}'103 104 105def generate_subparsers(root, parent_parser, definitions, spec):106 action_dest = '_'.join(parent_parser.prog.split()[1:] + ['action'])107 actions = parent_parser.add_subparsers(108 dest=action_dest109 )110 for command, properties in definitions.items():111 is_subparser = isinstance(properties, dict)112 properties = properties.copy()113 114 descr = properties.pop('help', None) if is_subparser else properties.pop(0)115 command_parser = actions.add_parser(command, description=descr)116 command_parser.root = root117 if is_subparser:118 generate_subparsers(root, command_parser, properties, spec)119 continue120 121 group = spec.add_group(parent_parser.prog + ' ' + command, description=descr)122 for argument in properties:123 argument = argument.copy()124 flags = argument.pop('flags', [])125 command_parser.add_argument(*flags, **map_qualifiers(argument, ARGPARSE_QUALIFIER_MAP))126 group.add_argument(*flags, **argument)127 128 129parser = HTTPieManagerArgumentParser(130 prog='httpie',131 description=dedent(132 '''133 Managing interface for the HTTPie itself. <https://httpie.io/docs#manager>134 135 Be aware that you might be looking for http/https commands for sending136 HTTP requests. This command is only available for managing the HTTTPie137 plugins and the configuration around it.138 '''139 ),140)141 142parser.add_argument(143 '--debug',144 action='store_true',145 default=False,146 help='''147 Prints the exception traceback should one occur, as well as other148 information useful for debugging HTTPie itself and for reporting bugs.149 150 '''151)152 153parser.add_argument(154 '--traceback',155 action='store_true',156 default=False,157 help='''158 Prints the exception traceback should one occur.159 160 '''161)162 163parser.add_argument(164 '--version',165 action='version',166 version=__version__,167 help='''168 Show version and exit.169 170 '''171)172 173man_page_hint = '''174If you are looking for the man pages of http/https commands, try one of the following:175 $ man http176 $ man https177 178'''179 180options = parser_to_parser_spec(parser, man_page_hint=man_page_hint, source_file=__file__)181generate_subparsers(parser, parser, COMMANDS, options)182 