mirror of
https://github.com/python/cpython.git
synced 2025-07-07 11:25:30 +00:00
gh-130645: Add color to stdlib argparse CLIs (gh-133380)
This commit is contained in:
parent
2b4e2b7830
commit
4ac916ae33
36 changed files with 66 additions and 33 deletions
|
@ -630,7 +630,7 @@ def main(args=None):
|
|||
import argparse
|
||||
import sys
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument('infile', nargs='?', default='-',
|
||||
help='the file to parse; defaults to stdin')
|
||||
parser.add_argument('-m', '--mode', default='exec',
|
||||
|
|
|
@ -810,7 +810,7 @@ def timegm(tuple):
|
|||
|
||||
def main(args=None):
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
textgroup = parser.add_argument_group('text only arguments')
|
||||
htmlgroup = parser.add_argument_group('html only arguments')
|
||||
textgroup.add_argument(
|
||||
|
|
|
@ -385,7 +385,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=Fa
|
|||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument('-q', action='store_true',
|
||||
help="don't print version and copyright messages")
|
||||
args = parser.parse_args()
|
||||
|
|
|
@ -317,7 +317,9 @@ def main():
|
|||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Utilities to support installing Python libraries.')
|
||||
description='Utilities to support installing Python libraries.',
|
||||
color=True,
|
||||
)
|
||||
parser.add_argument('-l', action='store_const', const=0,
|
||||
default=None, dest='maxlevels',
|
||||
help="don't recurse into subdirectories")
|
||||
|
|
|
@ -1131,7 +1131,7 @@ class Bytecode:
|
|||
def main(args=None):
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument('-C', '--show-caches', action='store_true',
|
||||
help='show inline caches')
|
||||
parser.add_argument('-O', '--show-offsets', action='store_true',
|
||||
|
|
|
@ -2870,7 +2870,7 @@ __test__ = {"_TestClass": _TestClass,
|
|||
def _test():
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description="doctest runner")
|
||||
parser = argparse.ArgumentParser(description="doctest runner", color=True)
|
||||
parser.add_argument('-v', '--verbose', action='store_true', default=False,
|
||||
help='print very verbose output for all tests')
|
||||
parser.add_argument('-o', '--option', action='append',
|
||||
|
|
|
@ -205,7 +205,7 @@ def _uninstall_helper(*, verbosity=0):
|
|||
|
||||
def _main(argv=None):
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument(
|
||||
"--version",
|
||||
action="version",
|
||||
|
|
|
@ -667,7 +667,9 @@ def main():
|
|||
from argparse import ArgumentParser
|
||||
parser = ArgumentParser(description=
|
||||
"A simple command line interface for the gzip module: act like gzip, "
|
||||
"but do not delete the input file.")
|
||||
"but do not delete the input file.",
|
||||
color=True,
|
||||
)
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
group.add_argument('--fast', action='store_true', help='compress faster')
|
||||
group.add_argument('--best', action='store_true', help='compress better')
|
||||
|
|
|
@ -1340,7 +1340,7 @@ if __name__ == '__main__':
|
|||
import argparse
|
||||
import contextlib
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument('--cgi', action='store_true',
|
||||
help='run as CGI server')
|
||||
parser.add_argument('-b', '--bind', metavar='ADDRESS',
|
||||
|
|
|
@ -3343,7 +3343,7 @@ def _main():
|
|||
import argparse
|
||||
import importlib
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument(
|
||||
'object',
|
||||
help="The object to be analysed. "
|
||||
|
|
|
@ -44,7 +44,7 @@ def _colorize_json(json_str):
|
|||
def main():
|
||||
description = ('A simple command line interface for json module '
|
||||
'to validate and pretty-print JSON objects.')
|
||||
parser = argparse.ArgumentParser(description=description)
|
||||
parser = argparse.ArgumentParser(description=description, color=True)
|
||||
parser.add_argument('infile', nargs='?',
|
||||
help='a JSON file to be validated or pretty-printed',
|
||||
default='-')
|
||||
|
|
|
@ -698,7 +698,9 @@ _default_mime_types()
|
|||
def _parse_args(args):
|
||||
from argparse import ArgumentParser
|
||||
|
||||
parser = ArgumentParser(description='map filename extensions to MIME types')
|
||||
parser = ArgumentParser(
|
||||
description='map filename extensions to MIME types', color=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-e', '--extension',
|
||||
action='store_true',
|
||||
|
|
11
Lib/pdb.py
11
Lib/pdb.py
|
@ -3296,10 +3296,13 @@ To let the script run up to a given line X in the debugged file, use
|
|||
def main():
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(usage="%(prog)s [-h] [-c command] (-m module | -p pid | pyfile) [args ...]",
|
||||
description=_usage,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
allow_abbrev=False)
|
||||
parser = argparse.ArgumentParser(
|
||||
usage="%(prog)s [-h] [-c command] (-m module | -p pid | pyfile) [args ...]",
|
||||
description=_usage,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
allow_abbrev=False,
|
||||
color=True,
|
||||
)
|
||||
|
||||
# We need to maunally get the script from args, because the first positional
|
||||
# arguments could be either the script we need to debug, or the argument
|
||||
|
|
|
@ -1911,7 +1911,9 @@ def _main(args=None):
|
|||
import argparse
|
||||
import pprint
|
||||
parser = argparse.ArgumentParser(
|
||||
description='display contents of the pickle files')
|
||||
description='display contents of the pickle files',
|
||||
color=True,
|
||||
)
|
||||
parser.add_argument(
|
||||
'pickle_file',
|
||||
nargs='+', help='the pickle file')
|
||||
|
|
|
@ -2842,7 +2842,9 @@ __test__ = {'disassembler_test': _dis_test,
|
|||
if __name__ == "__main__":
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(
|
||||
description='disassemble one or more pickle files')
|
||||
description='disassemble one or more pickle files',
|
||||
color=True,
|
||||
)
|
||||
parser.add_argument(
|
||||
'pickle_file',
|
||||
nargs='+', help='the pickle file')
|
||||
|
|
|
@ -1467,7 +1467,7 @@ def invalidate_caches():
|
|||
def _parse_args(args: list[str] | None):
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument("args", nargs="*", choices=["nonaliased", "terse"])
|
||||
parser.add_argument(
|
||||
"--terse",
|
||||
|
|
|
@ -177,7 +177,7 @@ def main():
|
|||
import argparse
|
||||
|
||||
description = 'A simple command-line interface for py_compile module.'
|
||||
parser = argparse.ArgumentParser(description=description)
|
||||
parser = argparse.ArgumentParser(description=description, color=True)
|
||||
parser.add_argument(
|
||||
'-q', '--quiet',
|
||||
action='store_true',
|
||||
|
|
|
@ -1011,7 +1011,7 @@ if hasattr(_os, "fork"):
|
|||
def _parse_args(arg_list: list[str] | None):
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(
|
||||
formatter_class=argparse.RawTextHelpFormatter)
|
||||
formatter_class=argparse.RawTextHelpFormatter, color=True)
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
group.add_argument(
|
||||
"-c", "--choice", nargs="+",
|
||||
|
|
|
@ -65,6 +65,7 @@ class SqliteInteractiveConsole(InteractiveConsole):
|
|||
def main(*args):
|
||||
parser = ArgumentParser(
|
||||
description="Python sqlite3 CLI",
|
||||
color=True,
|
||||
)
|
||||
parser.add_argument(
|
||||
"filename", type=str, default=":memory:", nargs="?",
|
||||
|
|
|
@ -2883,7 +2883,7 @@ def main():
|
|||
import argparse
|
||||
|
||||
description = 'A simple command-line interface for tarfile module.'
|
||||
parser = argparse.ArgumentParser(description=description)
|
||||
parser = argparse.ArgumentParser(description=description, color=True)
|
||||
parser.add_argument('-v', '--verbose', action='store_true', default=False,
|
||||
help='Verbose output')
|
||||
parser.add_argument('--filter', metavar='<filtername>',
|
||||
|
|
|
@ -3289,6 +3289,7 @@ class CommandLineTests(unittest.TestCase):
|
|||
with self.subTest(flags=args):
|
||||
self.invoke_ast(*args)
|
||||
|
||||
@support.force_not_colorized
|
||||
def test_help_message(self):
|
||||
for flag in ('-h', '--help', '--unknown'):
|
||||
with self.subTest(flag=flag):
|
||||
|
|
|
@ -987,6 +987,7 @@ class CommandLineTestCase(unittest.TestCase):
|
|||
self.assertCLIFails(*args)
|
||||
self.assertCmdFails(*args)
|
||||
|
||||
@support.force_not_colorized
|
||||
def test_help(self):
|
||||
stdout = self.run_cmd_ok('-h')
|
||||
self.assertIn(b'usage:', stdout)
|
||||
|
|
|
@ -6,7 +6,7 @@ import sys
|
|||
import unittest.mock
|
||||
from platform import win32_edition
|
||||
from test import support
|
||||
from test.support import os_helper
|
||||
from test.support import force_not_colorized, os_helper
|
||||
|
||||
try:
|
||||
import _winapi
|
||||
|
@ -437,6 +437,7 @@ class MiscTestCase(unittest.TestCase):
|
|||
|
||||
|
||||
class CommandLineTest(unittest.TestCase):
|
||||
@force_not_colorized
|
||||
def test_parse_args(self):
|
||||
args, help_text = mimetypes._parse_args("-h")
|
||||
self.assertTrue(help_text.startswith("usage: "))
|
||||
|
|
|
@ -745,6 +745,7 @@ class CommandLineTest(unittest.TestCase):
|
|||
expect = self.text_normalize(expect)
|
||||
self.assertListEqual(res.splitlines(), expect.splitlines())
|
||||
|
||||
@support.force_not_colorized
|
||||
def test_unknown_flag(self):
|
||||
stderr = io.StringIO()
|
||||
with self.assertRaises(SystemExit):
|
||||
|
|
|
@ -794,6 +794,7 @@ class CommandLineTest(unittest.TestCase):
|
|||
self.invoke_platform(*flags)
|
||||
obj.assert_called_once_with(aliased, terse)
|
||||
|
||||
@support.force_not_colorized
|
||||
def test_help(self):
|
||||
output = io.StringIO()
|
||||
|
||||
|
|
|
@ -1411,6 +1411,7 @@ class TestModule(unittest.TestCase):
|
|||
|
||||
|
||||
class CommandLineTest(unittest.TestCase):
|
||||
@support.force_not_colorized
|
||||
def test_parse_args(self):
|
||||
args, help_text = random._parse_args(shlex.split("--choice a b c"))
|
||||
self.assertEqual(args.choice, ["a", "b", "c"])
|
||||
|
|
|
@ -4,7 +4,12 @@ import unittest
|
|||
|
||||
from sqlite3.__main__ import main as cli
|
||||
from test.support.os_helper import TESTFN, unlink
|
||||
from test.support import captured_stdout, captured_stderr, captured_stdin
|
||||
from test.support import (
|
||||
captured_stdout,
|
||||
captured_stderr,
|
||||
captured_stdin,
|
||||
force_not_colorized,
|
||||
)
|
||||
|
||||
|
||||
class CommandLineInterface(unittest.TestCase):
|
||||
|
@ -32,6 +37,7 @@ class CommandLineInterface(unittest.TestCase):
|
|||
self.assertEqual(out, "")
|
||||
return err
|
||||
|
||||
@force_not_colorized
|
||||
def test_cli_help(self):
|
||||
out = self.expect_success("-h")
|
||||
self.assertIn("usage: ", out)
|
||||
|
|
|
@ -518,7 +518,7 @@ def _main(args=None):
|
|||
sys.exit(1)
|
||||
|
||||
# Parse the arguments and options
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument(dest='filename', nargs='?',
|
||||
metavar='filename.py',
|
||||
help='the file to tokenize; defaults to stdin')
|
||||
|
|
|
@ -604,7 +604,7 @@ class Trace:
|
|||
def main():
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument('--version', action='version', version='trace 2.0')
|
||||
|
||||
grp = parser.add_argument_group('Main options',
|
||||
|
|
|
@ -197,7 +197,7 @@ class TestProgram(object):
|
|||
return parser
|
||||
|
||||
def _getMainArgParser(self, parent):
|
||||
parser = argparse.ArgumentParser(parents=[parent])
|
||||
parser = argparse.ArgumentParser(parents=[parent], color=True)
|
||||
parser.prog = self.progName
|
||||
parser.print_help = self._print_help
|
||||
|
||||
|
@ -208,7 +208,7 @@ class TestProgram(object):
|
|||
return parser
|
||||
|
||||
def _getDiscoveryArgParser(self, parent):
|
||||
parser = argparse.ArgumentParser(parents=[parent])
|
||||
parser = argparse.ArgumentParser(parents=[parent], color=True)
|
||||
parser.prog = '%s discover' % self.progName
|
||||
parser.epilog = ('For test discovery all test modules must be '
|
||||
'importable from the top level directory of the '
|
||||
|
|
|
@ -949,7 +949,9 @@ def main():
|
|||
import argparse
|
||||
parser = argparse.ArgumentParser(
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
description="Generate a UUID using the selected UUID function.")
|
||||
description="Generate a UUID using the selected UUID function.",
|
||||
color=True,
|
||||
)
|
||||
parser.add_argument("-u", "--uuid",
|
||||
choices=uuid_funcs.keys(),
|
||||
default="uuid4",
|
||||
|
|
|
@ -624,7 +624,9 @@ def main(args=None):
|
|||
'created, you may wish to '
|
||||
'activate it, e.g. by '
|
||||
'sourcing an activate script '
|
||||
'in its bin directory.')
|
||||
'in its bin directory.',
|
||||
color=True,
|
||||
)
|
||||
parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
|
||||
help='A directory to create the environment in.')
|
||||
parser.add_argument('--system-site-packages', default=False,
|
||||
|
|
|
@ -719,7 +719,9 @@ if sys.platform == "ios":
|
|||
|
||||
def parse_args(arg_list: list[str] | None):
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(description="Open URL in a web browser.")
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Open URL in a web browser.", color=True,
|
||||
)
|
||||
parser.add_argument("url", help="URL to open")
|
||||
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
|
|
|
@ -187,7 +187,7 @@ def main(args=None):
|
|||
"""
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser = argparse.ArgumentParser(color=True)
|
||||
parser.add_argument('--output', '-o', default=None,
|
||||
help="The name of the output archive. "
|
||||
"Required if SOURCE is an archive.")
|
||||
|
|
|
@ -2317,7 +2317,7 @@ def main(args=None):
|
|||
import argparse
|
||||
|
||||
description = 'A simple command-line interface for zipfile module.'
|
||||
parser = argparse.ArgumentParser(description=description)
|
||||
parser = argparse.ArgumentParser(description=description, color=True)
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument('-l', '--list', metavar='<zipfile>',
|
||||
help='Show listing of a zipfile')
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Add color to stdlib argparse CLIs. Patch by Hugo van Kemenade.
|
Loading…
Add table
Add a link
Reference in a new issue