Questions tagged [optparse]

optparse is a command-line argument parser for Python included in the standard library, deprecated since 2.7. It is also an unrelated command-line argument parser for Ruby.

The Python optparse module is deprecated since 2.7 and will not be developed further; development will continue with the argparse module. See PEP 0389 for more info.

Using optparse with Ruby is encouraged.

289 questions
328
votes
5 answers

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…
fmark
  • 57,259
  • 27
  • 100
  • 107
287
votes
3 answers

Python argparse ignore unrecognised arguments

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…
joedborg
  • 17,651
  • 32
  • 84
  • 118
53
votes
1 answer

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…
mgag
  • 919
  • 2
  • 8
  • 13
43
votes
5 answers

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?
Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
40
votes
4 answers

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…
Daniel Delaney
  • 1,183
  • 3
  • 16
  • 21
40
votes
5 answers

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

I need to parse a command line like script.rb [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…
Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
30
votes
4 answers

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),…
rampion
  • 87,131
  • 49
  • 199
  • 315
27
votes
4 answers

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…
cdleary
  • 69,512
  • 53
  • 163
  • 191
27
votes
5 answers

Python optparse metavar

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

Disable unique prefix matches for argparse and optparse

When I use Python's argparse or optparse command line argument parser, any unique prefix of an argument is considered valid, e.g. $ ./buildall.py --help usage: buildall.py [-h] [-f] Build all repositories optional arguments: -h, --help show…
Simon Warta
  • 10,850
  • 5
  • 40
  • 78
26
votes
6 answers

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,…
CarpeNoctem
  • 5,540
  • 8
  • 30
  • 33
25
votes
5 answers

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

In python's OptionParser, how can I instruct it to ignore undefined options 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' ] I don't care if it…
Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
20
votes
1 answer

OptionParser returning bool instead of argument?

When I run this sample from the OptionParser documentation: require 'optparse' options = {} OptionParser.new do |opts| opts.banner = "Usage: example.rb [options]" opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| …
CrazyLion
  • 225
  • 2
  • 5
18
votes
10 answers

Can OptionParser skip unknown options, to be processed later in a Ruby program?

Is there any way to kick off OptionParser several times in one Ruby program, each with different sets of options? For example: $ myscript.rb --subsys1opt a --subsys2opt b Here, myscript.rb would use subsys1 and subsys2, delegating their options…
inger
  • 19,574
  • 9
  • 49
  • 54
16
votes
3 answers

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 main() File "./thumbnail.py", line 19, in main options['input_pattern'] AttributeError: Values…
Viet
  • 17,944
  • 33
  • 103
  • 135
1
2 3
19 20