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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:44:20+00:00 2026-06-15T22:44:20+00:00

Let’s say I have a simple program called readfile.py which supports command line arguments

  • 0

Let’s say I have a simple program called readfile.py which supports command line arguments by using Python’s argparse.

The program reads from a file specified by the positional argument input. Without specifying additional arguments, this program is not very exciting. It just reads from input and exits. The optional argument --output [OUTPUT_FILE] signifies that the input file should be written to a file. OUTPUT_FILE is also optional. If it is not specified, the input should be written to default.out.

I set up my argument parser like this:

parser = argparse.ArgumentParser(description='Read from a file')
parser.add_argument(
    'input',
    help='file to read from')
parser.add_argument(
    '--output',
    nargs='?',
    const='default.out',
    default=None,
    help="""write file to %(metavar)s. If %(metavar)s isn't
         specified, write file to %(const)s.""",
    metavar='OUTPUT_FILE')
args = parser.parse_args()
return args.file, args.output_file

Note I use default=None so that if --ouput doesn’t appear on the command line, I can detect its absence with None.

Which gives a usage signature like this:

usage: readfile.py [-h] [--output [OUTPUT_FILE]] input

This handles arguments as expected if I run

python readfile.py input.in --output somefile.out

or

python readfile.py --output somefile.out input

It sets output to default.out if I run

python readfile.py input.in --output

but if I run

python readfile.py --output input.in

it complains about there being too few arguments. I thought argparse would be ‘smart’ enough to interpret this pattern, setting input to input.in and output to default.out. The generated usage even suggests this.

Am I missing something?

  • 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-15T22:44:21+00:00Added an answer on June 15, 2026 at 10:44 pm

    No, it will not recognize this. You are in the last example clearly passing in input.in to --output.

    I would recommend that you flip the default and the const parameters. You want the default to be default.out, so it should be default='default.out'. const can then be set to something else for the case of having an empty --output parameter.

    That way you don’t have to add the --output parameter unless you want to change the default, and you can use an empty --output parameter if you want to output to stdout for example.

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

Sidebar

Related Questions

Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have a method in java, which looks up a user in
Let's say I have a main folder in my website named test which contains
Let's say I have an abstract parent class called shape, and that there are
Let's say I have an facebook application running using the JS SDK. First user
Let's say I have a simple form in the administration of my Rails 2.x
Let's say I have a simple UserControl with no codebehind: <UserControl xmlns= .... x:Class=TrafficLight.LightControl>
Let's say I have an Instant Messenger server using SignalR. I want to broadcast
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I don't have photoshop, but I want to make pattern files (.pat)

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.