I am using argparse to parse command line arguments. While going through the documentation for argparse I could only see a provision to use a different program name.
I want to be able to use the default program name without having to import sys. There is nothing in argparse, as far as I can see, that will return the program name.
import argparse parser = argparse.ArgumentParser() args = parser.parse_args() print(dir(args)) And here's the output:
['__class__', '__contains__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_args', '_get_kwargs']
Is there any other way of retrieving the program name without having to import the sys module?
Later, calling parse_args() will return an object with two attributes, integers and accumulate . The integers attribute will be a list of one or more ints, and the accumulate attribute will be either the sum() function, if --sum was specified at the command line, or the max() function if it was not.
ArgumentParser() initializes the parser so that you can start to add custom arguments. To add your arguments, use parser. add_argument() . Some important parameters to note for this method are name , type , and required .
The argparse module makes it easy to write user-friendly command-line interfaces. It parses the defined arguments from the sys. argv . The argparse module also automatically generates help and usage messages, and issues errors when users give the program invalid arguments.
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].
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:
_ or __ of other namespaces, they are used internally.This was fun, but just stick to import sys until argparse releases an api to access sys.argv[0].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With