Skip to content

Commit 52b5eb9

Browse files
simon04picnixz
andauthored
pythongh-131236: allow to generate multiple UUIDs at once via CLI (python#131218)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 4b3d5b6 commit 52b5eb9

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

Doc/library/uuid.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,13 @@ The following options are accepted:
377377
The name used as part of generating the uuid. Only required for
378378
:func:`uuid3` / :func:`uuid5` functions.
379379

380+
.. option:: -C <num>
381+
--count <num>
382+
383+
Generate *num* fresh UUIDs.
384+
385+
.. versionadded:: next
386+
380387

381388
.. _uuid-example:
382389

@@ -432,16 +439,18 @@ Here are some examples of typical usage of the :mod:`uuid` module::
432439
Command-Line Example
433440
--------------------
434441

435-
Here are some examples of typical usage of the :mod:`uuid` command line interface:
442+
Here are some examples of typical usage of the :mod:`uuid` command-line interface:
436443

437444
.. code-block:: shell
438445
439-
# generate a random uuid - by default uuid4() is used
446+
# generate a random UUID - by default uuid4() is used
440447
$ python -m uuid
441448
442-
# generate a uuid using uuid1()
449+
# generate a UUID using uuid1()
443450
$ python -m uuid -u uuid1
444451
445-
# generate a uuid using uuid5
452+
# generate a UUID using uuid5
446453
$ python -m uuid -u uuid5 -n @url -N example.com
447454
455+
# generate 42 random UUIDs
456+
$ python -m uuid -C 42

Doc/whatsnew/3.14.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,9 @@ uuid
10171017
Nil and Max UUID formats as defined by :rfc:`9562`.
10181018
(Contributed by Nick Pope in :gh:`128427`.)
10191019

1020+
* Allow to generate multiple UUIDs at once via :option:`python -m uuid --count <uuid --count>`.
1021+
(Contributed by Simon Legner in :gh:`131236`.)
1022+
10201023

10211024
zipinfo
10221025
-------

Lib/test/test_uuid.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,20 @@ def test_cli_uuid4_outputted_with_no_args(self):
11661166
self.assertEqual(output, str(uuid_output))
11671167
self.assertEqual(uuid_output.version, 4)
11681168

1169+
@mock.patch.object(sys, "argv", ["", "-C", "3"])
1170+
def test_cli_uuid4_outputted_with_count(self):
1171+
stdout = io.StringIO()
1172+
with contextlib.redirect_stdout(stdout):
1173+
self.uuid.main()
1174+
1175+
output = stdout.getvalue().strip().splitlines()
1176+
1177+
# Check that 3 UUIDs in the format of uuid4 have been generated
1178+
self.assertEqual(len(output), 3)
1179+
for o in output:
1180+
uuid_output = self.uuid.UUID(o)
1181+
self.assertEqual(uuid_output.version, 4)
1182+
11691183
@mock.patch.object(sys, "argv",
11701184
["", "-u", "uuid3", "-n", "@dns", "-N", "python.org"])
11711185
def test_cli_uuid3_ouputted_with_valid_namespace_and_name(self):

Lib/uuid.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -937,18 +937,22 @@ def main():
937937

938938
import argparse
939939
parser = argparse.ArgumentParser(
940-
description="Generates a uuid using the selected uuid function.")
941-
parser.add_argument("-u", "--uuid", choices=uuid_funcs.keys(), default="uuid4",
942-
help="The function to use to generate the uuid. "
943-
"By default uuid4 function is used.")
940+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
941+
description="Generate a UUID using the selected UUID function.")
942+
parser.add_argument("-u", "--uuid",
943+
choices=uuid_funcs.keys(),
944+
default="uuid4",
945+
help="function to generate the UUID")
944946
parser.add_argument("-n", "--namespace",
945-
help="The namespace is a UUID, or '@ns' where 'ns' is a "
946-
"well-known predefined UUID addressed by namespace name. "
947-
"Such as @dns, @url, @oid, and @x500. "
948-
"Only required for uuid3/uuid5 functions.")
947+
choices=["any UUID", *namespaces.keys()],
948+
help="uuid3/uuid5 only: "
949+
"a UUID, or a well-known predefined UUID addressed "
950+
"by namespace name")
949951
parser.add_argument("-N", "--name",
950-
help="The name used as part of generating the uuid. "
951-
"Only required for uuid3/uuid5 functions.")
952+
help="uuid3/uuid5 only: "
953+
"name used as part of generating the UUID")
954+
parser.add_argument("-C", "--count", metavar="NUM", type=int, default=1,
955+
help="generate NUM fresh UUIDs")
952956

953957
args = parser.parse_args()
954958
uuid_func = uuid_funcs[args.uuid]
@@ -963,9 +967,11 @@ def main():
963967
"Run 'python -m uuid -h' for more information."
964968
)
965969
namespace = namespaces[namespace] if namespace in namespaces else UUID(namespace)
966-
print(uuid_func(namespace, name))
970+
for _ in range(args.count):
971+
print(uuid_func(namespace, name))
967972
else:
968-
print(uuid_func())
973+
for _ in range(args.count):
974+
print(uuid_func())
969975

970976

971977
# The following standard UUIDs are for use with uuid3() or uuid5().
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow to generate multiple UUIDs at once via :option:`python -m uuid --count <uuid --count>`.

0 commit comments

Comments
 (0)