classification
Title: Add FlagAction to argparse
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jeremiah.Jordan, Yaniv.Aknin, berker.peksag, bethard, eric.araujo, eric.smith
Priority: normal Keywords: easy

Created on 2010-04-26 17:38 by eric.smith, last changed 2012-07-22 20:08 by bethard.

Files
File name Uploaded Description Edit
argparse_bool.py eric.smith, 2010-04-26 17:38
Messages (8)
msg104259 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-04-26 17:38
From a python-dev email from Neal Becker, copied here so it won't get lost.
----------------------------
steven.bethard@gmail.com made a very nice module for me to enhance argparse called argparse_bool.py, which contains ConfigureAction.  This will allow a boolean value to be set very like the gnu configure style:

--foo
--with-foo
--without-foo
--no-foo
--foo=yes
--foo=no

I've been happily using it, and I think it would be of sufficient general interest to include it with the standard library.
msg105032 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-05-05 13:57
Are we sure we want three or four ways to spell the same thing? --foo and --no-foo seem a useful couple to me, and --with-foo/--without-foo cater to different use cases. Perhaps it would be ok to have a SwitchAction (or FlagAction) for --thing/--no-hing and one ConfigureAction for the --with[out]-* style.
msg105620 - (view) Author: Yaniv Aknin (Yaniv.Aknin) Date: 2010-05-13 01:01
I'm looking into making a patch from this for py3k, and have the following observations:

1. I agree with merwok, at the moment the monolithic ConfigureAction is a bit excessive for most uses (other than maybe emulating ./Configure...). I vote we split it to FlagAction and ConfigureAction.
2. I don't think this should be added as an argparse builtin/registered action (like action='store' or action='count'), but rather as a ready-made user-action in a separate namespace (from argparse.actions import FlagAction)

Given feedback that these two decisions seem sane, I'd be happy to produce a patch against recent py3k.
msg107758 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-06-13 22:08
I think you can go ahead and produce a patch. Don’t start splitting argparse into a package now though, this is a sensible decision IMO but has to be agreed by the maintainer; just add FlagAction and ConfigureAction to argparse for now.
msg110610 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-07-17 22:41
Some useful tips to make patches: http://www.python.org/dev/patches/
msg113479 - (view) Author: Jeremiah Jordan (Jeremiah.Jordan) Date: 2010-08-09 21:28
I think this should be updated so that nargs=0 is allowed, so that you can only do --foo/--no-foo and don't clutter up the help/interface with --foo [FOO] --no-foo=[FOO]

You can do this by adding nargs to the ConfigureAction.__init__ and passing that through to super, and then updating __call__ to check 'if value is None or value == []:' instead of the None.
msg121530 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-11-19 15:16
I think FlagAction should implement strictly boolean options, that is --foo and --no-foo, without arguments at all.

For ConfigureAction, there is a precedent (unless I’m mistaken) in configure, which permits such things:
  --without-unicode
  --with-unicode=ucs4
  --with-unicode (uses default value for arg)

I say we focus on the simple FlagAction for this bug and keep ConfigureAction for another patch.

Yaniv: Can you give us a status update?
msg166165 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2012-07-22 20:08
On the off chance that someone was waiting for feedback from me, I'll say:

(1) A simple boolean --foo/--no-foo action seems useful to me. I would probably call it BooleanOptionalAction rather than FlagAction. (Almost anything could be considered a "flag".)

(2) At the moment, argparse doesn't supply any actions, so I don't think we should make a separate namespace for them. If it starts to grow a list of such actions, we can add a separate namespace later. (And given that the name will end with "Action", I think it should be pretty clear without the namespace.)
History
Date User Action Args
2012-07-22 20:08:21bethardsetmessages: + msg166165
2011-12-12 01:30:50berker.peksagsetnosy: + berker.peksag
2010-11-19 15:16:15eric.araujosetkeywords: + easy

messages: + msg121530
title: Add ConfigureAction to argparse -> Add FlagAction to argparse
2010-08-09 21:28:40Jeremiah.Jordansetnosy: + Jeremiah.Jordan
messages: + msg113479
2010-07-17 22:41:11eric.araujosetmessages: + msg110610
2010-06-13 22:08:33eric.araujosetmessages: + msg107758
stage: needs patch
2010-05-13 01:01:20Yaniv.Akninsetmessages: + msg105620
2010-05-05 13:57:20eric.araujosetnosy: + eric.araujo
messages: + msg105032
2010-05-02 12:07:26Yaniv.Akninsetnosy: + Yaniv.Aknin
2010-04-26 18:52:52eric.smithsetpriority: normal
2010-04-26 17:38:05eric.smithcreate