Skip to content

Commit 71c419d

Browse files
committed
Replace list of options with a callback
This will make it easier to later change to argparse.
1 parent 4270a7b commit 71c419d

File tree

3 files changed

+58
-57
lines changed

3 files changed

+58
-57
lines changed

bpython/args.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ def parse(args, extras=None, ignore_stdin=False):
6464
be a tuple of (title, description, options)
6565
title: The title for the option group
6666
description: A full description of the option group
67-
options: A list of optparse.Option objects to be added to the
68-
group
67+
callback: A callback that adds options to the option group
6968
7069
e.g.:
7170
71+
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')
74+
7275
parse(
7376
['-i', '-m', 'foo.py'],
7477
('Front end-specific options',
7578
'A full description of what these options are for',
76-
[optparse.Option('-f', action='store_true', dest='f', help='Explode'),
77-
optparse.Option('-l', action='store_true', dest='l', help='Love')]))
79+
callback))
7880
7981
8082
Return a tuple of (config, options, exec_args) wherein "config" is the
@@ -125,8 +127,7 @@ def parse(args, extras=None, ignore_stdin=False):
125127

126128
if extras is not None:
127129
extras_group = OptionGroup(parser, extras[0], extras[1])
128-
for option in extras[2]:
129-
extras_group.add_option(option)
130+
extras[2](extras_group)
130131
parser.add_option_group(extras_group)
131132

132133
try:

bpython/curtsies.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import collections
22
import logging
33
import sys
4-
from optparse import Option
54

65
import curtsies
76
import curtsies.window
@@ -134,25 +133,26 @@ def main(args=None, locals_=None, banner=None, welcome_message=None):
134133
"""
135134
translations.init()
136135

136+
def curtsies_arguments(parser):
137+
parser.add_argument(
138+
"--log",
139+
"-L",
140+
action="count",
141+
help=_("log debug messages to bpython.log"),
142+
)
143+
parser.add_argument(
144+
"--paste",
145+
"-p",
146+
action="store_true",
147+
help=_("start by pasting lines of a file into session"),
148+
)
149+
137150
config, options, exec_args = bpargs.parse(
138151
args,
139152
(
140153
"curtsies options",
141154
None,
142-
[
143-
Option(
144-
"--log",
145-
"-L",
146-
action="count",
147-
help=_("log debug messages to bpython.log"),
148-
),
149-
Option(
150-
"--paste",
151-
"-p",
152-
action="store_true",
153-
help=_("start by pasting lines of a file into session"),
154-
),
155-
],
155+
curtsies_arguments,
156156
),
157157
)
158158
if options.log is None:

bpython/urwid.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import time
3838
import locale
3939
import signal
40-
from optparse import Option
4140

4241
from pygments.token import Token
4342

@@ -1120,47 +1119,48 @@ def tab(self, back=False):
11201119
def main(args=None, locals_=None, banner=None):
11211120
translations.init()
11221121

1122+
def options_callback(group):
1123+
group.add_option(
1124+
"--twisted",
1125+
"-T",
1126+
action="store_true",
1127+
help=_("Run twisted reactor."),
1128+
)
1129+
group.add_option(
1130+
"--reactor",
1131+
"-r",
1132+
help=_(
1133+
"Select specific reactor (see --help-reactors). "
1134+
"Implies --twisted."
1135+
),
1136+
)
1137+
group.add_option(
1138+
"--help-reactors",
1139+
action="store_true",
1140+
help=_("List available reactors for -r."),
1141+
)
1142+
group.add_option(
1143+
"--plugin",
1144+
"-p",
1145+
help=_(
1146+
"twistd plugin to run (use twistd for a list). "
1147+
'Use "--" to pass further options to the plugin.'
1148+
),
1149+
)
1150+
group.add_option(
1151+
"--server",
1152+
"-s",
1153+
type="int",
1154+
help=_("Port to run an eval server on (forces Twisted)."),
1155+
)
1156+
11231157
# TODO: maybe support displays other than raw_display?
11241158
config, options, exec_args = bpargs.parse(
11251159
args,
11261160
(
11271161
"Urwid options",
11281162
None,
1129-
[
1130-
Option(
1131-
"--twisted",
1132-
"-T",
1133-
action="store_true",
1134-
help=_("Run twisted reactor."),
1135-
),
1136-
Option(
1137-
"--reactor",
1138-
"-r",
1139-
help=_(
1140-
"Select specific reactor (see --help-reactors). "
1141-
"Implies --twisted."
1142-
),
1143-
),
1144-
Option(
1145-
"--help-reactors",
1146-
action="store_true",
1147-
help=_("List available reactors for -r."),
1148-
),
1149-
Option(
1150-
"--plugin",
1151-
"-p",
1152-
help=_(
1153-
"twistd plugin to run (use twistd for a list). "
1154-
'Use "--" to pass further options to the plugin.'
1155-
),
1156-
),
1157-
Option(
1158-
"--server",
1159-
"-s",
1160-
type="int",
1161-
help=_("Port to run an eval server on (forces Twisted)."),
1162-
),
1163-
],
1163+
options_callback,
11641164
),
11651165
)
11661166

0 commit comments

Comments
 (0)