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 ):
4444def 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 extraoptions : this should
64- be a tuple of (title, description,options )
65- title: The title for theoption group
66- description: A full description of theoption group
67- callback: A callback that addsoptions to theoption group
63+ take appropriate action. Also receive optional extraargument : this should
64+ be a tuple of (title, description,callback )
65+ title: The title for theargument group
66+ description: A full description of theargument group
67+ callback: A callback that addsargument to theargument 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 """
8888if args is None :
8989args = sys .argv [1 :]
9090
91- parser = RaisingOptionParser (
91+ parser = RaisingArgumentParser (
9292usage = _ (
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" ,
106101default = default_config_path (),
107102help = _ ("Use CONFIG instead of default config file." ),
108103 )
109- parser .add_option (
104+ parser .add_argument (
110105"--interactive" ,
111106"-i" ,
112107action = "store_true" ,
113108help = _ ("Drop to bpython shell after running file instead of exiting." ),
114109 )
115- parser .add_option (
110+ parser .add_argument (
116111"--quiet" ,
117112"-q" ,
118113action = "store_true" ,
119114help = _ ("Don't flush the output to stdout." ),
120115 )
121- parser .add_option (
116+ parser .add_argument (
122117"--version" ,
123118"-V" ,
124119action = "store_true" ,
125120help = _ ("Print version and exit." ),
126121 )
127122
128123if extras is not None :
129- extras_group = OptionGroup ( parser , extras [0 ],extras [1 ])
124+ extras_group = parser . add_argument_group ( extras [0 ],extras [1 ])
130125extras [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
133130try :
134- options , args = parser .parse_args (args )
131+ options = parser .parse_args (args )
135132except OptionParserFailed :
136133# Just let Python handle this
137134os .execv (sys .executable , [sys .executable ]+ args )
@@ -149,7 +146,7 @@ def callback(group):
149146config = Struct ()
150147loadini (config ,options .config )
151148
152- return config ,options ,args
149+ return config ,options ,options . args
153150
154151
155152def exec_code (interpreter ,args ):