1

So what i am trying to do is simple is launch a simple help dialog when i do -h.. That works fine, the issue comes when i try to do anything besides -h. what is the best practice of taking care of this. There will be many more paramters being passed just starting out with -h for now. here is my main.

if __name__ == '__main__':
#if no args are specified in command line
if len(sys.argv)<2:
    print "Not enough arguments dude/dudett use -h for help"
else:
    #get args
    try:
        parser = OptionParser()
        parser.add_option("-h", "--help",
            action="store_true", dest="hlpBool")
        (opts, args) = parser.parse_args()
        if(options.hlpBool==true):
            print 'Help Statement'
            #sys.exit(2)
    except AttributeError as e:
        print (dir(e))
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
user3753693
  • 225
  • 1
  • 5
  • 13

2 Answers2

1

OptionParser is now depreciated, you should consider using the ArgumentParser:

from argparse import ArgumentParser

parser = ArgumentParser(description='Your argument parser')
parser.add_argument('--a', dest='a', help='a option')
parser.add_argument('--b', dest='b', help='b option')
parsed_args = parser.parse_args()

print parsed_args.a
print parsed_args.b

The -h functionality is builtin to the ArgumentParser, so you don't need to handle this option. It will display the usage if the user enters -h

Example script -h execution:

[root@srvr1 tmp]# python argparsing.py -h
usage: argparsing.py [-h] [--a A] [--b B]

Your argument parser

optional arguments:
  -h, --help  show this help message and exit
  --a A       a option
  --b B       b option

Example script execution:

[root@srvr1 tmp]# python argparsing.py --a SOME_VALUE_A --b SOME_VALUE_B
SOME_VALUE_A
SOME_VALUE_B
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
  • I am being forced to do this with python 2.4 is it available in 2.4? also what about if i want another value such as recursive? – user3753693 Jun 04 '16 at 16:17
  • Can you install the argparse module ? Are you using Linux? "yum install python-argparse" ? What do you mean another value such as recursive? – Alan Kavanagh Jun 04 '16 at 16:18
  • Definitely try install argparse. It makes parsing parameters alot easier and comes with alot of builtin functionality to clean up alot of the work you'll need to implement – Alan Kavanagh Jun 04 '16 at 16:21
  • Ok but is there a way to say if-h not in arg then set arg as null or somthing so it doesn't throw a excepton. or if i can tell what threw the exception mark it as null then continue? – user3753693 Jun 04 '16 at 16:26
  • If -h is not provided, it will default to None – Alan Kavanagh Jun 04 '16 at 16:28
  • 1
    -h is built into optparse also – user3753693 Jun 04 '16 at 16:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113819/discussion-between-alan-and-user3753693). – Alan Kavanagh Jun 04 '16 at 17:35
0

-h is built into optparse also so when i was doing calling options.hlpBool that is unnecessary

user3753693
  • 225
  • 1
  • 5
  • 13