If you know how many words after the flag you are going to get, you can change the way you create the --executable option in optparse to handle the situation correctly:
Instead of the single word after the option flag, you can set the optparse parser to search for two (or more) words:
from optparse import OptionParser parser = OptionParser() parser.add_option("-f", "--file", action="store", dest="filename", help="File to be processed.", metavar="FILE") parser.add_option("-e", "--executable", action="store", dest="my_exe", help="Command to be executed", metavar="EXE", nargs=2)
In this snippet, the -f or --file option expects only one word and saves it as a string (by default) in the filename variable.
Unlike the -e option, --executable two words are expected due to the nargs=2 option. This will cause the two words found behind the -e or --executable flag to be stored as strings in the my_exe Python my_exe .
Check out http://docs.python.org/library/optparse.html for more information on optparse and remember that it was deprecated since 2.7 in favor of argparse .
dtlussier
source share