optparse

Using a file to store optparse arguments

I've been using optparse for a while now, and would like to add the ability to load the arguments from a config file. So far the best I can think of is a wrapper batch script with the arguments hardcoded... seems clunky. What is the most elegant way to do this? ...

How can I get optparse's OptionParser to ignore invalid arguments?

In python's OptionParser, how can I instruct it to ignore undefined flag arguments supplied to method parse_args? e.g. I've only defined option --foo for my OptionParser instance, but I call parse_args with list [ '--foo', '--bar' ] EDIT: I don't care if it filters them out of the original list. I just want undefined options ignored...

Python optparse not seeing argument.

I am trying to pass '-f nameoffile' to the program when I call it from the command line. I got this from the python sites documentation but when I pass '-f filename' or '--file=filename' it throws the error that I didnt pass enough arguments. If i pass -h the programs responds how it should and gives me the help. Any ideas? I imagine ...

Why am I getting no attribute '__getitem__' error for dictionary?

Why am I getting no attribute __getitem__ error for dictionary: Traceback (most recent call last): File "./thumbnail.py", line 39, in <module> main() File "./thumbnail.py", line 19, in main options['input_pattern'] AttributeError: Values instance has no attribute '__getitem__' Here's the code: #!/usr/bin/env python impor...

How to parse an argument without a name with Ruby's optparse

I need to parse a command line like script.rb <mandatory filename> [options] with optparse. Sure I can write some custom code to handle the filename, then pass ARGV to optparse, but maybe there's a simpler way to do it? EDIT: there's another hacky way to parse such a command line, and that is pass ['--mandatory-filename'] + ARGV t...

How to know if optparse option was passed in the command line or as a default

Hi Using python optparse.py, is there a way to work out whether a specific option value was set from the command line or from the default value. Ideally I would like to have a dict just like defaults, but containing the options actually supplied from command line I know that you could compare the value for each option with defaults...

pb with callback in the python optparse module

Hi Guys, I'm playing with Python 2.6 and its optparse module. I would like to convert one of my arguments to a datetime through a callback but it fails. Here is the code: def parsedate(option, opt_str, value, parser): option.date = datetime.strptime(value, "%Y/%m/%d") def parse_options(args): parser = OptionParser(usage="%p...

Python optparse not working for me

I'm currently learning on how to use the Python optparse module. I'm trying the following example script but the args variable comes out empty. I tried this using Python 2.5 and 2.6 but to no avail. import optparse def main(): p = optparse.OptionParser() p.add_option('--person', '-p', action='store', dest='person', default='Me') ...

How do you handle options that can't be used together (using OptionParser)?

My Python script (for todo lists) is started from the command line like this: todo [options] <command> [command-options] Some options can not be used together, for example todo add --pos=3 --end "Ask Stackoverflow" would specify both the third position and the end of the list. Likewise todo list --brief --informative would con...

Using ruby's OptionParser to parse sub-commands

I'd like to be able to use ruby's OptionParser to parse sub-commands of the form COMMAND [GLOBAL FLAGS] [SUB-COMMAND [SUB-COMMAND FLAGS]] like: git branch -a gem list foo I know I could switch to a different option parser library (like Trollop), but I'm interested in learning how to do this from within OptionParser, since I'd like ...

[Python] name 'OptionGroup' is not defined

This error is done strictly by following examples found on the docs. And you can't find any clarification about it anywhere, be it that long long docs page, google or stackoverflow. Plus, reading optparse.py shows OptionGroup is there, so that adds to the confusion. Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) >>> from optparse imp...

optparse: No option string.

Hi. I am trying to use optparse but I am having a problem. My script usage would be: script <filename> I don't intend to add any option string, such as: script -f <filename> or script --file <filename> Is there any way I can choose not to pass an argument string? Or is there any way I can allow the user to do this: script -f <filena...

Parsing arbitrary number of arguments in Python OptParser

How can I define an option with an arbitrary number of arguments in Python's OptParser? I'd like something like: python my_program.py --my-option X,Y # one argument passed, "X,Y" python my_prgoram.py --my-option X,Y Z,W # two arguments passed, "X,Y" and "Z,W" the nargs= option of OptParser limits me to a defined number. How can ...

Why use argparse rather than optparse?

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 their new features I should know about? ...

ASCII art in the optparse description

I'm making a shell script with the optparse module, jut for fun, so I wanted to print a nice ascii drawing in place of the description. Turns out that this code: parser = optparse.OptionParser( prog='./spill.py', description=u''' / \ vvvvvvv /|__/| ...

Can optparse skip unknown options, to be processed later in a ruby program?

Is there any way to kick off optparse several times in one Ruby program, each with different sets of options? Example: $ myscript.rb --subsys1opt a --subsys2opt b here, myscript.rb would use subsys1 and subsys2, delegating their options handling logic to them, possibly in a sequence where 'a' is processed first, followed by 'b' in s...

OptParse: A way to handle directories or files?

I find my self doing this alot: optparse = OptionParser.new do |opts| options[:directory] = "/tmp/" opts.on('-d','--dir DIR', String, 'Directory to put the output in.') do |x| raise "No such directory" unless File.directory?(x) options[:directory] = x end end It would be nicer if I could specify Dir or Pathname instead o...

Python optparse and spaces in an argument

When using optparse i want to get the whole string after an option, but I only get part of it up to the first space. e.g.: python myprog.py --executable python someOtherProg.py What I get in 'executable' is just 'python'. Is it possible to parse such lines using optparse or do you have to use argparse to do it? €: I have already t...

parsing commandline arguments as wildcards

Hi everyone, I wrote a little ruby script using optparse. I'd like to pass a list of files to my script using optparse. I would like to add a couple of files using wildcards (e.g. /dir/*). (Background: simple script, that writes all given arguments to a single text file, separated by newline.) I tried this: opts = OptionParser.new o...