Skip to content

gh-148740: Allow setting namespace from command line usage of uuid module#148741

Open
sumpygump wants to merge 1 commit intopython:mainfrom
sumpygump:fix/148740-uuid-cli-usage
Open

gh-148740: Allow setting namespace from command line usage of uuid module#148741
sumpygump wants to merge 1 commit intopython:mainfrom
sumpygump:fix/148740-uuid-cli-usage

Conversation

@sumpygump
Copy link
Copy Markdown

@sumpygump sumpygump commented Apr 18, 2026

  • This applies to generating version 3 and 5 UUIDs
  • Will correctly bail when invalid UUID is supplied as namespace

Example usage with correction:

./python -m uuid --uuid uuid5 --namespace '0d6a16cc-34a7-47d8-b660-214d0ae184d2' --name some.user
a32f4562-2058-5b20-8055-5f38b28285b6

Example error if invalid UUID is supplied as --namespace argument:

./python -m uuid --uuid uuid5 --namespace 'invalid uuid' --name some.user
usage: python -m uuid [-h] [-u {uuid1,uuid3,uuid4,uuid5,uuid6,uuid7,uuid8}] [-n {any UUID,@dns,@url,@oid,@x500}] [-N NAME] [-C NUM]
python -m uuid: error: badly formed hexadecimal UUID string: 'invalid uuid'.

My solution was to update the argparse options to use metavar instead of choices for the namespace
argument. This allows to supply suggestions of options that can be supplied for the --namespace arguments instead of requiring ONLY the ones in the list.

My intent was to keep the help message consistent with how it looked before. Note that it still displays the possible values in the help text for the namespace: -n, --namespace {any UUID,@dns,@url,@oid,@x500}:

./python -m uuid --help
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)

 - This applies to generating version 3 and 5 UUIDs
 - Will correctly bail when invalid UUID is supplied as namespace
@python-cla-bot
Copy link
Copy Markdown

python-cla-bot bot commented Apr 18, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant