Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argparse --toggle --no-toggle flag

Is there a straightforward way to use --toggle and --no-toggle flags with Python's argparse?

Right now I'm using something similar to the following:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument('--toggle',
                    action='store_true',
                    dest='toggle')
parser.add_argument('--no-toggle',
                    action='store_true',
                    default=True,
                    dest='notoggle')

options = parser.parse_args([])

I'm just manually parsing out the possibilities in a long if chain, but it would be nice if there was a way to tidy this up and have the state immediately stored in one destination by the parser, e.g. options.toggle. Is this feasible and if so, how?

A somewhat related answer is Python argparse toggle flags however I'm interested in using --no- as the longopts store_false toggle prefix (similar to the - shortopts toggle prefix outlined in the aforementioned link).

like image 482
Six Avatar asked Jan 12 '16 04:01

Six


People also ask

How do you add an optional argument in Argparse?

Optional Arguments To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.

How do I make Argparse argument optional in Python?

Python argparse optional argument The example adds one argument having two options: a short -o and a long --ouput . These are optional arguments. The module is imported. An argument is added with add_argument .

How do you pass a Boolean in a command line argument in Python?

import argparse parser = argparse. ArgumentParser(description="Parse bool") parser. add_argument("--do-something", default=False, action="store_true", help="Flag to do something") args = parser. parse_args() if args.

What does Nargs do in Argparse?

By default, argparse will look for a single argument, shown above in the filename example. If you want your parameters to accept a list of items you can specify nargs=n for how many arguments to accept. Note, if you set nargs=1 , it will return as a list not a single value.


2 Answers

Why not do the same as the post you linked to??

import argparse

class NegateAction(argparse.Action):
    def __call__(self, parser, ns, values, option):
        setattr(ns, self.dest, option[2:4] != 'no')

ap = argparse.ArgumentParser()
ap.add_argument('--toggle', '--no-toggle', dest='toggle', action=NegateAction, nargs=0)

Then if the flag has no at the beginning it is set to False:

>>> ap.parse_args(['--toggle'])
Namespace(toggle=True)
>>> options = ap.parse_args(['--no-toggle'])
>>> options.toggle
False
like image 53
AChampion Avatar answered Oct 15 '22 08:10

AChampion


Arguments can use the same dest:

import argparse
parser = argparse.ArgumentParser()

parser.add_argument('--toggle',
                    action='store_const',
                    default = 'unknown',
                    const = 'yes',  # const=True also works
                    dest='toggle')
parser.add_argument('--no-toggle',
                    action='store_const',
                    const = 'no',   # or False
                    dest='toggle')

This produces:

In [60]: parser.parse_args([])    # none
Out[60]: Namespace(toggle='unknown')
In [61]: parser.parse_args(['--tog'])
Out[61]: Namespace(toggle='yes')
In [62]: parser.parse_args(['--no-tog'])
Out[62]: Namespace(toggle='no')
In [63]: parser.parse_args(['--no-tog','--tog'])  # more than one
Out[63]: Namespace(toggle='yes')

I used store_const rather than store_true and store_false to make the 3 alternatives clear. store_true is just store_const with const=True and default=False.

If you define a store_true and store_false pair, it is hard to distinguish between the no argument case and --no-toggle case. --no-toggle doesn't do anything significant, unless it follows a --toggle argument.

If you need to define a large set of these pairs, you can easily define a helper function, which takes a couple of the parameters, and creates the pair of arguments.

like image 43
hpaulj Avatar answered Oct 15 '22 07:10

hpaulj