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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:04:30+00:00 2026-06-06T06:04:30+00:00

Given: import argparse parser = argparse.ArgumentParser() parser.add_argument(‘–foo’) parser.add_argument(‘–bar’) print(parser.parse_args(‘–foo 1’.split())) How do I make

  • 0

Given:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--foo')
parser.add_argument('--bar')

print(parser.parse_args('--foo 1'.split()))

How do I

  • make at least one of "foo, bar" mandatory: --foo x, --bar y and --foo x --bar y are fine
  • make at most one of "foo, bar" mandatory: --foo x or --bar y are fine, --foo x --bar y is not
  • 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-06-06T06:04:32+00:00Added an answer on June 6, 2026 at 6:04 am

    I think you are searching for something like mutual exclusion (at least for the second part of your question).

    This way, only --foo or --bar will be accepted, not both.

    import argparse
    
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--foo',action=.....)
    group.add_argument('--bar',action=.....)
    args = parser.parse_args()
    

    BTW, just found another question referring to the same kind of issue.

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

Sidebar

Related Questions

This code import argparse parser = argparse.ArgumentParser() parser.add_argument('target', help='Specifiy who to attack!') magic =
Given this : import os import subprocess def check_server(): cl = subprocess.Popen([nmap,10.7.1.71], stdout=subprocess.PIPE) result
i Want to import a given css or javascript file depending os some conditions,
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
Given the following simple program: import wx class TestDraw(wx.Panel): def __init__(self,parent=None,id=-1): wx.Panel.__init__(self,parent,id,style=wx.TAB_TRAVERSAL) self.SetBackgroundColour(#FFFFFF) self.Bind(wx.EVT_PAINT,self.onPaint)
Given is the following code: import java.io.Serializable; import java.util.concurrent.locks.ReentrantLock; class Data { private int
Given a Django model likeso: from django.db import models class MyModel(models.Model): textfield = models.TextField()
In one of the example servers given at golang.org: package main import ( flag
Given the code: import clr clr.AddReference('System') from System.Text.RegularExpressions import * def ReplaceBlank(st): return Regex.Replace(
Given the following code : import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Color; import

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.