optparse

Python, optparse and file mask

if __name__=='__main__': parser = OptionParser() parser.add_option("-i", "--input_file", dest="input_filename", help="Read input from FILE", metavar="FILE") (options, args) = parser.parse_args() print options result is $ python convert.py -i video_* {'input_filename': 'video_...

Python optparse metavar

Hi, I am not sure what optparse's metavar parameter is used for. I see it used all around, but I can't see its use. Can someone make it clear to me? Thanks. ...

Python Optparse list

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 to convert comma seperated values into a lis...

How do I mock the Python method OptionParser.error(), which does a sys.exit()?

I'm trying to unit test some code that looks like this: def main(): parser = optparse.OptionParser(description='This tool is cool', prog='cool-tool') parser.add_option('--foo', action='store', help='The foo option is self-explanatory') options, arguments = parser.parse_args() if not options.foo: parser.error('--f...

How do I format positional argument help using Python's optparse?

As mentioned in the docs the optparse.OptionParser uses an IndentedHelpFormatter to output the formatted option help, for which which I found some API documentation. I want to display a similarly formatted help text for the required, positional arguments in the usage text. Is there an adapter or a simple usage pattern that can be used f...

OptionParser - supporting any option at the end of the command line

Hi guys, I'm writing a small program that's supposed to execute a command on a remote server (let's say a reasonably dumb wrapper around ssh [hostname] [command]). I want to execute it as such: ./floep [command] However, I need to pass certain command lines from time to time: ./floep -v [command] so I decided to use optparse.Option...

Extracting filenames from command line arguments with Ruby

I'm trying to use optparse to parse command line arguments. I would like my program to accept arguments like that: $ ./myscript.rb [options] filename I can easily manage the [options] part: require 'optparse' options = { :verbose => false, :type => :html } opts = OptionParser.new do |opts| opts.on('-v', '--verbose') do option...

optpase returns true class while string is provided in cmd line arguments

I'm stuck in a totally stupid situation. When I use the snippet below, despite my command line being "./the_script.rb -s serv" and I check the value of the service variable within the code, it's always taken to be of boolean class by optparse. So I cannot get my string from the command line... any ideas ? opt = OptionParser.new do |op...

Optparse: Usage on variable arg callback action does not indicate that extra params are needed

I have implemented in my python code a callback for variable arguments similar to what can be found here: hxxp://docs.python.org/library/optparse.html#callback-example-6-variable-arguments Adding the option like this: parser.add_option("-c", "--callback", dest="vararg_attr", action="callback", callback=vararg_callback) The problem ...

With Python's optparse module, how do you create an option that takes a variable number of arguments?

With Perl's Getopt::Long you can easily define command-line options that take a variable number of arguments: foo.pl --files a.txt --verbose foo.pl --files a.txt b.txt c.txt --verbose Is there a way to do this directly with Python's optparse module? As far as I can tell, the nargs option attribute can be used to specify a ...

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

Is there a way to make Python's optparse print the default value of an option or flag when showing the help with --help? ...

How to comply to PEP 257 docstrings when using Python's optparse module?

According to PEP 257 the docstring of command line script should be its usage message. The docstring of a script (a stand-alone program) should be usable as its "usage" message, printed when the script is invoked with incorrect or missing arguments (or perhaps with a "-h" option, for "help"). Such a docstring should docum...

Should I forward arguments as *args & **kwargs?

I have a class that handles command line arguments in my program using python's optparse module. It is also inherited by several classes to create subsets of parameters. To encapsulate the option parsing mechanism I want to reveal only a function add_option to inheriting classes. What this function does is then call optparse.make_option....

Python optparse defaults vs function defaults

I'm writing a python script which I would like to be able to both call from the command line and import as a library function. Ideally the command line options and the function should use the same set of default values. What is the best way to allow me to reuse a single set of defaults in both places? Here's the current code with duplic...

Optparse library - callback action while storing arg

My code: def main(): usage = "usage: %prog [options] arg" parser = OptionParser(usage) parser.add_option("-p", "--pending", action="callback", callback=pending, type="string", dest="test", help="View Pending Jobs") (options, args) = parser.parse_args() if x == 0: print usage, " (-h or --help for help)" print...

How to make a custom command line interface using OptionParser ?

I am using the OptionParser from optparse module to parse my command that I get using the raw_input(). I have these questions. 1.) I use OptionParser to parse this input, say for eg. (getting multiple args) my prompt> -a foo -b bar -c spam eggs I did this with setting the action='store_true' in add_option() for '-c',now if there ...

Python optparse Values Instance

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 i in opt: myDict[i] = opt[i] instead, its a clumsy, myDict[parm1] = opt.parm1 myDic...

Processing (possibly) optional arguments in Python

I am working on a series of command line tools which connect to the same server and do related but different things. I'd like users to be able to have a single configuration file where they can place common arguments such as connection information that can be shared across all the tools. Ideally, I'd like something that does the follow...

python optparse, how to include additional info in usage output?

Using python's optparse module I would like to add extra example lines below the regular usage output. My current help_print() output looks like this: usage: check_dell.py [options] options: -h, --help show this help message and exit -s, --storage checks virtual and physical disks -c, --chassis checks specified chassis components...

Proper help for arguments

Python optparse works very good when script usage is something like this %prog [options] [args] But I need to write help for script with 1 required argument, so usage will be like this %prog action [options] [args] You can see something similar when you use Subversion - its usage string is svn <subcommand> [options] [args] So my...