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

[cli] Override argparse error code #3408

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions analyzer/tests/functional/cmdline/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def test_parse_help(self):
parse_help = [env.codechecker_cmd(), 'parse', '--help']
self.assertEqual(0, run_cmd(parse_help)[0])

def test_invalid_subcommand(self):
""" Call CodeChecker with and invalid subcommand. """

dummy_cmd = [env.codechecker_cmd(), "dummy"]
self.assertEqual(1, run_cmd(dummy_cmd)[0])

def test_checkers(self):
""" Listing available checkers. """

Expand Down
14 changes: 13 additions & 1 deletion codechecker_common/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
import sys


class ArgumentParser(argparse.ArgumentParser):
def error(self, message):
"""
By default argparse will exit with error code 2 in case of error, but
we are using this error code for different purposes. For this reason
we will override this function, so we will exit with error code 1 in
such cases too.
"""
self.print_usage(sys.stderr)
self.exit(1, f"{self.prog}: error: {message}\n")


def add_subcommand(subparsers, sub_cmd, cmd_module_path, lib_dir_path):
"""
Load the subcommand module and then add the subcommand to the available
Expand Down Expand Up @@ -106,7 +118,7 @@ def signal_handler(signum, frame):
signal.signal(signal.SIGTERM, signal_handler)

try:
parser = argparse.ArgumentParser(
parser = ArgumentParser(
prog="CodeChecker",
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""Run the CodeChecker sourcecode analyzer framework.
Expand Down