Argparse: creating the required flags

I am trying to create a mandatory "-f" flag that accepts "filename.pdb" input in Argparse.

It's simple enough. The standard solution is to add the "required = True" option.

Unfortunately, after doing this, the "-f" flag still appears below the optional arguments in the help list. Even more confused, the "-f" flag appears as required at the "use" 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 removed from parentheses, indicating a need. Despite this, "-f" still appears in the "optional arguments" section.

Is it possible:

A) Custom help window format to fix this

or

B) Add some code to make the "-f", "--file_name" flag appear as a positional (versus optional) argument, but still need a flag?

I read that Argparse intentionally did this to avoid positional flags, but I have to do this to serve traditional Linux users.

Thank you kind interwebbers!

+3


source to share


1 answer


This question was discussed at http://bugs.python.org/issue9694 .'argparse required arguments displayed under "optional arguments"'

This is a terminology issue that is not easy to resolve due to historical practice (in both UNIX and Python) and the lack of good alternatives.

Arguments that take a flag like '-f'

are historically called options or options. Typically, you don't use them unless you want some value to be different from the default. But "argparse" allows you to specify required=True

, so now you have "required optional". And with nargs='?'

you can have "positional elements" that are not required.

Until the Python developers come up with some alternative terminology, your best bet is to use "ArgumentGroup" with the name and description you like. By default, the parser has 2 Group Arguments, "optional arguments" and "positional arguments". He has to put an argument in one way or another. You can create others and fill them in as you like.

see http://bugs.python.org/issue9694#msg132327 (post from original developer argparse).

A "usage" line is one that describes exactly how the arguments are used, and whether they are required or not. Group Arguments do not affect usage or parsing. They simply define how the help strings are grouped.




For your code:

parser = argparse.ArgumentParser()
req_grp = parser.add_argument_group(title='Required Optional')
req_grp.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()

"""
usage: stack26227536.py [-h] -f FILE_NAME [-bw BIN_WIDTH] [-bn BASE_NAME]

optional arguments:
  -h, --help            show this help message and exit
  -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

Required Optional:
  -f FILE_NAME, --file_name FILE_NAME
                        enter name of .pdb file
"""

      

Compare this with the help triggered by the reset flag -f

:

usage: stack26227536.py [-h] [-bw BIN_WIDTH] [-bn BASE_NAME] file_name

positional arguments:
  file_name             enter name of .pdb file

optional arguments:
  -h, --help            show this help message and exit
  -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

      

+6


source







All Articles