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

  • Home
  • SEARCH
  • 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 6359739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:33:31+00:00 2026-05-24T23:33:31+00:00

I’d like to use my program this way: pythonScript -f filename But if I

  • 0

I’d like to use my program this way:

pythonScript -f filename

But if I forgot -f, I still want to get the filename.

So I want this “pythonScript filename” also work (->I can get the file name from args.fileName or from handling the exception)

I know this must be easy, I just can not find a way to do it. Anyone can help me out? Thanks

import argparse
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('-f', action='store', dest='fileName',
    default = None, type=str, help='The file name')

try:
    args = parser.parse_args()
except:
    print "xxx"
  • 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-24T23:33:32+00:00Added an answer on May 24, 2026 at 11:33 pm

    If you want -f to be required, you can make it that way with argparse. Just include “required = True” inside of the add_argument function.

    Remember that required option is contradictory. It’s not suggested to make options required, but sometimes helpful. If the option is not given, then the destination remains the None value (or it gets assigned whatever default value you’ve given, which is None in your example).

    EDIT: Just saw your edit and I understand what you’re asking now. optparse makes this simpler IMO. It seems for positional arguments with argparse, you need to use add_argument.

    There’s really no easy way to have argparse do it for you. You can simulate it a little bit creatively with the following:

    parser = argparse.ArgumentParser()
    parser.add_argument('file', nargs='?')
    parser.add_argument('-f', dest='file_opt')
    args = parser.parse_args()
    if args.file_opt:
        args.file = args.file_opt
    

    Basically, if there’s a file option given, it overwrites the positional argument.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is

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.