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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:58:55+00:00 2026-06-06T19:58:55+00:00

I would like to have the following syntax: python utility.py file1 FILE1 file2 FILE2

  • 0

I would like to have the following syntax:

python utility.py file1 FILE1 file2 FILE2

where file1 and file2 are optional arguments.
It is simple to make it working with this syntax:

python utility.py --file1 FILE1 --file2 FILE2

using

parser.add_argument('--file1',type=file)
parser.add_argument('--file2',type=file)

however, if I remove the dashes, argparse starts to interprete it as a positional rather than optional argument…

In other words, is it possible to specifically tell argparse whether an argument is optional or positional so that I can have optional parameters without the dashes?

  • 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-06T19:58:58+00:00Added an answer on June 6, 2026 at 7:58 pm

    There is no way to get argparse to do this for you. However, you can make argparse accept any number of positional arguments:

    parser.add_argument('FILES',nargs='*')
    options=parser.parse_args()
    file1,optional_files=options.FILES[0],options.FILES[1:]
    

    Of course, you may want to add some checks to make sure that at least 1 file was given, etc.

    EDIT

    I’m still not 100% sure what you want here, but if file1 and file2 are literal strings, you can work around that a little bit by preprocessing sys.argv. Of course, this will still format your help message strangely, but you can always add an epilog explaining that either form is OK:

    import argparse
    import sys
    
    mangle_args=('file1','file2')
    arguments=['--'+arg if arg in mangle_args else arg for arg in sys.argv[1:]]
    
    parser=argparse.ArgumentParser()
    parser.add_argument('--file1')
    parser.add_argument('--file2')
    options=parser.parse_args(arguments)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following shortcode. What I would like to do is turn this
I have the following 2 arrays and would like to combine them. I'm more
I have the following issue and I would like to know what exactly happens.
I have the following LINQ that I would like to also order by file
I have the following table and would like to replace the string text of
Assume I have the following input in Pig: some And I would like to
I have the following C# method: private string[] test() { } I would like
I have the following construction: for (String playerName: players) . I would like to
I have the following HTML: <div id=mydiv> </div> I would like to load content
I have the following String First Last <first.last@email.com> I would like to extract first.last

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.