Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
Using optsparser to add functionality
  • chroniccommand
    Posts: 1,389
    Optsparser is a very useful module for Python scripts that adds functionality to command line orientated scripts.

    Optsparser adds command line arguments like such:

    python script.py -1 --arg1 -p 20

    It lets you add more functional args with tacks added before them. A good example of this is netcat:

    nc -l 20

    This uses something like optsparser as you can see it sets the argument of -l to 20 which can be very useful. Here I will show you how to use optsparser.

    First we import and add optsparser to our script.

    import optparse
    parser = optparse.OptionParser()
    Options = optparse.OptionGroup(parser, 'Options')

    That imports optsparse and sets it up so we can easily add command line arguments using parser.add_option

    Now to add two simple command line arguments:

    parser.add_option('-s', '--host',
    action='store', type='string', help=\"Remote host\", metavar=\"HOST\")
    parser.add_option('-u', '--udp',
    action=\"store_true\", help=\"Use UDP\")

    This is a simple excerpt of a script I'm currently working on, NetDog. The first one uses -s or --host. In the script you can use either of those. The action is set to store and the type is a string. The help is for when a user uses -h it displays what this option does We also set a metavar, which is like --host=HOST. The second one uses store_true. The difference is now we don't need a metavar nor do we need a type. We can just have a help.

    Using the options
    Now we can use the command line options with our script. Here is a simple functional script to demonstrate:

    import optparse
    parser = optparse.OptionParser()
    Options = optparse.OptionGroup(parser, 'Options')
    parser.add_option('-1', '--arg1',
    action='store', type='string', help=\"Argument one\")
    parser.add_option('-2', '--arg2',
    action=\"store\", type='string', help=\"Argument two\")
    parser.add_option_group(Options)
    (opts, args) = parser.parse_args()

    if opts.arg1:
    print(\"Argument one was %s\" % opts.arg1)
    if opts.arg2:
    print(\"Argument two was %s\" % opts.arg2)
    else:
    print(\"Need an argument!\")

    This is very simple. First we set two arguments. Then if opts1.arg1 is true, meaning if it's there then print what the argument was. Same with arg2. If nothing is there we print a simple help message. Here is a simple output:

    chronic@vandal:/tmp$ python opts.py -1 iexploit -2 rocks
    Argument one was iexploit
    Argument two was rocks
    chronic@vandal:/tmp$ python opts.py -2 rocks -1 iexploit
    Argument one was iexploit
    Argument two was rocks
    chronic@vandal:/tmp$ python opts.py -h
    Usage: opts.py [options]

    Options:
    -h, --help show this help message and exit
    -1 ARG1, --arg1=ARG1 Argument one
    -2 ARG2, --arg2=ARG2 Argument two

    Options:

    As you can see, the order in which the arguments are given doesn't matter. This is a big advantage with using optsparser.

    Optsparser adds -h by default. If you add -h as an argument, you will get an error because they conflict.

    Optsparser is a powerful module that is VERY useful. I now use it in a lot of my scripts that require command line arguments since it adds flexibility to them. For more reading up on optparser:
    http://docs.python.org/library/optparse.html

    Please note that optparser is deprecated. It will no longer be developed but I prefer optparser over argparse. Just a matter of style I guess.

    --chroniccommand