Argparse: Required arguments listed under “optional arguments”?

I use the following simple code to parse some arguments; note that one of them is required. Unfortunately, when the user runs the script without providing the argument, the displayed usage/help text does not indicate that there is a non-optional argument, which I find very confusing. How can I get python to indicate that an … Read more

Argparse: Way to include default values in ‘–help’?

Suppose I have the following argparse snippet: diags.cmdln_parser.add_argument( ‘–scan-time’, action = ‘store’, nargs=”?”, type = int, default = 5, help = “Wait SCAN-TIME seconds between status checks.”) Currently, –help returns: usage: connection_check.py [-h] [–version] [–scan-time [SCAN_TIME]] Test the reliability/uptime of a connection. optional arguments: -h, –help show this help message and exit –version show program’s … Read more

How to insert newlines on argparse help text?

I’m using argparse in Python 2.7 for parsing input options. One of my options is a multiple choice. I want to make a list in its help text, e.g. from argparse import ArgumentParser parser = ArgumentParser(description=’test’) parser.add_argument(‘-g’, choices=[‘a’, ‘b’, ‘g’, ‘d’, ‘e’], default=”a”, help=”Some option, where\n” ” a = alpha\n” ” b = beta\n” ” … Read more

How can I pass a list as a command-line argument with argparse?

I am trying to pass a list as an argument to a command line program. Is there an argparse option to pass a list as option? parser.add_argument(‘-l’, ‘–list’, type=list, action=’store’, dest=”list”, help='<Required> Set flag’, required=True) Script is called like below python test.py -l “265340 268738 270774 270817” 12 s 12 SHORT ANSWER Use the nargs … Read more

Parsing boolean values with argparse

I would like to use argparse to parse boolean command-line arguments written as “–foo True” or “–foo False”. For example: my_program –my_boolean_flag False However, the following test code does not do what I would like: import argparse parser = argparse.ArgumentParser(description=”My parser”) parser.add_argument(“–my_bool”, type=bool) cmd_line = [“–my_bool”, “False”] parsed_args = parser.parse(cmd_line) Sadly, parsed_args.my_bool evaluates to True. … Read more