# external from dephell_argparse import Command, Parser # app from .commands import COMMANDS from .constants import ReturnCodes from .exceptions import ExtraException logger = getLogger('dephell.cli') parser = Parser( description='Manage dependencies, projects, virtual environments.', usage='dephell COMMAND [OPTIONS]', ) for handler in COMMANDS.values(): parser.add_command(handler=handler) def main(argv: List[str]) -> int: # print help if not argv: parser._print_message(parser.format_help()) return ReturnCodes.OK.value if len(argv) == 1 and argv[0] in ('--help', 'help', 'commands'): parser._print_message(parser.format_help()) return ReturnCodes.OK.value if len(argv) == 1 and argv[0] in ('-v', '--version'): argv = ['inspect', 'self'] # rewrite argv to get help about command if len(argv) >= 1 and argv[0] in ('--help', 'help'):
# project from dephell_argparse import CommandHandler, Parser class SumCommand(CommandHandler): """Return sum of numbers """ @staticmethod def build_parser(parser: Parser) -> Parser: parser.add_argument('numbers', type=int, nargs='+') return parser def __call__(self): result = sum(self.args.numbers) self.print(result) return 0 parser = Parser() parser.add_command(SumCommand) if __name__ == '__main__': exit(parser.handle())
# project from dephell_argparse import Parser def hello(args) -> int: """Say "hello"! """ print('hello!') return 0 parser = Parser() parser.add_command(hello) if __name__ == '__main__': exit(parser.handle())