Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ambiguous option error with optparse

Please tell why the below code is giving the error though both indicates the same option.

In [3]: parser = optparse.OptionParser()

In [4]: parser.add_option("--currencies", "--currency", "--ccy")
Out[4]: <Option at 0x7f113e6c4488: --currencies/--currency/--ccy>

In [5]: parser.parse_args(["--curr", "CHF"])
Usage: ipython [options]

ipython: error: ambiguous option: --curr (--currencies, --currency?)
An exception has occurred, use %tb to see the full traceback.
like image 617
Vinu Natrajan Avatar asked Oct 15 '25 13:10

Vinu Natrajan


2 Answers

There is no real docs about this, but you can check the source code and see what happen.

In brief, optparse will check if given arguments are not too close from each other.

If two arguments (or more) starts by the same string (like foo, foobar and foofoo which all starts by foo) it will raise an AmbiguousOptionError.

In your case, --currencies and --currency starts by --curr, so when you ask to parse --curr optparse cannot say if you want to say --currency or --currencies.

like image 115
Arount Avatar answered Oct 18 '25 11:10

Arount


If you use "argparse" instead of "optparse", then you can add "allow_abbrev=False" attribute to ignore this error.

like image 24
Amir Avatar answered Oct 18 '25 09:10

Amir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!