D
D
Delsian2016-01-26 19:40:34
Python
Delsian, 2016-01-26 19:40:34

ArgParse: mutually exclusive arguments in place of positional?

You need to pass data to the script either from a file or from the command line.
I am using the construct:

parser_write = subparsers.add_parser('write', help='Write вфеф')
    parser_write.add_argument('address', type=int)
    parser_write_data = parser_write.add_mutually_exclusive_group(required=True)
    parser_write_data.add_argument('-f', help='Data as binary file', type=argparse.FileType(), metavar=('FILE'))
    parser_write_data.add_argument('data', help='Data as hex string', nargs='?')

Basically, everything works as expected, but ugly Help output:
usage: script.py write [-h] [-f FILE] address [data]

positional arguments:
  address
  data        Data as hex string

optional arguments:
  -h, --help  show this help message and exit
  -f FILE     Data as binary file

I want data and file to be combined into a construction like
usage: script.py write [-h] address {data|-f FILE}
Is this possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abcd0x00, 2016-01-27
@Delsian

parser_write = subparsers.add_parser(
    'write',
    help='Write вфеф',
    usage='%(prog)s [-h] address {data|-f FILE}'
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question