Skip to content

Commit 77c391a

Browse files
Harry-Leeshugovkpicnixz
authored
gh-131524: Update platform CLI to use argparse (#131542)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent ac56f8c commit 77c391a

File tree

3 files changed

+103
-6
lines changed

3 files changed

+103
-6
lines changed

Lib/platform.py

+37-5
Original file line numberDiff line numberDiff line change
@@ -1464,9 +1464,41 @@ def invalidate_caches():
14641464

14651465
### Command line interface
14661466

1467-
if __name__ == '__main__':
1468-
# Default is to print the aliased verbose platform string
1469-
terse = ('terse' in sys.argv or '--terse' in sys.argv)
1470-
aliased = (not 'nonaliased' in sys.argv and not '--nonaliased' in sys.argv)
1467+
def _parse_args(args: list[str] | None):
1468+
import argparse
1469+
1470+
parser = argparse.ArgumentParser()
1471+
parser.add_argument("args", nargs="*", choices=["nonaliased", "terse"])
1472+
parser.add_argument(
1473+
"--terse",
1474+
action="store_true",
1475+
help=(
1476+
"return only the absolute minimum information needed "
1477+
"to identify the platform"
1478+
),
1479+
)
1480+
parser.add_argument(
1481+
"--nonaliased",
1482+
dest="aliased",
1483+
action="store_false",
1484+
help=(
1485+
"disable system/OS name aliasing. If aliasing is enabled, "
1486+
"some platforms report system names different from "
1487+
"their common names, e.g. SunOS is reported as Solaris"
1488+
),
1489+
)
1490+
1491+
return parser.parse_args(args)
1492+
1493+
1494+
def _main(args: list[str] | None = None):
1495+
args = _parse_args(args)
1496+
1497+
terse = args.terse or ("terse" in args.args)
1498+
aliased = args.aliased and ('nonaliased' not in args.args)
1499+
14711500
print(platform(aliased, terse))
1472-
sys.exit(0)
1501+
1502+
1503+
if __name__ == "__main__":
1504+
_main()

Lib/test/test_platform.py

+64-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import os
1+
import contextlib
22
import copy
3+
import io
4+
import itertools
5+
import os
36
import pickle
47
import platform
58
import subprocess
@@ -741,5 +744,65 @@ def test_parse_os_release(self):
741744
self.assertEqual(len(info["SPECIALS"]), 5)
742745

743746

747+
class CommandLineTest(unittest.TestCase):
748+
def setUp(self):
749+
platform.invalidate_caches()
750+
self.addCleanup(platform.invalidate_caches)
751+
752+
def invoke_platform(self, *flags):
753+
output = io.StringIO()
754+
with contextlib.redirect_stdout(output):
755+
platform._main(args=flags)
756+
return output.getvalue()
757+
758+
def test_unknown_flag(self):
759+
with self.assertRaises(SystemExit):
760+
output = io.StringIO()
761+
# suppress argparse error message
762+
with contextlib.redirect_stderr(output):
763+
_ = self.invoke_platform('--unknown')
764+
self.assertStartsWith(output, "usage: ")
765+
766+
def test_invocation(self):
767+
flags = (
768+
"--terse", "--nonaliased", "terse", "nonaliased"
769+
)
770+
771+
for r in range(len(flags) + 1):
772+
for combination in itertools.combinations(flags, r):
773+
self.invoke_platform(*combination)
774+
775+
def test_arg_parsing(self):
776+
# For backwards compatibility, the `aliased` and `terse` parameters are
777+
# computed based on a combination of positional arguments and flags.
778+
#
779+
# Test that the arguments are correctly passed to the underlying
780+
# `platform.platform()` call.
781+
options = (
782+
(["--nonaliased"], False, False),
783+
(["nonaliased"], False, False),
784+
(["--terse"], True, True),
785+
(["terse"], True, True),
786+
(["nonaliased", "terse"], False, True),
787+
(["--nonaliased", "terse"], False, True),
788+
(["--terse", "nonaliased"], False, True),
789+
)
790+
791+
for flags, aliased, terse in options:
792+
with self.subTest(flags=flags, aliased=aliased, terse=terse):
793+
with mock.patch.object(platform, 'platform') as obj:
794+
self.invoke_platform(*flags)
795+
obj.assert_called_once_with(aliased, terse)
796+
797+
def test_help(self):
798+
output = io.StringIO()
799+
800+
with self.assertRaises(SystemExit):
801+
with contextlib.redirect_stdout(output):
802+
platform._main(args=["--help"])
803+
804+
self.assertStartsWith(output.getvalue(), "usage:")
805+
806+
744807
if __name__ == '__main__':
745808
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add help message to :mod:`platform` command-line interface. Contributed by
2+
Harry Lees.

0 commit comments

Comments
 (0)