Handling optional arguments like -h

How can I tell argparseconsidered optional arguments are the same as -h, --help?

test.py

def test():
    print '## This is my test'

import argparse
parser = argparse.ArgumentParser(prog='test.py')
parser.add_argument('-t', '--test', action='store_true',
                    help='Should work just like "-h", "--help"')
parser.add_argument('req',
                    help='Otherwise required')

args = parser.parse_args()

if args.test:
    test()

if args.req:
    print '## Found "req"'

# python test.py -h

usage: test.py [-h] [-t] req

positional arguments:
  req         Otherwise required

optional arguments:
  -h, --help  show this help message and exit
  -t, --test  Should work just like "-h", "--help"

# python test.py -t

usage: test.py [-h] [-t] req
test.py: error: too few arguments

# python test.py -t test

## This is my test
## Found "req"

# test python test.py

## Found "req"

I want to see if -t, --testgiven, argparseit stops here, as is the case with -h, --help.

+4
source share
3 answers

The output of the funcitonality function is determined in the method argparse._HelpAction __call__.

class _HelpAction(Action):
    def __init__(self,
                 option_strings,
                 dest=SUPPRESS,
                 default=SUPPRESS,
                 help=None):
        super(_HelpAction, self).__init__(
            option_strings=option_strings,
            dest=dest,
            default=default,
            nargs=0,
            help=help)

    def __call__(self, parser, namespace, values, option_string=None):
        parser.print_help()
        parser.exit()  # it exists here

You need to either use the action help:

parser.add_argument('-t', '--test', action='help',
                    help='Should work just like "-h", "--help"')

Or create your own argparse action.

class MyCustomAction(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        print("argument found, I should exit")
        self.exit()

parser.add_argument('-t', '--test', action= MyCustomAction,
                    help='Should work just like "-h", "--help"')

docs.

+2

.

parser.add_argument('req', nargs='?', default="truc", help='Otherwise required')

Python . , nargs='?' . .

#python test.py -t

## This is my test
## Found "req"

Z.

+1

, nargs:

parser.add_argument('req', nargs='?',
                    help='Otherwise required')

? 0 .

:

import sys

if args.test:
    test()
    sys.exit()

:

import sys

def test():
    print '## This is my test'

import argparse
parser = argparse.ArgumentParser(prog='test.py')
parser.add_argument('-t', '--test', action='store_true',
                    help='Should work just like "-h", "--help"')
parser.add_argument('req', nargs='?',
                    help='Otherwise required')

args = parser.parse_args()

if args.test:
    test()
    sys.exit()

if args.req:
    print '## Found "req"'

:

[8] oz123@laptop-oz:~ $ python bla.py -t
## This is my test
[9] oz123@laptop-oz:~ $ python bla.py -h
usage: test.py [-h] [-t] [req]

positional arguments:
  req         Otherwise required

optional arguments:
  -h, --help  show this help message and exit
  -t, --test  Should work just like "-h", "--help"
[10] oz123@laptop-oz:~ $ python bla.py bla
## Found "req"

sys.exit

sys.exit(), .

[12] oz123@laptop-oz:~ $ python bla.py -t bla
## This is my test
## Found "req"
[13] oz123@laptop-oz:~ $ python bla.py -t 
## This is my test
0

All Articles