Skip to content

Commit 2a68a0e

Browse files
committed
Trap SystemExit when calling argparse.parse() to on argparse commands.
1 parent 0a1c41c commit 2a68a0e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

cmd2/cmd2.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,12 @@ def arg_decorator(func: Callable):
251251
@functools.wraps(func)
252252
def cmd_wrapper(instance, cmdline):
253253
lexed_arglist = parse_quoted_string(cmdline)
254-
args, unknown = argparser.parse_known_args(lexed_arglist)
255-
return func(instance, args, unknown)
254+
try:
255+
args, unknown = argparser.parse_known_args(lexed_arglist)
256+
except SystemExit:
257+
return
258+
else:
259+
return func(instance, args, unknown)
256260

257261
# argparser defaults the program name to sys.argv[0]
258262
# we want it to be the name of our command
@@ -288,8 +292,12 @@ def arg_decorator(func: Callable):
288292
@functools.wraps(func)
289293
def cmd_wrapper(instance, cmdline):
290294
lexed_arglist = parse_quoted_string(cmdline)
291-
args = argparser.parse_args(lexed_arglist)
292-
return func(instance, args)
295+
try:
296+
args = argparser.parse_args(lexed_arglist)
297+
except SystemExit:
298+
return
299+
else:
300+
return func(instance, args)
293301

294302
# argparser defaults the program name to sys.argv[0]
295303
# we want it to be the name of our command

0 commit comments

Comments
 (0)