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
Merged
Prev Previous commit
Next Next commit
Update tests and add default value
hugovk committed Apr 10, 2024
commit 44a520062e5db0de2c4226afbf523cefe8f0f594
23 changes: 11 additions & 12 deletions Lib/test/test_webbrowser.py
Original file line number Diff line number Diff line change
@@ -435,25 +435,24 @@ def test_environment_preferred(self):

class CliTest(unittest.TestCase):
def test_parse_args(self):
for command, url, new_window, new_tab in [
for command, url, new_win in [
# No optional arguments
("https://example.com", "https://example.com", False, False),
("https://example.com", "https://example.com", 0),
# Each optional argument
("https://example.com -n", "https://example.com", True, False),
("-n https://example.com", "https://example.com", True, False),
("https://example.com -t", "https://example.com", False, True),
("-t https://example.com", "https://example.com", False, True),
("https://example.com -n", "https://example.com", 1),
("-n https://example.com", "https://example.com", 1),
("https://example.com -t", "https://example.com", 2),
("-t https://example.com", "https://example.com", 2),
# Long form
("https://example.com --new-window", "https://example.com", True, False),
("--new-window https://example.com", "https://example.com", True, False),
("https://example.com --new-tab", "https://example.com", False, True),
("--new-tab https://example.com", "https://example.com", False, True),
("https://example.com --new-window", "https://example.com", 1),
("--new-window https://example.com", "https://example.com", 1),
("https://example.com --new-tab", "https://example.com", 2),
("--new-tab https://example.com", "https://example.com", 2),
]:
args = webbrowser.parse_args(shlex.split(command))

self.assertEqual(args.url, url)
self.assertEqual(args.new_window, new_window)
self.assertEqual(args.new_tab, new_tab)
self.assertEqual(args.new_win, new_win)

def test_parse_args_error(self):
for command in [
4 changes: 2 additions & 2 deletions Lib/webbrowser.py
Original file line number Diff line number Diff line change
@@ -684,10 +684,10 @@ def parse_args(arg_list: list[str] | None):

group = parser.add_mutually_exclusive_group()
group.add_argument("-n", "--new-window", action="store_const",
const=1, dest="new_win",
const=1, default=0, dest="new_win",
help="open new window")
group.add_argument("-t", "--new-tab", action="store_const",
const=2, dest="new_win",
const=2, default=0, dest="new_win",
help="open new tab")

args = parser.parse_args(arg_list)