diff --git a/plumbum/cli/application.py b/plumbum/cli/application.py index f36b30c4e..847486234 100644 --- a/plumbum/cli/application.py +++ b/plumbum/cli/application.py @@ -9,7 +9,7 @@ from plumbum import colors, local from plumbum.cli.i18n import get_translation_for -from plumbum.lib import getdoc, six +from plumbum.lib import getdoc, six, get_main_module_frame from .switches import ( CountOf, @@ -78,6 +78,7 @@ def __repr__(self): # CLI Application base class # =================================================================================================== +main_module_ending_rx = re.compile("\.__main__$") class Application(object): """The base class for CLI applications; your "entry point" class should derive from it, @@ -176,7 +177,11 @@ def __init__(self, executable): # Filter colors if self.PROGNAME is None: - self.PROGNAME = os.path.basename(executable) + spec = get_main_module_frame().f_globals.get("__spec__", None) + if spec: + self.PROGNAME = " ".join(("python -m", main_module_ending_rx.sub("", spec.name))) + else: + self.PROGNAME = os.path.basename(executable) elif isinstance(self.PROGNAME, colors._style): self.PROGNAME = self.PROGNAME | os.path.basename(executable) elif colors.filter(self.PROGNAME) == "": diff --git a/plumbum/lib.py b/plumbum/lib.py index 7ae905e7c..dc686b5c9 100644 --- a/plumbum/lib.py +++ b/plumbum/lib.py @@ -165,3 +165,12 @@ def read_fd_decode_safely(fd, size=4096): if i == 3: raise data += os.read(fd.fileno(), 1) + +def get_main_module_frame(): + """ + Gets the frame of the __main__ module (the one which is called with command line) of an app. + """ + fr = sys._getframe(0) + while fr and fr.f_globals["__name__"] != "__main__": + fr = fr.f_back + return fr