2929import importlib .util
3030import os
3131import sys
32- from optparse import OptionParser , OptionGroup
32+ import argparse
3333
3434from . import __version__ , __copyright__
3535from .config import default_config_path , loadini , Struct
3636from .translations import _
3737
3838
39- class OptionParserFailed (ValueError ):
39+ class ArgumentParserFailed (ValueError ):
4040 """Raised by the RaisingOptionParser for a bogus commandline."""
4141
4242
43- class RaisingOptionParser ( OptionParser ):
43+ class RaisingArgumentParser ( argparse . ArgumentParser ):
4444 def error (self , msg ):
45- raise OptionParserFailed ()
45+ raise ArgumentParserFailed ()
4646
4747
4848def version_banner (base = "bpython" ):
@@ -60,17 +60,17 @@ def copyright_banner():
6060
6161def parse (args , extras = None , ignore_stdin = False ):
6262 """Receive an argument list - if None, use sys.argv - parse all args and
63- take appropriate action. Also receive optional extra options : this should
64- be a tuple of (title, description, options )
65- title: The title for the option group
66- description: A full description of the option group
67- callback: A callback that adds options to the option group
63+ take appropriate action. Also receive optional extra argument : this should
64+ be a tuple of (title, description, callback )
65+ title: The title for the argument group
66+ description: A full description of the argument group
67+ callback: A callback that adds argument to the argument group
6868
6969 e.g.:
7070
7171 def callback(group):
72- group.add_option ('-f', action='store_true', dest='f', help='Explode')
73- group.add_option ('-l', action='store_true', dest='l', help='Love')
72+ group.add_argument ('-f', action='store_true', dest='f', help='Explode')
73+ group.add_argument ('-l', action='store_true', dest='l', help='Love')
7474
7575 parse(
7676 ['-i', '-m', 'foo.py'],
@@ -82,56 +82,53 @@ def callback(group):
8282 Return a tuple of (config, options, exec_args) wherein "config" is the
8383 config object either parsed from a default/specified config file or default
8484 config options, "options" is the parsed options from
85- OptionParser .parse_args, and "exec_args" are the args (if any) to be parsed
85+ ArgumentParser .parse_args, and "exec_args" are the args (if any) to be parsed
8686 to the executed file (if any).
8787 """
8888 if args is None :
8989 args = sys .argv [1 :]
9090
91- parser = RaisingOptionParser (
91+ parser = RaisingArgumentParser (
9292 usage = _ (
93- "Usage: %prog [options] [file [args]]\n "
93+ "Usage: %( prog)s [options] [file [args]]\n "
9494 "NOTE: If bpython sees an argument it does "
9595 "not know, execution falls back to the "
9696 "regular Python interpreter."
9797 )
9898 )
99- # This is not sufficient if bpython gains its own -m support
100- # (instead of falling back to Python itself for that).
101- # That's probably fixable though, for example by having that
102- # option swallow all remaining arguments in a callback.
103- parser .disable_interspersed_args ()
104- parser .add_option (
99+ parser .add_argument (
105100 "--config" ,
106101 default = default_config_path (),
107102 help = _ ("Use CONFIG instead of default config file." ),
108103 )
109- parser .add_option (
104+ parser .add_argument (
110105 "--interactive" ,
111106 "-i" ,
112107 action = "store_true" ,
113108 help = _ ("Drop to bpython shell after running file instead of exiting." ),
114109 )
115- parser .add_option (
110+ parser .add_argument (
116111 "--quiet" ,
117112 "-q" ,
118113 action = "store_true" ,
119114 help = _ ("Don't flush the output to stdout." ),
120115 )
121- parser .add_option (
116+ parser .add_argument (
122117 "--version" ,
123118 "-V" ,
124119 action = "store_true" ,
125120 help = _ ("Print version and exit." ),
126121 )
127122
128123 if extras is not None :
129- extras_group = OptionGroup ( parser , extras [0 ], extras [1 ])
124+ extras_group = parser . add_argument_group ( extras [0 ], extras [1 ])
130125 extras [2 ](extras_group )
131- parser .add_option_group (extras_group )
126+
127+ # collect all the remaining arguments into a list
128+ parser .add_argument ('args' , nargs = argparse .REMAINDER )
132129
133130 try :
134- options , args = parser .parse_args (args )
131+ options = parser .parse_args (args )
135132 except OptionParserFailed :
136133 # Just let Python handle this
137134 os .execv (sys .executable , [sys .executable ] + args )
@@ -149,7 +146,7 @@ def callback(group):
149146 config = Struct ()
150147 loadini (config , options .config )
151148
152- return config , options , args
149+ return config , options , options . args
153150
154151
155152def exec_code (interpreter , args ):
0 commit comments