user308883
user308883

Reputation:

'no such option' using OptionParser

# Edit: I did not choose to start Ipython with the --pylab argument. I have python(x,y) installed so maybe that is the problem?

Edit2: I'm on Windows. I start the IDE from the python(x,y) console. Problem also occurs when I start Visual Studio (i'm using PTVS). Problem doesn't occur when I directly start IDLE and run the example there.

Trying to run the following code from Python docs: https://docs.python.org/2/library/optparse.html

from optparse import OptionParser
[...]
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")

I get the following error:

In [55]: (options, args) = parser.parse_args()
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

Usage: -c [options]

-c: error: no such option: --pylab
To exit: use 'exit', 'quit', or Ctrl-D.

I'm new to python and not really sure whats going on here. Curious why the error is referencing pylab? Seems odd. Was wondering if anybody else had run into a similar problem. Here is the traceback in case it helps:

In [56]: %tb
---------------------------------------------------------------------------
SystemExit                                Traceback (most recent call last)
<ipython-input-55-9900fd0b7216> in <module>()
----> 1 (options, args) = parser.parse_args()

C:\Python27\lib\optparse.pyc in parse_args(self, args, values)
   1399             stop = self._process_args(largs, rargs, values)
   1400         except (BadOptionError, OptionValueError), err:
-> 1401             self.error(str(err))
   1402 
   1403         args = largs + rargs

C:\Python27\lib\optparse.pyc in error(self, msg)
   1581         """
   1582         self.print_usage(sys.stderr)
-> 1583         self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
   1584 
   1585     def get_usage(self):

C:\Python27\lib\optparse.pyc in exit(self, status, msg)
   1571         if msg:
   1572             sys.stderr.write(msg)
-> 1573         sys.exit(status)
   1574 
   1575     def error(self, msg):

SystemExit: 2

Upvotes: 0

Views: 5811

Answers (1)

Jahaja
Jahaja

Reputation: 3302

OptParse will read from sys.argv by default. You've started ipython with the arg --pylab which doesn't exist in your optparse setup, hence the error.

You can pass an empty string to parse_args('') to test the defaults.

Upvotes: 1

Related Questions