Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7536719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:39:10+00:00 2026-05-30T06:39:10+00:00

I’m trying to set up argparse on my python program but it’s not working.

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:39:12+00:00Added an answer on May 30, 2026 at 6:39 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.