[IPython-dev] Minus signs in argparse line magics

Matthew Brett matthew.brett@gmail....
Fri Aug 10 17:00:18 CDT 2012


Hi,

I ran into this when using the R magics, but it's obviously generic to
argparse magics:

In [1]: %load_ext rmagic
In [6]: %R c(1, -1)
UsageError: unrecognized arguments: -1)

The problem is that argparse is looking for '-' options throughout the
input string, not just preceding the main line text.  Thus it's
identifying '-1' as an option and barfing.  Obviously there's a
variety of workarounds:

In [7]: %R c(1,-1) # no space before '-'
Out[7]: array([ 1., -1.])
In [8]: %R -- c(1, -1) # '--' signalling end of options to argparse
Out[8]: array([ 1., -1.])

but the error is rather confusing and it's a little annoying having to
consider argument parsing in your line text.   I couldn't think
immediately of a clean way to fix it that wouldn't involve hacking on
argparse.   Is there a good way?

Thanks a lot,

Matthew


More information about the IPython-dev mailing list