Skip to content

Commit ecaf806

Browse files
committed
pythongh-93096: Port a bunch of stdlib cli to ArgumentParser
1 parent a95e60d commit ecaf806

File tree

3 files changed

+57
-60
lines changed

3 files changed

+57
-60
lines changed

Lib/filecmp.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,14 @@ def _filter(flist, skip):
298298
# Demonstration and testing.
299299
#
300300
def demo():
301-
import sys
302-
import getopt
303-
options, args = getopt.getopt(sys.argv[1:], 'r')
304-
if len(args) != 2:
305-
raise getopt.GetoptError('need exactly two args', None)
306-
dd = dircmp(args[0], args[1])
307-
if ('-r', '') in options:
301+
from argparse import ArgumentParser
302+
parser = ArgumentParser(description='compare directories and files')
303+
parser.add_argument('-r', action='store_true', help='recursive comparison')
304+
parser.add_argument('dir_a', help='one side of the comparison')
305+
parser.add_argument('dir_b', help='another side of the comparison')
306+
arguments = parser.parse_args()
307+
dd = dircmp(arguments['dir_a'], arguments['dir_b'])
308+
if arguments['r']:
308309
dd.report_full_closure()
309310
else:
310311
dd.report()

Lib/fileinput.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -424,19 +424,20 @@ def openhook(filename, mode):
424424

425425

426426
def _test():
427-
import getopt
428-
inplace = False
429-
backup = False
430-
opts, args = getopt.getopt(sys.argv[1:], "ib:")
431-
for o, a in opts:
432-
if o == '-i': inplace = True
433-
if o == '-b': backup = a
434-
for line in input(args, inplace=inplace, backup=backup):
435-
if line[-1:] == '\n': line = line[:-1]
436-
if line[-1:] == '\r': line = line[:-1]
437-
print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(),
438-
isfirstline() and "*" or "", line))
439-
print("%d: %s[%d]" % (lineno(), filename(), filelineno()))
427+
from argparse import ArgumentParser
428+
parser = ArgumentParser(description='show glued content of files')
429+
parser.add_argument('-i', '--inplace', action='store_true',
430+
help='access original files, not copies')
431+
parser.add_argument('-b', '--backup', action='store_true',
432+
help='file extension for the copies')
433+
parser.add_argument('file', nargs='+', help='Input path')
434+
arguments = parser.parse_args()
435+
436+
for line in input(arguments.file, arguments.inplace, arguments.backup):
437+
line = line.rstrip('\r\n')
438+
start_flag = '*' if isfirstline() else ''
439+
print(f'{lineno()}: {filename()}[{filelineno()}]{start_flag} {line}')
440+
print(f'{lineno()}: {filename()}[{filelineno()}]')
440441

441442
if __name__ == '__main__':
442443
_test()

Lib/idlelib/pyshell.py

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,34 +1460,14 @@ def fix_x11_paste(root):
14601460
root.bind_class(cls, '<<Paste>>'))
14611461

14621462

1463-
usage_msg = """\
1464-
1465-
USAGE: idle [-deins] [-t title] [file]*
1466-
idle [-dns] [-t title] (-c cmd | -r file) [arg]*
1467-
idle [-dns] [-t title] - [arg]*
1468-
1469-
-h print this help message and exit
1470-
-n run IDLE without a subprocess (DEPRECATED,
1471-
see Help/IDLE Help for details)
1472-
1473-
The following options will override the IDLE 'settings' configuration:
1474-
1475-
-e open an edit window
1476-
-i open a shell window
1477-
1478-
The following options imply -i and will open a shell:
1479-
1480-
-c cmd run the command in a shell, or
1481-
-r file run script from file
1482-
1483-
-d enable the debugger
1484-
-s run $IDLESTARTUP or $PYTHONSTARTUP before anything else
1485-
-t title set title of shell window
1463+
usage="""%(prog)s -h
1464+
%(prog)s [-deins] [-t title] [file ...]
1465+
%(prog)s [-dns] [-t title] (-c cmd | -r file) [arg ...]
1466+
%(prog)s [-dns] [-t title] - [arg ...]"""
14861467

1468+
epilog = """\
14871469
A default edit window will be bypassed when -c, -r, or - are used.
14881470
1489-
[arg]* are passed to the command (-c) or script (-r) in sys.argv[1:].
1490-
14911471
Examples:
14921472
14931473
idle
@@ -1511,30 +1491,45 @@ def fix_x11_paste(root):
15111491
15121492
echo "import sys; print(sys.argv)" | idle - "foobar"
15131493
Open a shell window, run the script piped in, passing '' in sys.argv[0]
1514-
and "foobar" in sys.argv[1].
1515-
"""
1494+
and "foobar" in sys.argv[1]."""
15161495

15171496
def main():
1518-
import getopt
1497+
from argparse import ArgumentParser, RawTextHelpFormatter
15191498
from platform import system
15201499
from idlelib import testing # bool value
15211500
from idlelib import macosx
15221501

15231502
global flist, root, use_subprocess
15241503

15251504
capture_warnings(True)
1526-
use_subprocess = True
1527-
enable_shell = False
1528-
enable_edit = False
1529-
debug = False
1530-
cmd = None
1531-
script = None
1532-
startup = False
1533-
try:
1534-
opts, args = getopt.getopt(sys.argv[1:], "c:deihnr:st:")
1535-
except getopt.error as msg:
1536-
print("Error: %s\n%s" % (msg, usage_msg), file=sys.stderr)
1537-
sys.exit(2)
1505+
parser = ArgumentParser(description='Python’s Integrated Development and '
1506+
'Learning Environment', usage=usage, epilog=epilog,
1507+
formatter_class=RawTextHelpFormatter)
1508+
parser.add_argument('-n', action='store_true', help='run IDLE without a '
1509+
'subprocess (DEPRECATED, see Help/IDLE Help for '
1510+
'details)')
1511+
parser.add_argument('file', nargs='*', help='file path to open')
1512+
parser.add_argument('arg', nargs='*', help='parameter passed to the '
1513+
'command (-c), script (-r), or REPL (-) in '
1514+
'sys.argv[1:]')
1515+
settings = parser.add_argument_group(description="the following options "
1516+
"have priority over the IDLE "
1517+
"'settings' configuration:")
1518+
settings.add_argument('-e', '--edit', action='store_true', help='open an edit window')
1519+
settings.add_argument('-i', '--interactive', action='store_true', help='open a shell window')
1520+
shell = parser.add_argument_group(description='the following options '
1521+
'imply -i:')
1522+
run = parser.add_mutually_exclusive_group()
1523+
run.add_argument('-c', '--command', metavar='cmd',
1524+
help='run the command in a shell, or')
1525+
run.add_argument('-r', '--run', metavar='file', help='run script from file')
1526+
shell.add_argument('-d', '--debug', action='store_true', help='enable the debugger')
1527+
shell.add_argument('-s', action='store_true', help='run $IDLESTARTUP or '
1528+
'$PYTHONSTARTUP before anything else')
1529+
shell.add_argument('-t', '--title', metavar='title', help='set title of shell window')
1530+
1531+
arguments = parser.parse_args()
1532+
15381533
for o, a in opts:
15391534
if o == '-c':
15401535
cmd = a

0 commit comments

Comments
 (0)