Skip to content

Replace custom CLI with argparse - #83

Open
hugovk wants to merge 4 commits into
python:mainfrom
hugovk:argparse
Open

Replace custom CLI with argparse#83
hugovk wants to merge 4 commits into
python:mainfrom
hugovk:argparse

Conversation

@hugovk

@hugovk hugovk commented Aug 5, 2026

Copy link
Copy Markdown
Member

There's a lot of custom code for the CLI handling, which we can replace with argparse.

This saves us about 150 lines of code, and we get colour help, plus I've added backticks in some help messages for "code" formatting as well.

Right now, blurb has both subcommand and option versions of help and version:

blurb help
blurb -h
blurb --help

blurb version
blurb -V
blurb --version

This PR deprecates the subcommands in favour of the more ususal options.

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🌈

Comment thread src/blurb/_cli.py Outdated
)

def add_subcommand(name: str, doc: str) -> argparse.ArgumentParser:
doc = doc.strip()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Won't strip only strip the first line? I suggest using inpect.cleandoc instead.

Comment thread src/blurb/_cli.py
# Keep the legacy 'help' and 'version' subcommands working as aliases.
elif args[0] == 'help':
print(
"Warning: 'blurb help' is deprecated, use 'blurb --help' instead",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not warnings.warn(..., DeprecationWarning), that way we can suppress it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a CLI, not a library, so I don't think we need warnings here.

If you're calling blurb help or blurb version in a script and see the message, change to blurb --help or blurb --version instead of adding suppression handling.

Comment thread src/blurb/_cli.py
"Warning: 'blurb help' is deprecated, use 'blurb --help' instead",
file=sys.stderr,
)
args = [*args[1:2], '--help']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Won't this allow extra arguments after --help?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

argparse will show the help immediately with --help and extra args don't matter, we get the help anyway:

blurb --help
usage: blurb [-h] [-V] subcommand ...

Management tool for CPython Misc/NEWS and Misc/NEWS.d entries.

positional arguments:
  subcommand
    add          Add a blurb (a Misc/NEWS.d/next entry) to the current CPython repo.
    export       Removes blurb data files, for building release tarballs/installers.
    merge        Merge all blurbs together into a single Misc/NEWS file.
    populate     Creates and populates the Misc/NEWS.d directory tree.
    release      Move all new blurbs to a single blurb file for the release.

options:
  -h, --help     show this help message and exit
  -V, --version  show program's version number and exit

If blurb is run without any arguments, this is equivalent to blurb add.blurb --help abc
usage: blurb [-h] [-V] subcommand ...

Management tool for CPython Misc/NEWS and Misc/NEWS.d entries.

positional arguments:
  subcommand
    add          Add a blurb (a Misc/NEWS.d/next entry) to the current CPython repo.
    export       Removes blurb data files, for building release tarballs/installers.
    merge        Merge all blurbs together into a single Misc/NEWS file.
    populate     Creates and populates the Misc/NEWS.d directory tree.
    release      Move all new blurbs to a single blurb file for the release.

options:
  -h, --help     show this help message and exit
  -V, --version  show program's version number and exit

If blurb is run without any arguments, this is equivalent to blurb add.

Also fine for a subcommand:

blurb merge --help
usage: blurb merge [-h] [-f] [output]

Merge all blurbs together into a single Misc/NEWS file.

Optional output argument specifies where to write to.
Default is <cpython-root>/Misc/NEWS.

If overwriting, blurb merge will prompt you to make sure it's okay.
To force it to overwrite, use -f.

positional arguments:
  output        where to write the NEWS file (default: Misc/NEWS)

options:
  -h, --help    show this help message and exit
  -f, --forced  overwrite an existing file without promptingblurb merge --help abc
usage: blurb merge [-h] [-f] [output]

Merge all blurbs together into a single Misc/NEWS file.

Optional output argument specifies where to write to.
Default is <cpython-root>/Misc/NEWS.

If overwriting, blurb merge will prompt you to make sure it's okay.
To force it to overwrite, use -f.

positional arguments:
  output        where to write the NEWS file (default: Misc/NEWS)

options:
  -h, --help    show this help message and exit
  -f, --forced  overwrite an existing file without prompting

Comment thread README.md



### blurb help

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd suggest keeping at least a little note pointing to --help.

@StanFromIreland

Copy link
Copy Markdown
Member

When this lands I think it'll be time for a release, we haven't had any for quite some time (and, to be honest that "in in" is bugging me now ;-). I'm happy to take care of it then.

@hugovk hugovk left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

When this lands I think it'll be time for a release, we haven't had any for quite some time (and, to be honest that "in in" is bugging me now ;-). I'm happy to take care of it then.

Yes, I think it's time, go for it!

https://github.com/python/blurb/blob/main/CHANGELOG.md shows 2.1.0 and 2.2.0 but neither has been released yet, so they need merging as 2.1.0.

And https://github.com/python/blurb/blob/main/.github/release.yml needs to update dependabot to dependabot[bot].

Comment thread src/blurb/_cli.py
# Keep the legacy 'help' and 'version' subcommands working as aliases.
elif args[0] == 'help':
print(
"Warning: 'blurb help' is deprecated, use 'blurb --help' instead",

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a CLI, not a library, so I don't think we need warnings here.

If you're calling blurb help or blurb version in a script and see the message, change to blurb --help or blurb --version instead of adding suppression handling.

Comment thread src/blurb/_cli.py
"Warning: 'blurb help' is deprecated, use 'blurb --help' instead",
file=sys.stderr,
)
args = [*args[1:2], '--help']

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

argparse will show the help immediately with --help and extra args don't matter, we get the help anyway:

blurb --help
usage: blurb [-h] [-V] subcommand ...

Management tool for CPython Misc/NEWS and Misc/NEWS.d entries.

positional arguments:
  subcommand
    add          Add a blurb (a Misc/NEWS.d/next entry) to the current CPython repo.
    export       Removes blurb data files, for building release tarballs/installers.
    merge        Merge all blurbs together into a single Misc/NEWS file.
    populate     Creates and populates the Misc/NEWS.d directory tree.
    release      Move all new blurbs to a single blurb file for the release.

options:
  -h, --help     show this help message and exit
  -V, --version  show program's version number and exit

If blurb is run without any arguments, this is equivalent to blurb add.blurb --help abc
usage: blurb [-h] [-V] subcommand ...

Management tool for CPython Misc/NEWS and Misc/NEWS.d entries.

positional arguments:
  subcommand
    add          Add a blurb (a Misc/NEWS.d/next entry) to the current CPython repo.
    export       Removes blurb data files, for building release tarballs/installers.
    merge        Merge all blurbs together into a single Misc/NEWS file.
    populate     Creates and populates the Misc/NEWS.d directory tree.
    release      Move all new blurbs to a single blurb file for the release.

options:
  -h, --help     show this help message and exit
  -V, --version  show program's version number and exit

If blurb is run without any arguments, this is equivalent to blurb add.

Also fine for a subcommand:

blurb merge --help
usage: blurb merge [-h] [-f] [output]

Merge all blurbs together into a single Misc/NEWS file.

Optional output argument specifies where to write to.
Default is <cpython-root>/Misc/NEWS.

If overwriting, blurb merge will prompt you to make sure it's okay.
To force it to overwrite, use -f.

positional arguments:
  output        where to write the NEWS file (default: Misc/NEWS)

options:
  -h, --help    show this help message and exit
  -f, --forced  overwrite an existing file without promptingblurb merge --help abc
usage: blurb merge [-h] [-f] [output]

Merge all blurbs together into a single Misc/NEWS file.

Optional output argument specifies where to write to.
Default is <cpython-root>/Misc/NEWS.

If overwriting, blurb merge will prompt you to make sure it's okay.
To force it to overwrite, use -f.

positional arguments:
  output        where to write the NEWS file (default: Misc/NEWS)

options:
  -h, --help    show this help message and exit
  -f, --forced  overwrite an existing file without prompting

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants