]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-131236: allow to generate multiple UUIDs at once via CLI (#131218)
authorSimon Legner <Simon.Legner@gmail.com>
Wed, 26 Mar 2025 20:49:28 +0000 (21:49 +0100)
committerGitHub <noreply@github.com>
Wed, 26 Mar 2025 20:49:28 +0000 (22:49 +0200)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Doc/library/uuid.rst
Doc/whatsnew/3.14.rst
Lib/test/test_uuid.py
Lib/uuid.py
Misc/NEWS.d/next/Library/2025-03-14-12-22-02.gh-issue-131236.HjqFq0.rst [new file with mode: 0644]

index e9b4fb107a58168036c4e0e7860cbd2c526aef4a..1c46d7d40bbdfa49506f74af20e2ccd94ccf893b 100644 (file)
@@ -377,6 +377,13 @@ The following options are accepted:
    The name used as part of generating the uuid. Only required for
    :func:`uuid3` / :func:`uuid5` functions.
 
+.. option:: -C <num>
+            --count <num>
+
+   Generate *num* fresh UUIDs.
+
+   .. versionadded:: next
+
 
 .. _uuid-example:
 
@@ -432,16 +439,18 @@ Here are some examples of typical usage of the :mod:`uuid` module::
 Command-Line Example
 --------------------
 
-Here are some examples of typical usage of the :mod:`uuid` command line interface:
+Here are some examples of typical usage of the :mod:`uuid` command-line interface:
 
 .. code-block:: shell
 
-   # generate a random uuid - by default uuid4() is used
+   # generate a random UUID - by default uuid4() is used
    $ python -m uuid
 
-   # generate a uuid using uuid1()
+   # generate a UUID using uuid1()
    $ python -m uuid -u uuid1
 
-   # generate a uuid using uuid5
+   # generate a UUID using uuid5
    $ python -m uuid -u uuid5 -n @url -N example.com
 
+   # generate 42 random UUIDs
+   $ python -m uuid -C 42
index a7a4617cb1d692650ad97d43bac43ce4510674e4..ac5b53ef94bfb1e287e11f4dc636555447dcfb18 100644 (file)
@@ -1017,6 +1017,9 @@ uuid
   Nil and Max UUID formats as defined by :rfc:`9562`.
   (Contributed by Nick Pope in :gh:`128427`.)
 
+* Allow to generate multiple UUIDs at once via :option:`python -m uuid --count <uuid --count>`.
+  (Contributed by Simon Legner in :gh:`131236`.)
+
 
 zipinfo
 -------
index bcc673233f6588a4a47dbbe22155e66da3edd635..91210923991f8e89f195fbbf70fd9cc5808693a1 100755 (executable)
@@ -1166,6 +1166,20 @@ class BaseTestUUID:
         self.assertEqual(output, str(uuid_output))
         self.assertEqual(uuid_output.version, 4)
 
+    @mock.patch.object(sys, "argv", ["", "-C", "3"])
+    def test_cli_uuid4_outputted_with_count(self):
+        stdout = io.StringIO()
+        with contextlib.redirect_stdout(stdout):
+            self.uuid.main()
+
+        output = stdout.getvalue().strip().splitlines()
+
+        # Check that 3 UUIDs in the format of uuid4 have been generated
+        self.assertEqual(len(output), 3)
+        for o in output:
+            uuid_output = self.uuid.UUID(o)
+            self.assertEqual(uuid_output.version, 4)
+
     @mock.patch.object(sys, "argv",
                        ["", "-u", "uuid3", "-n", "@dns", "-N", "python.org"])
     def test_cli_uuid3_ouputted_with_valid_namespace_and_name(self):
index 6f7a7a3f42ac11c37ba660cda31480e944bee239..984981512d64a69adf69befbee7b80656ea3e6fb 100644 (file)
@@ -937,18 +937,22 @@ def main():
 
     import argparse
     parser = argparse.ArgumentParser(
-        description="Generates a uuid using the selected uuid function.")
-    parser.add_argument("-u", "--uuid", choices=uuid_funcs.keys(), default="uuid4",
-                        help="The function to use to generate the uuid. "
-                        "By default uuid4 function is used.")
+        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
+        description="Generate a UUID using the selected UUID function.")
+    parser.add_argument("-u", "--uuid",
+                        choices=uuid_funcs.keys(),
+                        default="uuid4",
+                        help="function to generate the UUID")
     parser.add_argument("-n", "--namespace",
-                        help="The namespace is a UUID, or '@ns' where 'ns' is a "
-                        "well-known predefined UUID addressed by namespace name. "
-                        "Such as @dns, @url, @oid, and @x500. "
-                        "Only required for uuid3/uuid5 functions.")
+                        choices=["any UUID", *namespaces.keys()],
+                        help="uuid3/uuid5 only: "
+                        "a UUID, or a well-known predefined UUID addressed "
+                        "by namespace name")
     parser.add_argument("-N", "--name",
-                        help="The name used as part of generating the uuid. "
-                        "Only required for uuid3/uuid5 functions.")
+                        help="uuid3/uuid5 only: "
+                        "name used as part of generating the UUID")
+    parser.add_argument("-C", "--count", metavar="NUM", type=int, default=1,
+                        help="generate NUM fresh UUIDs")
 
     args = parser.parse_args()
     uuid_func = uuid_funcs[args.uuid]
@@ -963,9 +967,11 @@ def main():
                 "Run 'python -m uuid -h' for more information."
             )
         namespace = namespaces[namespace] if namespace in namespaces else UUID(namespace)
-        print(uuid_func(namespace, name))
+        for _ in range(args.count):
+            print(uuid_func(namespace, name))
     else:
-        print(uuid_func())
+        for _ in range(args.count):
+            print(uuid_func())
 
 
 # The following standard UUIDs are for use with uuid3() or uuid5().
diff --git a/Misc/NEWS.d/next/Library/2025-03-14-12-22-02.gh-issue-131236.HjqFq0.rst b/Misc/NEWS.d/next/Library/2025-03-14-12-22-02.gh-issue-131236.HjqFq0.rst
new file mode 100644 (file)
index 0000000..31c1676
--- /dev/null
@@ -0,0 +1 @@
+Allow to generate multiple UUIDs at once via :option:`python -m uuid --count <uuid --count>`.