+ Reply to Thread
Results 1 to 5 of 5

Thread: Python Tutorial - Handling Command Line Arguments

  1. #1
    ShadenSmith's Avatar
    ShadenSmith is offline Newbie
    Join Date
    May 2009
    Location
    Kentucky - USA
    Posts
    19
    Rep Power
    10

    Python Tutorial - Handling Command Line Arguments

    Command Line Arguments in Python - A Tutorial

    Hello World, this is my first post! I wrote this because I recently wrote my first python application that requires command line arguments and discovered how easy it is to work with arguments in Python. Command line programs are very handy when your program is designed for one purpose and an entire GUI to select options is unnecessary.

    Before we begin, we should cover the difference between arguments and options. Options are optional (duh). They are what you supply that begin with - or -- in a command line application. Arguments are required information that do not begin with - or --. Options can sometimes require arguments supplied with them, such as an output destination. Arguments are always supplied after all options, but options can be supplied in any order. This tutorial will cover both options and arguments.

    Only one import statement is necessary:

    Code:
    #!/usr/bin/env python
    
    from optparse import OptionParser
    The first thing we would like to do is set a usage string. This will be displayed when the user supplies -h or --help as an option. We then create a parser object with this usage string.

    Code:
    use = "Usage: %prog [options] argument1 argument2"
    
    parser = OptionParser(usage = use)
    %prog will print out the name of the file being run. A usage string is not required and will default to:

    Usage: tut.py [options]
    Our next step is to add options to the parser.

    Code:
    parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Set mode to verbose.")
    parser.add_option("-f", "--filename", dest="write" metavar="FILE", help="write output to FILE"),
    The first two arguments of add_option give the short and longhand calls for the option. 'dest=STRING' stores a variable with the name of the given string. 'metavar' gives the name of the expected argument to be received with the option. 'metavar' should be used when you do not want to store a variable as a boolean. Parser will print an error and exit if the expected argument is not given. Finally, 'help' stores the string that will be printed given the argument --help.

    If the option -h is supplied with our program, the output would be:

    Usage: tut.py [options] argument1 argument2

    Options:
    -h, --help show this help message and exit
    -v, --verbose Set mode to verbose.
    -f FILE, --filename=FILE write output to FILE

    The final step is to parse the options and arguments into variables we can use later.

    Code:
    options, args = parser.parse_args()
    After this statement, 'options' will contain the variables we set to store when we added options to parser. 'args' will contain the arguments listed after all options.

    After all options and arguments have been parsed, it would make sense to be able to access the information. Options are accessed through parser.<variable name> and arguments are accessed through an index in the array, 'args'.

    Code:
    if options.verbose:
    	print "Mode is set to verbose!"
    
    print options.write
    print args[0]
    print args[1]
    Given the input:
    Code:
    ./test.py -v -f my_file ARG1 ARG1
    The output would be:

    Mode is set to verbose!
    my_file
    ARG1
    ARG2

    I hope this helps in writing your command line based programs in Python. I'd be glad to write about more advanced optparse applications if anyone finds this helpful!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Python Tutorial - Handling Command Line Arguments

    Very Nice! Thanks for sharing.

  4. #3
    ShadenSmith's Avatar
    ShadenSmith is offline Newbie
    Join Date
    May 2009
    Location
    Kentucky - USA
    Posts
    19
    Rep Power
    10

    Re: Python Tutorial - Handling Command Line Arguments

    I'm glad you liked the tutorial. Command line arguments can be a very powerful part of a program.

  5. #4
    Jordan Guest

    Re: Python Tutorial - Handling Command Line Arguments

    Very nice tutorial! +rep

  6. #5
    Mufasa is offline Newbie
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Re: Python Tutorial - Handling Command Line Arguments

    You may be interested in a little Python module I wrote to make handling of command line arguments even easier (open source and free to use) - Commando

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. problem with command line arguments
    By csepraveenkumar in forum Java Help
    Replies: 1
    Last Post: 05-17-2011, 09:43 AM
  2. Command line arguments in DOS
    By Actor in forum Computer Software/OS
    Replies: 0
    Last Post: 05-18-2010, 12:57 AM
  3. Command Line Arguments
    By whitey6993 in forum C Tutorials
    Replies: 0
    Last Post: 04-04-2010, 12:57 PM
  4. Reading Command Line Arguments
    By Guest in forum C Tutorials
    Replies: 18
    Last Post: 02-01-2010, 02:34 PM
  5. Reading Command line arguments in VB.NET
    By Vswe in forum Visual Basic Tutorials
    Replies: 2
    Last Post: 06-06-2009, 06:46 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts