28

Currently when I enter invalid options or omit positional arguments, argparse kicks me back to the prompt and displays the usage for my app.

This is ok, but I would rather automatically display the full help listing (that explains the options, etc.) than require the user to type

./myscript.py -h
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
jpswain.w
  • 283
  • 1
  • 3
  • 4
  • 1
    see also this SO question for more info: [http://stackoverflow.com/questions/4042452][1] [1]: http://stackoverflow.com/questions/4042452 – jpoppe Feb 21 '12 at 09:19

5 Answers5

34

To print help you might want to use: print_help function on ArgumentParser instance

parser = argparse.ArgumentParser()
(...)
parser.print_help()

To print help message on error you need to create own subclass of ArgumentParser instance, that overrides error() method; for example:

class MyParser(argparse.ArgumentParser): 
   def error(self, message):
      sys.stderr.write('error: %s\n' % message)
      self.print_help()
      sys.exit(2)

and then use it:

parser = MyParser()

When this parser encounters unparseable argument line it will print help.

Adam Smooch
  • 1,167
  • 1
  • 12
  • 27
jb.
  • 23,300
  • 18
  • 98
  • 136
17

This thread over at Google groups has the following code snippet which seems to do the trick (modified slightly).

class DefaultHelpParser(argparse.ArgumentParser):
    def error(self, message):
        sys.stderr.write('error: %s\n' % message)
        self.print_help()
        sys.exit(2)
Mike T
  • 41,085
  • 18
  • 152
  • 203
4

I just fixed this same problem myself using the following syntax:

parser = ArgumentParser()
... add arguments ...
parser.usage = parser.format_help()
args = parser.parse_args()
infinitesteps
  • 411
  • 3
  • 5
2

Suppress printing of usage with usage=argparse.SUPPRESS. Then catch the SystemExit exception that ArgumentParser raises on error, print the help, and exit by raising the exception again.

parser = argparse.ArgumentParser(usage=argparse.SUPPRESS)
parser.add_argument(...)
try:
    args = parser.parse_args()
except SystemExit:
    parser.print_help()
    raise
Roger Dahl
  • 15,132
  • 8
  • 62
  • 82
  • @GerhardBarnard Read :) – Roger Dahl Nov 27 '19 at 15:27
  • Now when you invoke the program with `-h`, it prints the help twice. – nisetama Aug 04 '22 at 18:51
  • 1
    I wanted to print both the usage and the help message, so I removed the `usage=argparse.SUPPRESS` option when I created the parser object, but I added `arp.usage=argparse.SUPPRESS` on the first line after `except SystemExit:` so the usage isn't printed twice. – nisetama Aug 04 '22 at 18:52
0

You, also, can print help without using a class or exception:

def _error(parser):
    def wrapper(interceptor):
        parser.print_help()

        sys.exit(-1)

    return wrapper

def _args_get(args=sys.argv[1:]):
    parser = argparser.ArgumentParser()

    parser.error = _error(parser)

    parser.add_argument(...)
    ...

. Just wrap ArgumentParser.error function in your and intercept message argument. I answered, there, earlier:

https://stackoverflow.com/a/60714163/10152015

Kassi
  • 151
  • 2
  • 12