Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/uuid.rst
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ The following options are accepted:
The name used as part of generating the uuid. Only required for
:func:`uuid3` / :func:`uuid5` functions.

.. option:: --count <count>

Generate more uuids in loop.


.. _uuid-example:

Expand Down Expand Up @@ -445,3 +449,5 @@ Here are some examples of typical usage of the :mod:`uuid` command line interfac
# generate a uuid using uuid5
$ python -m uuid -u uuid5 -n @url -N example.com

# generate 42 random uuids
$ python -m uuid --count 42
8 changes: 6 additions & 2 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,8 @@ def main():
parser.add_argument("-N", "--name",
help="The name used as part of generating the uuid. "
"Only required for uuid3/uuid5 functions.")
parser.add_argument("--count", type=int, default=1,
help="Generate more uuids in loop. ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
help="Generate more uuids in loop. ")
help="Generate COUNT UUIDs. Default: 1.")

Or add formatter_class=argparse.ArgumentDefaultsHelpFormatter to argparse.ArgumentParser() and:

Suggested change
help="Generate more uuids in loop. ")
help="Generate this many UUIDs.")

And also delete "By default uuid4 function is used."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to have a short option -C as well so to mimick uuidgen, and use NUM as a metavar instead of COUNT. In addition, we can just say Generate NUM fresh UUIDs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also delete "By default uuid4 function is used."

Isn't it the default though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but argparse.ArgumentDefaultsHelpFormatter prints the default for us:

./python.exe -m uuid -h
usage: python.exe -m uuid [-h]
                          [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}]
                          [-n NAMESPACE] [-N NAME] [--count COUNT]

Generates a uuid using the selected uuid function.

options:
  -h, --help            show this help message and exit
  -u, --uuid {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}
                        The function to use to generate the uuid. By default
                        uuid4 function is used. (default: uuid4)
  -n, --namespace NAMESPACE
                        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. (default: None)
  -N, --name NAME       The name used as part of generating the uuid. Only
                        required for uuid3/uuid5 functions. (default: None)
  --count COUNT         Generate more uuids in loop. (default: 1)

(I have an idea to add colour to those in future.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. I thought that the suggestion was separate, my bad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With ArgumentDefaultsHelpFormatter it would look the following:

It seems that the typesetting of the help text mixed – Do we start the help text with a capital letter? Do we use a period at the end (this looks strange with the (default: 1) generated by ArgumentDefaultsHelpFormatter)?

> python -m uuid -h
usage: python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n NAMESPACE] [-N NAME] [-C NUM]

Generates a uuid using the selected uuid function.

options:
  -h, --help            show this help message and exit
  -u, --uuid {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}
                        The function to use to generate the uuid. (default: uuid4)
  -n, --namespace NAMESPACE
                        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. (default: None)
  -N, --name NAME       The name used as part of generating the uuid. Only required for uuid3/uuid5 functions. (default: None)
  -C, --count NUM       Generate NUM fresh UUIDs. (default: 1)

Copy link
Contributor Author

@simon04 simon04 Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding ArgumentDefaultsHelpFormatter, going for lowercase first letter, removing period at the end, adding metavar="NS" for namespace yields a more compact and consistent help output:

> python -m uuid -h
usage: python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n NS] [-N NAME] [-C NUM]

Generate a UUID using the selected UUID function.

options:
  -h, --help            show this help message and exit
  -u, --uuid {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}
                        function to generate the UUID (default: uuid4)
  -n, --namespace NS    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)
                        (default: None)
  -N, --name NAME       name used as part of generating the UUID (only required for uuid3/uuid5) (default: None)
  -C, --count NUM       generate NUM fresh UUIDs (default: 1)

I'm still not happy with the verbose --namespace help, though:

  -n, --namespace NS    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)
                        (default: None)

Is the following any better?

  -n, --namespace NS    a UUID, or @dns, @url, @oid, @x500, or any '@ns' where 'ns' is a well-known predefined UUID addressed by namespace name (only required for uuid3/uuid5)
                        (default: None)

Or specify the few choices explicitly?

  -n, --namespace {any UUID,@dns,@url,@oid,@x500}
                        a UUID, or a well-known predefined UUID addressed by namespace name (only required for uuid3/uuid5) (default: None)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding ArgumentDefaultsHelpFormatter, going for lowercase first letter, removing period at the end, adding metavar="NS" for namespace yields a more compact and consistent help output:

👍

I'm still not happy with the very verbose "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)", though. Is "a UUID, or @dns, @url, @oid, @x500, or any '@ns' where 'ns' is a well-known predefined UUID addressed by namespace name (only required for uuid3/uuid5)" any better?

It's a bit better, and I don't have a much better suggestion :)

I think we could also prefix and shorten the "(only required for uuid3/uuid5)" bits, so if you're not using uuid3/5 you can immediately skip it and save some mental effort:

  -n, --namespace NS    uuid3/uuid5 only: [etc]
                        (default: None)
  -N, --name NAME       uuid3/uuid5 only: name used as part of generating the UUID (default: None)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For --namespace, maybe specify the few choices explicitly?

  -n, --namespace {any UUID,@dns,@url,@oid,@x500}
                        uuid3/uuid5 only: a UUID, or a well-known predefined UUID addressed by namespace name (default: None)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your ideas. Here's the concise result:

> python -m uuid -h
usage: python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n {any UUID,@dns,@url,@oid,@x500}] [-N NAME] [-C NUM]

Generate a UUID using the selected UUID function.

options:
  -h, --help            show this help message and exit
  -u, --uuid {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}
                        function to generate the UUID (default: uuid4)
  -n, --namespace {any UUID,@dns,@url,@oid,@x500}
                        uuid3/uuid5 only: a UUID, or a well-known predefined UUID addressed by namespace name) (default: None)
  -N, --name NAME       uuid3/uuid5 only: name used as part of generating the UUID (default: None)
  -C, --count NUM       generate NUM fresh UUIDs (default: 1)


args = parser.parse_args()
uuid_func = uuid_funcs[args.uuid]
Expand All @@ -963,9 +965,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().
Expand Down
Loading