argparse: identify which subparser was used [duplicate]

A simpler solution is to add dest to the add_subparsers call. This is buried a bit further down in the documentation: […] If it is necessary to check the name of the subparser that was invoked, the dest keyword argument to the add_subparsers() call will work In your example replace: subparsers = parser.add_subparsers(help=’commands’) with: subparsers … Read more

In Python, using argparse, allow only positive integers

This should be possible utilizing type. You’ll still need to define an actual method that decides this for you: def check_positive(value): ivalue = int(value) if ivalue <= 0: raise argparse.ArgumentTypeError(“%s is an invalid positive int value” % value) return ivalue parser = argparse.ArgumentParser(…) parser.add_argument(‘foo’, type=check_positive) This is basically just an adapted example from the perfect_square … Read more

Having options in argparse with a dash

As indicated in the argparse docs: For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial — string. Any internal – characters will be converted to _ characters to make sure the … 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

Require either of two arguments using argparse

I think you are searching for something like mutual exclusion (at least for the second part of your question). This way, only foo or bar will be accepted, not both. import argparse parser = argparse.ArgumentParser() group = parser.add_mutually_exclusive_group(required=True) group.add_argument(‘–foo’,action=…..) group.add_argument(‘–bar’,action=…..) args = parser.parse_args() BTW, just found another question referring to the same kind of issue.

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

Python argparse ignore unrecognised arguments

Replace args = parser.parse_args() with args, unknown = parser.parse_known_args() For example, import argparse parser = argparse.ArgumentParser() parser.add_argument(‘–foo’) args, unknown = parser.parse_known_args([‘–foo’, ‘BAR’, ‘spam’]) print(args) # Namespace(foo=’BAR’) print(unknown) # [‘spam’]

Display help message with Python argparse when script is called without any arguments

This answer comes from Steven Bethard on Google groups. I’m reposting it here to make it easier for people without a Google account to access. You can override the default behavior of the error method: import argparse import sys class MyParser(argparse.ArgumentParser): def error(self, message): sys.stderr.write(‘error: %s\n’ % message) self.print_help() sys.exit(2) parser = MyParser() parser.add_argument(‘foo’, nargs=”+”) … 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