5

I currently have a python file that utilizes sys.argv[1] to accept a string at the command line. It then performs operations on that string and then returns the modified string to the command line.

I would like to implement a batch mode option in which I can provide a file of strings (one per line, fwiw) and have it return to the command line so that I can redirect the output doing something like

$ python script.py -someflag file.txt > modified.txt 

while still retaining the current capabilities.

I am only running 2.6, so argparse is not an option. The tutorials I have seen either use argparse, getopt, or delve into examples that are too complex/don't apply.

What is the best way to check the input and act appropriately?

shakaran
  • 10,612
  • 2
  • 29
  • 46
verbsintransit
  • 888
  • 3
  • 8
  • 18
  • 1
    argparse is still an option, it's just not built into 2.6. You can still install it like any 3rd party package (for example, `pip install argparse`). – David Robinson Aug 31 '12 at 21:01

2 Answers2

6

argparse is still an option, it's just not built into 2.6. You can still install it like any 3rd party package (for example, using easy_install argparse).

An example of code for this would be:

import sys
import argparse

p = argparse.ArgumentParser(description="script.py")
p.add_argument("-s", dest="string")
p.add_argument("-f", dest="infile")

args = p.parse_args()

if args.infile == None and args.string == None:
    print "Must be given either a string or a file"
    sys.exit(1)
if args.infile != None and args.string != None:
    print "Must be given either a string or a file, not both"
    sys.exit(1)
if args.infile:
    # process the input file one string at a time
if args.string:
    # process the single string
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Thank you! I didn't know that argparse was third-party before 2.7, and the code is an added bonus. I will likely accept this as soon as I get a chance to try it out :) – verbsintransit Aug 31 '12 at 21:15
  • You're quite welcome. argparse is a very powerful and intuitive tool and I highly recommend it. – David Robinson Aug 31 '12 at 21:19
  • 1
    When testing against `None`, it is more pythonic to use `is` and `is not` to test for that value: `if args.infile is None and args.string is None:` and `if args.infile is not None and args.string is not None:`. – Martijn Pieters Sep 14 '12 at 06:44
3

See my answer here: What's the best way to grab/parse command line arguments passed to a Python script?

As a shortcut, here's some sample code:

import optparse

parser = optparse.OptionParser()

parser.add_option('-q', '--query',
    action="store", dest="query",
    help="query string", default="spam")

options, args = parser.parse_args()

print 'Query string:', options.query
Community
  • 1
  • 1
Thomas Vander Stichele
  • 36,043
  • 14
  • 56
  • 60
  • **Note**: Using *optparse* is discouraged since python version 2.7. The optparse module is deprecated and will not be developed further; development will continue with the *argparse* module. See [PEP 0389](http://www.python.org/dev/peps/pep-0389/) for more info. – shakaran Apr 03 '13 at 23:30