12
12
from bpython import __version__
13
13
from bpython .config import loadini , Struct , migrate_rc
14
14
15
+
16
+ class OptionParserFailed (ValueError ):
17
+ """Raised by the RaisingOptionParser for a bogus commandline."""
18
+
19
+
20
+ class RaisingOptionParser (OptionParser ):
21
+ def error (self , msg ):
22
+ raise OptionParserFailed ()
23
+
24
+
15
25
def parse (args , extras = None ):
16
26
"""Receive an argument list - if None, use sys.argv - parse all args and
17
27
take appropriate action. Also receive optional extra options: this should
@@ -39,10 +49,16 @@ def parse(args, extras=None):
39
49
if args is None :
40
50
args = sys .argv [1 :]
41
51
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.' )
52
+ parser = RaisingOptionParser (
53
+ usage = 'Usage: %prog [options] [file [args]]\n '
54
+ 'NOTE: If bpython sees an argument it does '
55
+ 'not know, execution falls back to the '
56
+ 'regular Python interpreter.' )
57
+ # This is not sufficient if bpython gains its own -m support
58
+ # (instead of falling back to Python itself for that).
59
+ # That's probably fixable though, for example by having that
60
+ # option swallow all remaining arguments in a callback.
61
+ parser .disable_interspersed_args ()
46
62
parser .add_option ('--config' , '-c' , default = '~/.bpython/config' ,
47
63
help = 'use CONFIG instead of default config file' )
48
64
parser .add_option ('--interactive' , '-i' , action = 'store_true' ,
@@ -59,17 +75,11 @@ def parse(args, extras=None):
59
75
extras_group .add_option (option )
60
76
parser .add_option_group (extras_group )
61
77
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 ):
78
+ try :
79
+ options , args = parser .parse_args (args )
80
+ except OptionParserFailed :
64
81
# Just let Python handle this
65
82
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 )
73
83
74
84
if options .version :
75
85
print 'bpython version' , __version__ ,
@@ -91,7 +101,7 @@ def parse(args, extras=None):
91
101
92
102
loadini (config , options .config )
93
103
94
- return config , options , exec_args
104
+ return config , options , args
95
105
96
106
def exec_code (interpreter , args ):
97
107
"""
0 commit comments