ArgumentParser
instances have a prog
attribute which I think is what you want.
import argparseparser = argparse.ArgumentParser()print('parser.prog: {}'.format(parser.prog))
I discovered this by reading the module's source code in Lib/argparse.py
—specifically looking at the class ArgumentParser
definition. Since the attribute's name doesn't start with an underscore character, I assume it's public.
Update
I see that, nowadays at least, that the prog
attribute of ArgumentParser
instance is (or has been since this question was asked) documented in both Python 2's documentation and Python 3's documentation.
So, yes, it's definitely public, and in both versions, if it is not supplied as a keyword argument when creating the ArgumentParser
, it defaults to prog = _os.path.basename(_sys.argv[0])
(where _os
and _sys
are private argparse
module attributes that correspond to their non-underscore-prefixed counterparts. Note that because of the use of os.basename()
, this will only be the script's filename, not the complete path to it that may (it's OS dependent) have been in sys.argv[0]
.