1

I'm trying to set up argparse on my python program but it's not working. The arguments I'm trying to process are as follows:

Either '--interactive' OR an integer for the first argument, one of these is required

Any one of either '--xml' OR '--html' OR '--text' OR '--console'. Again, it can be any one of these but one of them is required for the second argument

And finally for the third argument, a '--verbose' flag which is optional.

All of these arguments change variables to True, apart from the integer on the first argument.

This is the code I have at the minute:

import argparse

parser = argparse.ArgumentParser(description='Python Historical Event Calculator.',
                             prog='tempus.py')

inputs = parser.add_mutually_exclusive_group(required=True)
exports = parser.add_mutually_exclusive_group(required=True)

inputs.add_argument('integer', metavar='I', type=float,
               help='percentage to use')

inputs.add_argument('-i','--interactive', dest='bool_interactive', 
               action='store_true', help='enter interactive mode')

exports.add_argument('-x','--xml', dest='bool_xml', action='store_true',
               help='export output as xml')

exports.add_argument('--html', dest='bool_html', action='store_true',
               help='export output as html')

exports.add_argument('-t','--text', dest='bool_text', action='store_true',
               help='export output as plaintext')

exports.add_argument('-c','--console', dest='bool_con', action='store_true',
               help='export output to console')

parser.add_argument('-v','--verbose', dest='verbose', action='store_true',
               help='enter verbose/debug mode', required=False)

args = parser.parse_args()

But I have no idea if I'm on the right track with this though, can anyone help? Does this look about right or have I done it completely wrong?

Edit

I get this traceback when I pass any flag to it:

Traceback (most recent call last):
  File "C:\Users\Callum\Dropbox\Tempus\Feature Tests\argparsetest.py", line 9, in <module>
    help='percentage to use')
  File "C:\Python32\lib\argparse.py", line 1305, in add_argument
    return self._add_action(action)
  File "C:\Python32\lib\argparse.py", line 1528, in _add_action
    raise ValueError(msg)
ValueError: mutually exclusive arguments must be optional
4
  • Why don't you simplify and change it so your --xml, --html, --text, --console options become a single flag, called --output=x, where x can be xml, html, text, or console, and then set a default for that? Similarly, --interactive can be optional, or the integer can be optional. Otherwise you're going to have more logic to determine what takes precedence over others. Commented Feb 23, 2012 at 21:45
  • Do you have an error? If so, please show a traceback. On a first glance your code seems funtional even, if you should consider birryree's arguments. So if you don't get any errors your post belongs -imho- to code-review: codereview.stackexchange.com Commented Feb 23, 2012 at 21:49
  • @Birryree: How would I go about changing those arguments to that format? Commented Feb 23, 2012 at 21:51
  • @DonQuestion: I've added the traceback as an edit :) Commented Feb 23, 2012 at 21:55

2 Answers 2

4

Your error, ValueError: mutually exclusive arguments must be optional, happened because you are adding integer (a positional argument), to a mutually exclusive group. Mutually exclusive groups are only for optional arguments, whereas positional arguments are always required. One solution is to make both interactive and integer optional arguments, and mutually exclusive them.

I originally missed the fact that you used a mutually_exclusive_group on your modes, so that only xml, html, console, or text were specified, but I did change it up if you do like that idea.

This parser would work, it makes your interactive and integer arguments mutually exclusive, and makes mode a choice list.

#!/usr/bin/env python

import argparse

def get_parser():
    parser = argparse.ArgumentParser(prog='tempus.py')
    ex_group = parser.add_mutually_exclusive_group(required=True)
    parser.add_argument('--mode', type=str, choices=['html', 'xml', 'text', 'console'], required=True)
    parser.add_argument('-v', '--verbose', action='store_true')
    ex_group.add_argument('--interactive', action='store_true')
    ex_group.add_argument('--integer', metavar='I', type=float, help='percentage to use')
    return parser

def main():
    aparser = get_parser()
    args = aparser.parse_args()

    print('mode: %s' % args.mode)
    print('integer: %s' % args.integer)
    print('interactive: %s' % args.interactive)
    print('verbose: %s' % args.verbose)

# Only used in Python 2.x
if __name__ == '__main__':
    main()

Sample run:

+> python .\tempus.py
usage: tempus.py [-h] --mode {html,xml,text,console} [-v]
                 (--interactive | --integer I)
tempus.py: error: argument --mode is required

+> python .\tempus.py --mode html
usage: tempus.py [-h] --mode {html,xml,text,console} [-v]
                 (--interactive | --integer I)
tempus.py: error: one of the arguments --interactive --integer is required

+> python .\tempus.py --mode html --integer
usage: tempus.py [-h] --mode {html,xml,text,console} [-v]
                 (--interactive | --integer I)
tempus.py: error: argument --integer: expected one argument

+> python .\tempus.py --mode html --integer 12
mode: html
integer: 12.0
interactive: False
verbose: False
Sign up to request clarification or add additional context in comments.

Comments

0

Change integer to be an optional positional

inputs.add_argument('integer', nargs='?', metavar='I', type=float, ...)

With this, -i and 33 would both be accepted by the inputs group, -i 33 would not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.