Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-44431: Added command line functionality to UUID module. #26751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
Prev Previous commit
implemented -h --help which prints module doc string. Added command-l…
…ine usage to doc string.
  • Loading branch information
ephenixaws committed Jun 17, 2021
commit d00cf6bb757717416661a8b4c90d6dc6cdf02d0c
32 changes: 18 additions & 14 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@
# make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

Command-Line usage:

python -m uuid [-h] | COMMAND [NAMESPACE] [NAME]

# generate a random uuid
>>> python -m uuid

# generate a uuid1
>>> python -m uuid uuid1

# generate a uuid3 or uuid5
>>> python -m uuid uuid5 NAMESPACE_DNS python.org

"""

import os
Expand Down Expand Up @@ -728,31 +742,21 @@ def uuid5(namespace, name):
NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')


if __name__ == '__main__':
"""
Enables uuid to be called from the command line
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we can move this logic into a helper method so that we just call the method here? Most libraries seem to follow this e.g Lib/timeit.py


Example usage:

python -m uuid
> 16fd2706-8baf-433b-82eb-8c7fada847da

python -m uuid uuid5 NAMESPACE_DNS python.org
> 886313e1-3b8a-5372-9b90-0c9aee199e5d

python -m uuid uuid5 {custom_uuid} mydomain.com
> e332bfe2-fed1-4e9c-ad45-db54e087b39e
"""

allowed_cmds = ['uuid1', 'uuid3', 'uuid4', 'uuid5']
allowed_cmds = ['-h','--help','uuid1', 'uuid3', 'uuid4', 'uuid5']
if len(sys.argv) == 1:
# By default, print a random uuid.
print(uuid4())
else:
cmd = sys.argv[1]
if cmd not in allowed_cmds:
raise Exception(f"Command not found: {cmd}. Allowed values are {allowed_cmds}")
raise Exception(f"Command not found: [{cmd}]. Run `python -m uuid -h` for more information.")
if cmd in ['-h', '--help']:
print(__doc__)
if cmd == 'uuid1':
print(uuid1())
if cmd in ['uuid3', 'uuid5']:
Expand Down