From c863be2be9f1a3becc5ea1c7ab3a833e9885f3f8 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Tue, 14 Jan 2020 15:01:00 -0800 Subject: [PATCH] expose prog name used for argparse parser (#293) --- colcon_core/command.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/colcon_core/command.py b/colcon_core/command.py index 0236b7f4..07c20545 100644 --- a/colcon_core/command.py +++ b/colcon_core/command.py @@ -206,12 +206,8 @@ def _parse_optional(self, arg_string): return result # top level parser - prog = sys.argv[0] - if os.path.basename(prog) == '__main__.py': - # use the module name in case the script was invoked with python -m ... - prog = os.path.basename(os.path.dirname(prog)) parser = CustomArgumentParser( - prog=prog, + prog=get_prog_name(), formatter_class=CustomFormatter, epilog=get_environment_variables_epilog( environment_variables_group_name)) @@ -224,6 +220,15 @@ def _parse_optional(self, arg_string): return parser +def get_prog_name(): + """Get the prog name used for the argparse parser.""" + prog = sys.argv[0] + if os.path.basename(prog) == '__main__.py': + # use the module name in case the script was invoked with python -m ... + prog = os.path.basename(os.path.dirname(prog)) + return prog + + class CustomFormatter(argparse.RawDescriptionHelpFormatter): """A custom formatter to maintain newlines."""