1

Here is the code.

def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description="infomedia"
    )
    parser.add_argument("file", help="path to file")

    parser.add_argument(
        "-i",
        "--info",
        type=str,
        default="False",
        help="get information about",
    )

    cli_args = parser.parse_args()

    worker = Worker(
        cli_args.input,
        cli_args.info,
    )

    worker._application()

When the program is running with -h / --help it shows the default values.

positional arguments:
  file                  path to file

optional arguments:
  -h, --help            show this help message and exit
  -i INFO, --info INFO  get information about (default: False)

How to avoid printing the default values? Or is there a way to define the default values of this code in a different way?

trueThari
  • 105
  • 1
  • 8
  • why are you using `formatter_class=argparse.ArgumentDefaultsHelpFormatter`? – hpaulj Apr 06 '21 at 13:44
  • @hpaulj Sorry. I thought it should always be defined. It was in a guide and I added that to my code. Is that the problem? – trueThari Apr 06 '21 at 13:56
  • yes, that specifies an alternative formatter, one that displays the defaults. That is explained in the official python reference. – hpaulj Apr 06 '21 at 14:05
  • You want the default formatter: `argparse.ArgumentParser(description="infomedia")` https://docs.python.org/3/library/argparse.html#formatter-class – hpaulj Apr 06 '21 at 14:29

2 Answers2

2

You can create new class inheriting from argparse.ArgumentDefaultsHelpFormatter and override _get_help_string method and pass your newly created class which is MyHelpFormatter in the below example as formatter_class in ArgumentParser constructor. Here is the example code which can help you:

import argparse

class MyHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
    def _get_help_string(self, action):
        return action.help

def main():
    parser = argparse.ArgumentParser(
        formatter_class=MyHelpFormatter,
        description="infomedia",
    )
    parser.add_argument("file", help="path to file")

    parser.add_argument(
        "-i",
        "--info",
        type=str,
        default="False",
        help="get information about",
    )

    cli_args = parser.parse_args()

if __name__ == "__main__":
    main()
sonulohani
  • 1,225
  • 9
  • 8
  • That's a round about way of reverting the behavior to the default `HelpFormatter`. Why not just omit that parameter? `parser = argparse.ArgumentParser(description="infomedia")`. – hpaulj Apr 06 '21 at 14:27
0

I think you want more like:

parser.add_argument("--info", help="get information", action="store_true")
JonSG
  • 10,542
  • 2
  • 25
  • 36
  • At this point, specification of --info on the command line will set parse_args().info to be boolean True. If you needed that to be the string, you could cast it with str(parse_args().info) – JonSG Apr 06 '21 at 16:38