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
7
votes
5 answers

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…
GeeF
  • 707
  • 2
  • 8
  • 16
7
votes
2 answers

optparse - why the last char of the option can be ignored? With `--file` it behaves same as `--fil`

Here is simple example of code: from optparse import OptionParser parser = OptionParser() parser.add_option("-f", "--file", dest="filename") (options, args) = parser.parse_args() print options I have saved it to file and run. It works: $ python…
dimadvk
  • 405
  • 3
  • 6
7
votes
2 answers

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-options] Some options can not be used together, for example todo add --pos=3 --end "Ask Stackoverflow" would specify both the third…
Joel
  • 3,227
  • 5
  • 21
  • 16
7
votes
3 answers

SystemExit: 2 error when calling parse_args() in iPython Notebook

I'm learning to use Python and scikit-learn and executed the following block of codes (originally from http://scikit-learn.org/stable/auto_examples/document_classification_20newsgroups.html#example-document-classification-20newsgroups-py) in iPython…
cecilia
  • 81
  • 1
  • 4
7
votes
2 answers

python optparse, default values for optional options

This is more like a code design question. what are good default values for optional options that are of type string/directory/fullname of files? Let us say I have code like this: import optparse parser =…
Dnaiel
  • 7,622
  • 23
  • 67
  • 126
7
votes
1 answer

Python: switching from optparse to argparse

After switching from optparse to argparse - I'm experiencing strange errors. Argparse parse args only if leave no space: myScript.py -oOpt or put an equal sign: myScript.py -o=Opt and it doesn't work the normal way: myScript.py -o Opt Here's my…
Adobe
  • 12,967
  • 10
  • 85
  • 126
6
votes
3 answers

python - beginner - integrating optparse in a program

I've started a serious attempt to learn some Python as my first programming language with some basic knowledge on algorithms. Since everyone recommends that the best way to start is to find something useful to do, I've decided to do a small script…
nmarques
  • 131
  • 5
6
votes
1 answer

OptionParse in Ruby and params not starting with '-'

I want to have params like this: program dothis --additional --options and: program dothat --with_this_option=value and I can't get how to do that. The only thing I managed to do is to use params with -- at the beginning. Any ideas?
Szymon Lipiński
  • 27,098
  • 17
  • 75
  • 77
6
votes
2 answers

Python optparse, default values, and explicit options

Take the following rather standard code: from optparse import OptionParser opts = OptionParser() opts.add_option('-f', action="store_true") opts.add_option("-x", dest="x", type="int", default=1) options, args =…
9000
  • 39,899
  • 9
  • 66
  • 104
6
votes
3 answers

displaying newlines in the help message when using python's optparse

I'm using the optparse module for option/argument parsing. For backwards compatibility reasons, I can't use the argparse module. How can I format my epilog message so that newlines are preserved? In the below example, I'd like the epilog to be…
DannyTree
  • 1,137
  • 2
  • 12
  • 16
6
votes
2 answers

Ruby OptionParser Short Code for Boolean Option?

I am using Ruby's OptionParser (require 'optparse') processing a "verbose" option that can be either true or false. It is in the code like this: parser.on('-v', '--[no-]verbose', 'Verbose mode') do |v| self.verbose = v end I support…
Keith Bennett
  • 4,722
  • 1
  • 25
  • 35
6
votes
2 answers

Imported python module overrides option parser

I have written a python utility script that uses optparse to include options and flags at script launch. Everything works great, but when I import google API oauth2client and run its execute function, it overrides my add_options into the options it…
ShaharA
  • 860
  • 8
  • 19
6
votes
3 answers

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…
rodo
  • 260
  • 3
  • 8
5
votes
4 answers

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

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…
Evert
  • 93,428
  • 18
  • 118
  • 189
5
votes
1 answer

Optparse callback not consuming argument

I'm trying to get to know optparse a bit better, but I'm struggling to understand why the following code behaves the way it does. Am I doing something stupid? import optparse def store_test(option, opt_str, value, parser, args=None, kwargs=None): …
Acorn
  • 49,061
  • 27
  • 133
  • 172
1 2
3
19 20