optparse

Optparse Option Actions

Optparse Option Actions Question: I’m working with optparse, and I’m writing a script with 3 commandline arguments. The first one (-qtype) specifies whether information needs to be called from a local file, or from the web. Then depending on this, the 2nd argument (-qname) is either a string of nucleotides, or a FASTA filename. The …

Total answers: 1

Set a default choice for optionparser when the option is given

Set a default choice for optionparser when the option is given Question: I have a python option parsers that parses an optional –list-something option. I also want the –list-something option to have an optional argument (an option) Using the argument default=”simple” does not work here, otherwise simple will always be the default, not only when …

Total answers: 2

Python argparse ignore unrecognised arguments

Python argparse ignore unrecognised arguments Question: Optparse, the old version just ignores all unrecognised arguments and carries on. In most situations, this isn’t ideal and was changed in argparse. But there are a few situations where you want to ignore any unrecognised arguments and parse the ones you’ve specified. For example: parser = argparse.ArgumentParser() parser.add_argument(‘–foo’, …

Total answers: 3

Why use argparse rather than optparse?

Why use argparse rather than optparse? Question: I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In addition to getopt and optparse we now have argparse. Why has yet another command-line parsing module been created? Why should I use it instead of optparse? Are there new features that I should know …

Total answers: 5

Python optparse Values Instance

Python optparse Values Instance Question: How can I take the opt result of opt, args = parser.parse_args() and place it in a dict? Python calls opt a “Values Instance” and I can’t find any way to turn a Values Instance into a list or dict. One can’t copy items from opt in this way, for …

Total answers: 1

Can Python's optparse display the default value of an option?

Can Python's optparse display the default value of an option? Question: Is there a way to make Python’s optparse print the default value of an option or flag when showing the help with –help? Asked By: pupeno || Source Answers: Try using the %default string placeholder: # This example taken from http://docs.python.org/library/optparse.html#generating-help parser.add_option(“-m”, “–mode”, default=”intermediate”, …

Total answers: 5

Python Optparse list

Python Optparse list Question: I’m using the python optparse module in my program, and I’m having trouble finding an easy way to parse an option that contains a list of values. For example: –groups one,two,three. I’d like to be able to access these values in a list format as options.groups[]. Is there an optparse option …

Total answers: 4