How do you get the name of the program using argparse?
Of course the correct way would be:
>>> import sys
>>> print sys.argv[0]
scripts/script.py
But let's assume for a moment you have a good reason that prevents you to import sys
but allows you to import argparse
.
martineau has done a wonderful job discovering prog
, let's try it:
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> print parser.prog
script.py
As noted by hpaulj, this only has the filename and not the full path like sys.argv[0]
because the module argparse.py
is using prog = os.path.basename(sys.argv[0])
.
But argparse
must use sys
, so it needs to be accessible in argparse
namespace. Let's check it:
>>> import argparse
>>> print argparse.__dict__
{ ..., '_sys': <module 'sys' (built-in)>, ... }
Here it is! Let's try to use _sys
:
>>> import argparse
>>> print argparse._sys.argv[0]
scripts/script.py
You are using sys
! Of course, but I haven't imported it, only argparse
, that was the question!
Of course this has a number of contraindications:
- You should not use variables prefixed by
_
or__
of other namespaces, they are used internally. - You should not rely on imports of other modules, they may change.
- You should not rely on undocumented api, they may change.
tl;dr
This was fun, but just stick to import sys
until argparse
releases an api to access sys.argv[0]
.
ArgumentParser
instances have a prog
attribute which I think is what you want.
import argparse
parser = 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]
.