Skip to content

Commit e092506

Browse files
refactor: remove hack for CLI required commands for Py3.6
1 parent c316f2f commit e092506

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

deepl/__main__.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,9 @@ def get_parser(prog_name):
287287
"fallback",
288288
)
289289

290-
# Note: add_subparsers param 'required' is not available in py36
291-
subparsers = parser.add_subparsers(metavar="command", dest="command")
290+
subparsers = parser.add_subparsers(
291+
metavar="command", dest="command", required=True
292+
)
292293

293294
def add_common_arguments(subparser: argparse.ArgumentParser):
294295
"""Adds arguments shared between text and document commands to the
@@ -492,9 +493,8 @@ def add_common_arguments(subparser: argparse.ArgumentParser):
492493
description="manage glossaries using subcommands",
493494
)
494495

495-
# Note: add_subparsers param 'required' is not available in py36
496496
glossary_subparsers = parser_glossary.add_subparsers(
497-
metavar="subcommand", dest="subcommand"
497+
metavar="subcommand", dest="subcommand", required=True
498498
)
499499
parser_glossary_create = glossary_subparsers.add_parser(
500500
"create",
@@ -600,12 +600,6 @@ def main(args=None, prog_name=None):
600600
parser, parser_glossary = get_parser(prog_name)
601601
args = parser.parse_args(args)
602602

603-
if args.command is None:
604-
# Support for Python 3.6 - subcommands cannot be required
605-
sys.stderr.write("Error: command is required\n")
606-
parser.print_help(sys.stderr)
607-
sys.exit(1)
608-
609603
logger = logging.getLogger("deepl")
610604
if args.verbose == 1:
611605
logger.setLevel(logging.INFO)
@@ -653,13 +647,6 @@ def main(args=None, prog_name=None):
653647
if len(args.text) == 1 and args.text[0] == "-":
654648
args.text = [sys.stdin.read()]
655649

656-
elif args.command == "glossary":
657-
if args.subcommand is None:
658-
# Support for Python 3.6 - subcommands cannot be required
659-
sys.stderr.write("Error: glossary subcommand is required\n")
660-
parser_glossary.print_help(sys.stderr)
661-
sys.exit(1)
662-
663650
# Remove global args so they are not unrecognised in action functions
664651
del (
665652
args.verbose,

tests/test_cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def test_env_auth_no_keyring(mock, runner):
103103

104104
def test_no_command(runner):
105105
result = runner.invoke(main_function, "")
106-
assert result.exit_code == 1, f"exit: {result.exit_code}\n {result.output}"
107-
assert "command is required" in result.output
106+
assert result.exit_code == 2, f"exit: {result.exit_code}\n {result.output}"
107+
assert "required: command" in result.output
108108

109109

110110
def test_usage(runner):
@@ -284,8 +284,8 @@ def test_invalid_document(runner, tmpdir):
284284

285285
def test_glossary_no_subcommand(runner):
286286
result = runner.invoke(main_function, "glossary")
287-
assert result.exit_code == 1, f"exit: {result.exit_code}\n {result.output}"
288-
assert "subcommand is required" in result.output
287+
assert result.exit_code == 2, f"exit: {result.exit_code}\n {result.output}"
288+
assert "required: subcommand" in result.output
289289

290290

291291
def test_glossary_create(

0 commit comments

Comments
 (0)