Skip to content
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

gh-68583: webbrowser: replace getopt with argparse, add long options #117047

Merged
merged 17 commits into from Apr 13, 2024

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Mar 19, 2024

Replace getopt with argparse in webbrowser's CLI.

Also add long options for -n and -t: --new-window and --new-tab.

Add tests for the CLI (inspired by https://pythontest.com/testing-argparse-apps/), and for an error case of new().

Before

./python.exe -m webbrowser --help
Usage: /Users/hugo/github/python/cpython/main/Lib/webbrowser.py [-n | -t | -h] url
    -n: open new window
    -t: open new tab
    -h, --help: show help

After

./python.exe -m webbrowser --help
usage: webbrowser.py [-h] [-n | -t] url

Open URL in a web browser.

positional arguments:
  url               URL to open

options:
  -h, --help        show this help message and exit
  -n, --new-window  open new window
  -t, --new-tab     open new tab

Plus some cleanup:

  • Fix whitespace
  • Fix/use f-strings
  • Remove redundant parentheses
  • Use triple backticks for docstrings
  • Use new-style class

📚 Documentation preview 📚: https://cpython-previews--117047.org.readthedocs.build/

Lib/webbrowser.py Outdated Show resolved Hide resolved
Copy link
Member

@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

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

I did not look at the new CliTest class. I have not use argparse but have read doc and examples and it looks good, and much nicer than existing. One suggestion.

Lib/webbrowser.py Show resolved Hide resolved
Lib/webbrowser.py Outdated Show resolved Hide resolved
@bedevere-app
Copy link

bedevere-app bot commented Apr 10, 2024

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.


url = args[0]
open(url, new_win)
def parse_args(arg_list: list[str] | None):
Copy link
Member

Choose a reason for hiding this comment

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

What's our current policy on stdlib type hints?

Copy link
Member Author

Choose a reason for hiding this comment

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

python/devguide#1304 says in general don't add, but there are some specific exceptions, but that the policy is undocumented. I think these might be okay because they're "simple" and internal, but I'm also happy to remove if you'd prefer?

"https://example.com -n -t",
"https://example.com --new-window --new-tab",
"https://example.com -n --new-tab",
"https://example.com --new-window -t",
Copy link
Member

Choose a reason for hiding this comment

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

argparse allows argument shortening and handles ambiguous shortenings as one might hope, but it might be nice to confirm that --new fails properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice, I didn't know about that feature :) I've added a test case.

@hugovk hugovk merged commit 56ed979 into python:main Apr 13, 2024
33 checks passed
@hugovk hugovk deleted the webbrowser-argparse branch April 13, 2024 14:56
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
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.

None yet

3 participants