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
16
votes
6 answers

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…
pufferfish
  • 16,651
  • 15
  • 56
  • 65
15
votes
4 answers

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') …
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
15
votes
3 answers

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''' / \ …
tutuca
  • 3,444
  • 6
  • 32
  • 54
14
votes
2 answers

Consistent way to redirect both stdin & stdout to files in python using optparse

I've got a dozen programs that can accept input via stdin or an option, and I'd like to implement the same features in a similar way for the output. The optparse code looks like this: parser.add_option('-f', '--file', default='-', …
KenFar
  • 1,531
  • 1
  • 14
  • 25
13
votes
3 answers

Python argparse argument with quotes

Is there any way I can tell argparse to not eat quotation marks? For example, When I give an argument with quotes, argparse only takes what's inside of the quotes as the argument. I want to capture the quotation marks as well (without having to…
denvaar
  • 2,174
  • 2
  • 22
  • 26
12
votes
3 answers

understanding OptionParser

I was trying out optparse and this is my initial script. #!/usr/bin/env python import os, sys from optparse import OptionParser parser = OptionParser() usage = "usage: %prog [options] arg1 arg2" parser.add_option("-d", "--dir", type="string", …
MacUsers
  • 2,091
  • 3
  • 35
  • 56
12
votes
5 answers

Subcommand alternative to argparse and optparse

Is there any intuitive alternative for argparse/optparse for subcommands? They both are bad - it is either insane config or insane output. Real world example (stolen, not wanted): >>> parser = argparse.ArgumentParser() >>> subparsers =…
anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
12
votes
6 answers

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…
FMc
  • 41,963
  • 13
  • 79
  • 132
10
votes
3 answers

Is it possible to make an option in optparse a mandatory?

Is it possible to make an option in optparse a mandatory?
Alex
  • 165
  • 1
  • 3
  • 6
9
votes
5 answers

Python Command Line Arguments: Calling a function

So I'm stuck on a project I'm working on that involves the command line in python. So basically, here's what I'm trying to accomplish: I have a set of functions in a class, say, def do_option1(self, param1, param2) : #some python code…
lesley2958
  • 2,538
  • 4
  • 15
  • 15
8
votes
1 answer

Negative boolean options --no-whatever in optparse?

With optparse, is there a simple way to define negative options, e.g., --no-cleanup? I did it this way, but it's cumbersome and bug-prone, especially due to the None check which is easy to forget and leave out: #!/bin/env python from __future__…
Frank
  • 64,140
  • 93
  • 237
  • 324
8
votes
1 answer

R optparse error with command line arguments

For some reason, the optparse usage in this script breaks: test.R: #!/usr/bin/env Rscript library("optparse") option_list <- list( make_option(c("-n", "--name"), type="character", default=FALSE, dest="report_name", help="A…
user5359531
  • 3,217
  • 6
  • 30
  • 55
8
votes
3 answers

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

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…
user265454
  • 3,071
  • 3
  • 21
  • 12
8
votes
1 answer

Ruby optparse Limitations

I currently script in Python but I wish to try Ruby for several reasons. I've looked at a lot of sample code and read a lot of documentation over the last week. One point of concern I have is the lack of a proper command line argument parsing…
p0lAris
  • 4,750
  • 8
  • 45
  • 80
7
votes
5 answers

Getting command line arguments as tuples in python

Here is an example of how I would like to call my script: python script.py -f file1.txt "string1" "string2" -f file2.txt "string3" "string4" Every file that goes as input will have 2 strings associated with that file. There can be any number of…
bits
  • 8,110
  • 8
  • 46
  • 55
1
2
3
19 20