Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most pythonic way of accepting arguments using optparse

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?

like image 654
verbsintransit Avatar asked Aug 31 '12 20:08

verbsintransit


People also ask

What is the use of Optparse?

optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module. optparse uses a more declarative style of command-line parsing: you create an instance of OptionParser , populate it with options, and parse the command line.

How do you pass arguments to Argparse?

To add your arguments, use parser. add_argument() . Some important parameters to note for this method are name , type , and required . The name is exactly what it sounds like — the name of the command line field.

What is argument parsing?

argparse — parse the arguments. Using argparse is how you let the user of your program provide values for variables at runtime. It's a means of communication between the writer of a program and the user. That user might be your future self.

What command line argument does the ArgumentParser provide by default?

Parsing arguments In a script, parse_args() will typically be called with no arguments, and the ArgumentParser will automatically determine the command-line arguments from sys.argv .


2 Answers

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
like image 178
David Robinson Avatar answered Nov 15 '22 11:11

David Robinson


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
like image 20
Thomas Vander Stichele Avatar answered Nov 15 '22 11:11

Thomas Vander Stichele