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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:26:33+00:00 2026-05-17T22:26:33+00:00

Assume I have a program that uses argparse to process command line arguments/options. The

  • 0

Assume I have a program that uses argparse to process command line arguments/options. The following will print the ‘help’ message:

./myprogram -h

or:

./myprogram --help

But, if I run the script without any arguments whatsoever, it doesn’t do anything. What I want it to do is to display the usage message when it is called with no arguments. How is that done?

  • 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-17T22:26:34+00:00Added an answer on May 17, 2026 at 10:26 pm

    This answer comes from Steven Bethard on Google groups. I’m reposting it here to make it easier for people without a Google account to access.

    You can override the default behavior of the error method:

    import argparse
    import sys
    
    class MyParser(argparse.ArgumentParser):
        def error(self, message):
            sys.stderr.write('error: %s\n' % message)
            self.print_help()
            sys.exit(2)
    
    parser = MyParser()
    parser.add_argument('foo', nargs='+')
    args = parser.parse_args()
    

    Note that the above solution will print the help message whenever the error
    method is triggered. For example, test.py --blah will print the help message
    too if --blah isn’t a valid option.

    If you want to print the help message only if no arguments are supplied on the
    command line, then perhaps this is still the easiest way:

    import argparse
    import sys
    
    parser=argparse.ArgumentParser()
    parser.add_argument('foo', nargs='+')
    if len(sys.argv)==1:
        parser.print_help(sys.stderr)
        sys.exit(1)
    args=parser.parse_args()
    

    Note that parser.print_help() prints to stdout by default. As init_js suggests, use parser.print_help(sys.stderr) to print to stderr.

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

Sidebar

Related Questions

I have a console-mode program that uses SQLite3 to maintain a database file. It
Assume that i have written a program in C++ without using RTTI and run-time
Assume we have a trivial Java program that consists of just one class: public
Assume I have an ASP.NET MVC app that's not doing anything too fancy (no
I have a Sql Server that uses Windows Authentication. I want to connect to
I'm writing a program that uses SetWindowRgn to make transparent holes in a window
Assume that you have a running SQL Server Express instance named (local)\SQLEXPRESS. Its database
We have a legacy VB6 application that uses Crystal Reports XI to generate printed
Let's assume I browse a specific web page that uses JavaScript to update its
I have a parent program that calls a worker console app when and update

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.