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 6918363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:50:56+00:00 2026-05-27T09:50:56+00:00

I need some help regarding using argparse. What I want to achieve is that

  • 0

I need some help regarding using argparse. What I want to achieve is that I need to pass in only one argument, it could be one of the followings: --k, --r, --b, --p,(ignore the rest). If the argument count is not 1, print “usage” information and quit. Also the program needs to know which flag is passed in in order to create corresponding object. I tried several times but I doesn’t work, can anyone give me a hint on this? Thanks.

  • 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-27T09:50:57+00:00Added an answer on May 27, 2026 at 9:50 am

    What you need to use to accomplish that is a mutually exclusive group:

    import argparse
    
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-k', action='store_true')
    group.add_argument('-r', action='store_true')
    group.add_argument('-b', action='store_true')
    group.add_argument('-p', action='store_true')
    parser.parse_args()
    

    As it can be seen in the example below, only one option in a mutually exclusive group is allowed at the same time:

    $ python test.py -k -r -b -p
    usage: test.py [-h] [-k | -r | -b | -p]
    test.py: error: argument -r: not allowed with argument -k
    

    To check which flag was passed, you just need to look at the argparse.Namespace object returned by parse_args method (the flag passed will be set to True).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help regarding algorithm for randomness. So Problem is. There are 50
I need some help regarding the arrays in Perl This is the constructor I
Need some help about with Memcache. I have created a class and want to
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help assigning a mouseover event to display some icons that start out
I am using unity 2.0 with MVC3 and need some help understanding the LifeTimeManagers.
I need some help with concepts and terminology regarding website 'root' urls and directories.
I need some help regarding jquery arrays. var queryArr; $(markersArray).each(function(index) { var _locationId =
I really need some help regarding to my coding. In my coding, I need

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.