77import sys
88import code
99from optparse import OptionParser , OptionGroup
10- from itertools import takewhile
1110
1211from bpython import __version__
1312from bpython .config import loadini , Struct , migrate_rc
1413
14+
15+ class OptionParserFailed (ValueError ):
16+ """Raised by the RaisingOptionParser for a bogus commandline."""
17+
18+
19+ class RaisingOptionParser (OptionParser ):
20+ def error (self , msg ):
21+ raise OptionParserFailed ()
22+
23+
1524def parse (args , extras = None ):
1625 """Receive an argument list - if None, use sys.argv - parse all args and
1726 take appropriate action. Also receive optional extra options: this should
@@ -39,10 +48,16 @@ def parse(args, extras=None):
3948 if args is None :
4049 args = sys .argv [1 :]
4150
42- parser = OptionParser (usage = 'Usage: %prog [options] [file [args]]\n '
43- 'NOTE: If bpython sees an argument it does '
44- 'not know, execution falls back to the '
45- 'regular Python interpreter.' )
51+ parser = RaisingOptionParser (
52+ usage = 'Usage: %prog [options] [file [args]]\n '
53+ 'NOTE: If bpython sees an argument it does '
54+ 'not know, execution falls back to the '
55+ 'regular Python interpreter.' )
56+ # This is not sufficient if bpython gains its own -m support
57+ # (instead of falling back to Python itself for that).
58+ # That's probably fixable though, for example by having that
59+ # option swallow all remaining arguments in a callback.
60+ parser .disable_interspersed_args ()
4661 parser .add_option ('--config' , '-c' , default = '~/.bpython/config' ,
4762 help = 'use CONFIG instead of default config file' )
4863 parser .add_option ('--interactive' , '-i' , action = 'store_true' ,
@@ -59,17 +74,11 @@ def parse(args, extras=None):
5974 extras_group .add_option (option )
6075 parser .add_option_group (extras_group )
6176
62- all_args = set (parser ._short_opt .keys () + parser ._long_opt .keys ())
63- if args and not all_args .intersection (arg .split ('=' )[0 ] for arg in args ):
77+ try :
78+ options , args = parser .parse_args (args )
79+ except OptionParserFailed :
6480 # Just let Python handle this
6581 os .execv (sys .executable , [sys .executable ] + args )
66- else :
67- # Split args in bpython args and args for the executed file
68- real_args = list (takewhile (lambda arg : arg .split ('=' )[0 ] in all_args ,
69- args ))
70- exec_args = args [len (real_args ):]
71-
72- options , args = parser .parse_args (real_args )
7382
7483 if options .version :
7584 print 'bpython version' , __version__ ,
@@ -91,7 +100,7 @@ def parse(args, extras=None):
91100
92101 loadini (config , options .config )
93102
94- return config , options , exec_args
103+ return config , options , args
95104
96105def exec_code (interpreter , args ):
97106 """
0 commit comments