Pergunta

using argparse i have made a command line tool, which takes below arguments,

usage: ipush [-h] [-v] [-c] [-to [TO]] [-V] [-p PATCHES] [-d DIFF]

from the below code..

parser = argparse.ArgumentParser(prog='ipush',
    description='Utility to push the last commit and email the color diff')
parser.add_argument('-v', '--verbose', action='store_true',
    help='if enabled will spit every command and its resulting data.')
parser.add_argument('-c', '--compose', action='store_true',
    help='compose message in default git editor to be sent prefixed with diff')
parser.add_argument('-to', type=validate_address, default=os.getenv('myemail'),
    help='enter a valid email you want to send to.', nargs='?')
parser.add_argument('-V', '--version', action='version',
    version='%(prog)s 1.0')
parser.add_argument('-p', '--patches', type=int, default=0,
    help='total number of pathces of last commits to email')
parser.add_argument('-d', '--diff', required=False, default='HEAD^ HEAD',
    help='if present pass arguments to it as you \
    will do to git diff in inverted commas')

is it possible to display TO in [-to [TO]] PATCHES in [-p PATCHES] and DIFF in [-d DIFF] with a different text?

Foi útil?

Solução

Yes, as described in the argparse documentation:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', metavar='YYY')
>>> parser.add_argument('bar', metavar='XXX')
>>> parser.parse_args('X --foo Y'.split())
Namespace(bar='X', foo='Y')
>>> parser.print_help()
usage:  [-h] [--foo YYY] XXX

positional arguments:
 XXX

optional arguments:
 -h, --help  show this help message and exit
 --foo YYY
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top