Argparse with required subparser

You need to give subparsers a dest. parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest=”cmd”) subparsers.required = True Now: 1909:~/mypy$ argdev/python3 stack23349349.py usage: stack23349349.py [-h] {foo} … stack23349349.py: error: the following arguments are required: cmd In order to issue this ‘missing arguments’ error message, the code needs to give that argument a name. For a positional argument … Read more

Python Argparse conditionally required arguments

I’ve been searching for a simple answer to this kind of question for some time. All you need to do is check if ‘–argument’ is in sys.argv, so basically for your code sample you could just do: import argparse import sys if __name__ == ‘__main__’: p = argparse.ArgumentParser(description=’…’) p.add_argument(‘–argument’, required=False) p.add_argument(‘-a’, required=’–argument’ in sys.argv) #only … Read more

Can’t get argparse to read quoted string with dashes in it?

Updated answer: You can put an equals sign when you call it: python Application.py -env=”-env” Original answer: I too have had troubles doing what you are trying to do, but there is a workaround build into argparse, which is the parse_known_args method. This will let all arguments that you haven’t defined pass through the parser … Read more

Python argparse mutual exclusive group

add_mutually_exclusive_group doesn’t make an entire group mutually exclusive. It makes options within the group mutually exclusive. What you’re looking for is subcommands. Instead of prog [ -a xxxx | [-b yyy -c zzz]], you’d have: prog command 1 -a: … command 2 -b: … -c: … To invoke with the first set of arguments: prog … Read more

Python argparse: default value or specified value

import argparse parser = argparse.ArgumentParser() parser.add_argument(‘–example’, nargs=”?”, const=1, type=int) args = parser.parse_args() print(args) % test.py Namespace(example=None) % test.py –example Namespace(example=1) % test.py –example 2 Namespace(example=2) nargs=”?” means 0-or-1 arguments const=1 sets the default when there are 0 arguments type=int converts the argument to int If you want test.py to set example to 1 even if … Read more

Why use argparse rather than optparse?

As of python 2.7, optparse is deprecated, and will hopefully go away in the future. argparse is better for all the reasons listed on its original page (https://code.google.com/archive/p/argparse/): handling positional arguments supporting sub-commands allowing alternative option prefixes like + and / handling zero-or-more and one-or-more style arguments producing more informative usage messages providing a much … Read more

How do you write tests for the argparse portion of a python module?

You should refactor your code and move the parsing to a function: def parse_args(args): parser = argparse.ArgumentParser(…) parser.add_argument… # …Create your parser as you like… return parser.parse_args(args) Then in your main function you should just call it with: parser = parse_args(sys.argv[1:]) (where the first element of sys.argv that represents the script name is removed to … Read more

Python argparse – Add argument to multiple subparsers

This can be achieved by defining a parent parser containing the common option(s): import argparse parent_parser = argparse.ArgumentParser(description=”The parent parser”) parent_parser.add_argument(“-p”, type=int, required=True, help=”set db parameter”) subparsers = parent_parser.add_subparsers(title=”actions”) parser_create = subparsers.add_parser(“create”, parents=[parent_parser], add_help=False, description=”The create parser”, help=”create the orbix environment”) parser_create.add_argument(“–name”, help=”name of the environment”) parser_update = subparsers.add_parser(“update”, parents=[parent_parser], add_help=False, description=”The update parser”, help=”update … Read more

Argparse: Required argument ‘y’ if ‘x’ is present

No, there isn’t any option in argparse to make mutually inclusive sets of options. The simplest way to deal with this would be: if args.prox and (args.lport is None or args.rport is None): parser.error(“–prox requires –lport and –rport.”) Actually there’s already an open PR with an enhancement proposal : https://github.com/python/cpython/issues/55797