Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make argparse print usage when no option is given to the code [duplicate]

With the following code:

import argparse
parser = argparse.ArgumentParser(description="Prepare something code.")
parser.add_argument("-t","--tabular", help="print something in tabular way for EXCEL",
                      action="store_true")
parser.add_argument("-v","--verbose", action="store_true")
args = parser.parse_args()
if args.tabular:
    print "Tabular print"
elif args.verbose:
    print "VERBOSE"

It's only when I execute it the following way, that it prints usage:

$ python mycode.py -h
usage: mycode.py [-h] [-t] [-v]

Prepare something code.

optional arguments:
  -h, --help     show this help message and exit
  -t, --tabular  print something in tabular way for EXCEL
  -v, --verbose

What I want to do is to simply run the code: $ my code.py without any -h option whatsoever to print the usage. How can I do that?

like image 756
neversaint Avatar asked Mar 13 '14 03:03

neversaint


People also ask

How do you add an optional argument in Argparse?

To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.

What is Argparse ArgumentParser ()?

The argparse module provides a convenient interface to handle command-line arguments. It displays the generic usage of the program, help, and errors. The parse_args() function of the ArgumentParser class parses arguments and adds value as an attribute dest of the object.

What does parse_args return?

Adding arguments Later, calling parse_args() will return an object with two attributes, integers and accumulate . The integers attribute will be a list of one or more ints, and the accumulate attribute will be either the sum() function, if --sum was specified at the command line, or the max() function if it was not.

What is Metavar in Argparse Python?

Metavar: It provides a different name for optional argument in help messages.


2 Answers

That 2010 question covers the same thing, but only has 1 answer. While that answer comes indirectly from the designer of argparse, it does not cover all possibilities.

Here's one which surprised me as to its simplicity:

import sys
parser = ...
if len(sys.argv)==1:
    parser.print_help()
    # parser.print_usage() # for just the usage line
    parser.exit()
args = parser.parse_args()

Yes you can check all the namespace args for default values, but that gets old if there are many arguments. But here I am just checking whether there are any argument strings. If none, then call the parser's own help function.

ipython does something like this to generate help. It checks sys.argv for some version of help, and produces its own help message(s), before even defining the parser.

like image 198
hpaulj Avatar answered Nov 07 '22 04:11

hpaulj


import argparse
parser = argparse.ArgumentParser(description="Prepare something code.")
parser.add_argument("-t","--tabular", help="print something in tabular way for EXCEL",
                      action="store_true")
parser.add_argument("-v","--verbose", action="store_true")
args = parser.parse_args()
if args.tabular:
    print "Tabular print"
elif args.verbose:
    print "VERBOSE"
else:
    print parser.print_help()
like image 28
hd1 Avatar answered Nov 07 '22 03:11

hd1