X Tutup
Skip to content

Commit ce29d8b

Browse files
committed
Replace deprecated optparse with argparse
1 parent 0327893 commit ce29d8b

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

bpython/args.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@
2929
import importlib.util
3030
import os
3131
import sys
32-
from optparse import OptionParser, OptionGroup
32+
import argparse
3333

3434
from . import __version__, __copyright__
3535
from .config import default_config_path, loadini, Struct
3636
from .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

4848
def version_banner(base="bpython"):
@@ -60,17 +60,17 @@ def copyright_banner():
6060

6161
def 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

155152
def exec_code(interpreter, args):

bpython/curtsies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ def main(args=None, locals_=None, banner=None, welcome_message=None):
134134
translations.init()
135135

136136
def curtsies_arguments(parser):
137-
parser.add_option(
137+
parser.add_argument(
138138
"--log",
139139
"-L",
140140
action="count",
141141
help=_("log debug messages to bpython.log"),
142142
)
143-
parser.add_option(
143+
parser.add_argument(
144144
"--paste",
145145
"-p",
146146
action="store_true",

bpython/urwid.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,37 +1120,37 @@ def main(args=None, locals_=None, banner=None):
11201120
translations.init()
11211121

11221122
def options_callback(group):
1123-
group.add_option(
1123+
group.add_argument(
11241124
"--twisted",
11251125
"-T",
11261126
action="store_true",
11271127
help=_("Run twisted reactor."),
11281128
)
1129-
group.add_option(
1129+
group.add_argument(
11301130
"--reactor",
11311131
"-r",
11321132
help=_(
11331133
"Select specific reactor (see --help-reactors). "
11341134
"Implies --twisted."
11351135
),
11361136
)
1137-
group.add_option(
1137+
group.add_argument(
11381138
"--help-reactors",
11391139
action="store_true",
11401140
help=_("List available reactors for -r."),
11411141
)
1142-
group.add_option(
1142+
group.add_argument(
11431143
"--plugin",
11441144
"-p",
11451145
help=_(
11461146
"twistd plugin to run (use twistd for a list). "
11471147
'Use "--" to pass further options to the plugin.'
11481148
),
11491149
)
1150-
group.add_option(
1150+
group.add_argument(
11511151
"--server",
11521152
"-s",
1153-
type="int",
1153+
type=int,
11541154
help=_("Port to run an eval server on (forces Twisted)."),
11551155
)
11561156

0 commit comments

Comments
 (0)
X Tutup