I am attempting to create a required flag "-f" that accepts the input "filename.pdb" in Argparse.
This is simple enough. The standard solution is to add the option "required=True."
Unfortunately, after doing this, the "-f" flag still appears under optional arguments in the help list. Even more confusingly, the "-f" flag appears as required in the "usage" prompt in the help list.
Here is my code:
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file_name", required=True, help="enter name of .pdb file")
parser.add_argument("-bw", "--bin_width", default=.25, help="enter desired bin width in nanometers. default = .25")
parser.add_argument("-bn","--base_name", default="IDP", help="custom prefix for output file naming. default = IDP")
args = parser.parse_args()
And here is the help window that is returned by --help
usage: rgcalc.py [-h] -f FILE_NAME [-bw BIN_WIDTH] [-bn BASE_NAME]
optional arguments:
-h, --help show this help message and exit
-f FILE_NAME, --file_name FILE_NAME
enter name of .pdb file
-bw BIN_WIDTH, --bin_width BIN_WIDTH
enter desired bin width in nanometers. default = .25
-bn BASE_NAME, --base_name BASE_NAME
custom prefix for output file naming. default = IDP
As you can see in the "usage" block, "-f" has been taken out of brackets, indicating that it is required. Despite this, "-f" still appears in the "optional arguments" section.
Is it possible to:
A) Custom format the help window to fix this
or
B) Add some code to have the flag "-f", "--file_name" appear as a positional (opposed to an optional) argument, yet still require a flag?
I read that Argparse intentionally did this to avoid positional flags, but I am supposed to do it in order to cater to traditional linux users.
Thank you kind interwebbers!