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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:38:04+00:00 2026-06-12T05:38:04+00:00

I have a program that accepts a file as input, does some work with

  • 0

I have a program that accepts a file as input, does some work with the contents of the file and the pushes it out to a server. I want to add an optional command line switch to specify a “dry run” of the program whereby it does all the file crunching, but skips doing the writes out to the server. I am using argparse to bring in the command line arguments, but I don’t see a way to do an “OR” function between the arguments. Here is what I’m more or less looking for…

Allowable options:

1) prog.py inputfile servername

2) prog.py inputfile -d

3) prog.py inputfile -d servername

Dissallowed:

1) prog.py inputfile

I want to ensure that either the server name “OR” the dry run flag are on the command line. And, if both are there… that’s OK too. (hence being an OR and not an XOR). If I use mutually exclusive with required=true, I can get the XOR; but, I can’t seem to figure out how to do this as an “OR” where both can be present. To complicate matters, the server name is a positional argument and the dry run flag is an optional argument that could be anywhere on the command line. Does anyone have an idea on how to pull this off?

  • 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-12T05:38:05+00:00Added an answer on June 12, 2026 at 5:38 am

    Here’s what I would do:

    import argparse
    
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--dry-run', action='store_true')
    parser.add_argument('input_file', type=argparse.FileType('r'))
    parser.add_argument('servername', nargs='?')
    
    args = parser.parse_args()
    if args.servername is None and not args.dry_run:
        parser.error("Option 'servername' is required when not in dry-run mode.")
    
    print args
    

    Examples:

    $ ./prog.py inputfile servername
    Namespace(dry_run=False, input_file=<open file 'inputfile', mode 'r' at 0x283440>, servername='servername')
    $ ./prog.py inputfile -d
    Namespace(dry_run=True, input_file=<open file 'inputfile', mode 'r' at 0x2cf440>, servername=None)
    $ ./prog.py -d inputfile servername
    Namespace(dry_run=True, input_file=<open file 'inputfile', mode 'r' at 0x1f4440>, servername='servername')
    $ ./prog.py inputfile
    usage: prog.py [-h] [-d] input_file [servername]
    prog.py: error: Option 'servername' is required when not in dry-run mode.
    

    You could also do this using a custom action and it has the same effect:

    class ServernameAction(argparse.Action):
        def __call__(self, parser, namespace, values, option_string=None):
            if values is None and not namespace.dry_run:
                parser.error("Option 'servername' is required when not in dry-run mode.")
            setattr(namespace, self.dest, values)
    
    ...
    parser.add_argument('servername', nargs='?', action=ServernameAction)
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that accepts two file names as arguments: it reads the
I have a Haskell program that shows a prompt and then accepts input from
I have a program that accepts a string parameter. I create a batch file
i have a command line executable program that accepts a configuration file as it's
I have a very simple question. I have a program that accepts an integer
I have program that runs fast enough. I want to see the number of
I have a program that gets a JSON from the server using getJSON and
I have a program that reads from a file that grabs 4 bytes from
I have a program that I want to distribute, without giving the source code
Our company is using some software that ONLY accepts input from an Imaging Device

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.