Skip to content

Commit 5eed43f

Browse files
committed
Convert platform CLI to use argparse
1 parent ce79274 commit 5eed43f

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

Lib/platform.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,9 +1464,24 @@ 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)
1471-
print(platform(aliased, terse))
1472-
sys.exit(0)
1467+
def _parse_args(args: list[str] | None):
1468+
import argparse
1469+
1470+
parser = argparse.ArgumentParser()
1471+
parser.add_argument("--terse", action="store_true")
1472+
parser.add_argument("--nonaliased", action="store_true")
1473+
1474+
return parser.parse_args(args)
1475+
1476+
1477+
def _main(args: list[str] | None = None):
1478+
args = _parse_args(args)
1479+
1480+
aliased = not args.nonaliased
1481+
1482+
print(platform(aliased, args.terse))
1483+
return 0
1484+
1485+
1486+
if __name__ == "__main__":
1487+
raise SystemExit(_main())

Lib/test/test_platform.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import io
2+
import itertools
13
import os
24
import copy
5+
import contextlib
36
import pickle
47
import platform
58
import subprocess
69
import sys
710
import unittest
811
from unittest import mock
12+
from textwrap import dedent
913

1014
from test import support
1115
from test.support import os_helper
@@ -741,5 +745,52 @@ def test_parse_os_release(self):
741745
self.assertEqual(len(info["SPECIALS"]), 5)
742746

743747

748+
class CommandLineTest(unittest.TestCase):
749+
def setUp(self):
750+
self.clear_caches()
751+
self.addCleanup(self.clear_caches)
752+
753+
def clear_caches(self):
754+
platform._platform_cache.clear()
755+
platform._sys_version_cache.clear()
756+
platform._uname_cache = None
757+
platform._os_release_cache = None
758+
759+
@staticmethod
760+
def text_normalize(string):
761+
"""Dedent *string* and strip it from its surrounding whitespaces.
762+
This method is used by the other utility functions so that any
763+
string to write or to match against can be freely indented.
764+
"""
765+
return dedent(string).strip()
766+
767+
def invoke_platform(self, *flags):
768+
output = io.StringIO()
769+
with contextlib.redirect_stdout(output):
770+
platform._main(args=flags)
771+
return self.text_normalize(output.getvalue())
772+
773+
def test_unknown_flag(self):
774+
with self.assertRaises(SystemExit):
775+
# suppress argparse error message
776+
with contextlib.redirect_stderr(io.StringIO()):
777+
_ = self.invoke_platform('--unknown')
778+
779+
def test_invocation(self):
780+
self.invoke_platform("--terse", "--nonaliased")
781+
self.invoke_platform("--nonaliased")
782+
self.invoke_platform("--terse")
783+
self.invoke_platform()
784+
785+
def test_help(self):
786+
output = io.StringIO()
787+
788+
with self.assertRaises(SystemExit):
789+
with contextlib.redirect_stdout(output):
790+
platform._main(args=["--help"])
791+
792+
self.assertIn("usage:", output.getvalue())
793+
794+
744795
if __name__ == '__main__':
745796
unittest.main()

0 commit comments

Comments
 (0)