def build_parser(parser: Parser) -> Parser: parser.add_argument('numbers', type=int, nargs='+') return parser
# built-in from functools import reduce # project from dephell_argparse import CommandHandler, Parser parser = Parser() @parser.add_command class PingCommand(CommandHandler): def __call__(self): self.print('pong') return 14 @parser.add_command class MathSumCommand(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 15
# built-in from functools import reduce # project from dephell_argparse import CommandHandler, Parser parser = Parser() @parser.add_command class PingCommand(CommandHandler): def __call__(self): self.print('pong') return 0 @parser.add_command class MathSumCommand(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
# 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())
from logging import getLogger from pdb import post_mortem from sys import argv from typing import List # 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']
# 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())