Skip to content

Add FlagAction to argparse #52784

Description

@ericvsmith
BPO 8538
Nosy @vstinner, @ericvsmith, @merwok, @berkerpeksag, @matrixise, @wm75, @remilapeyre, @kenahoo
PRs
  • bpo-8538: Add support for boolean actions to argparse #11478
  • bpo-8538: Add support for boolean actions to argparse #11478
  • bpo-8538: Add support for boolean actions to argparse #11478
  • Files
  • argparse_bool.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-09-13.10:19:40.812>
    created_at = <Date 2010-04-26.17:38:05.465>
    labels = ['easy', 'type-feature', 'library', '3.9']
    title = 'Add FlagAction to argparse'
    updated_at = <Date 2019-09-13.10:19:40.811>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2019-09-13.10:19:40.811>
    actor = 'matrixise'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-09-13.10:19:40.812>
    closer = 'matrixise'
    components = ['Library (Lib)']
    creation = <Date 2010-04-26.17:38:05.465>
    creator = 'eric.smith'
    dependencies = []
    files = ['17100']
    hgrepos = []
    issue_num = 8538
    keywords = ['patch', 'patch', 'patch', 'easy']
    message_count = 19.0
    messages = ['104259', '105032', '105620', '107758', '110610', '113479', '121530', '166165', '270269', '332621', '332625', '332627', '332628', '333022', '333071', '333325', '352259', '352277', '352278']
    nosy_count = 11.0
    nosy_names = ['bethard', 'vstinner', 'eric.smith', 'eric.araujo', 'Jeremiah.Jordan', 'berker.peksag', 'paul.j3', 'matrixise', 'wolma', 'remi.lapeyre', 'kenahoo']
    pr_nums = ['11478', '11478', '11478']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue8538'
    versions = ['Python 3.9']

    Activity

    1. ericvsmith commented on Apr 26, 2010

      @ericvsmith
      MemberAuthor

      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.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Apr 26, 2010
    3. merwok commented on May 5, 2010

      @merwok
      Member

      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.

    4. YanivAknin commented on May 13, 2010

      YanivAkninmannequin
      Mannequin

      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.

    5. merwok commented on Jun 13, 2010

      @merwok
      Member

      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.

    6. merwok commented on Jul 17, 2010

      @merwok
      Member

      Some useful tips to make patches: http://www.python.org/dev/patches/

    7. JeremiahJordan commented on Aug 9, 2010

      JeremiahJordanmannequin
      Mannequin

      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.

    8. merwok commented on Nov 19, 2010

      @merwok
      Member

      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?

    9. changed the title [-]Add ConfigureAction to argparse[/-] [+]Add FlagAction to argparse[/+] on Nov 19, 2010
    10. bethard commented on Jul 22, 2012

      bethardmannequin
      Mannequin

      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.)

    11. vstinner commented on Jul 12, 2016

      @vstinner
      Member

      I'm sorry but there is no activity since 4 years, so I guess that the feature is not common enough to require a builtin support in argparse.

    12. kenahoo commented on Dec 27, 2018

      kenahoomannequin
      Mannequin

      @vstinner - I don't think that conclusion is correct, here is a very highly-upvoted answer on SO that indicates a lot of people are still looking for this:

      https://stackoverflow.com/a/15008806/169947

      I myself asked a related (more focused?) question where I was directed here:

      https://stackoverflow.com/q/53937481/169947

      I'm guessing the right thing to do now would be refocus the merge request in a new ticket - is this still the right tracker?

    13. 2 remaining items

    14. kenahoo commented on Dec 28, 2018

      kenahoomannequin
      Mannequin

      Thanks, Paul and Eric, for your very quick replies.

      You're quite correct, the original question in https://stackoverflow.com/a/15008806/169947 is indeed hoping for --foo TRUE and --foo False etc. to work. Personally I don't like that as much as the GNU-style --foo and --no-foo technique, because when you let people type TRUE or True or T or 1, etc., it gets a bit confusing about exactly what is accepted as a true value, what's false, is a zero interpreted as 0 or "0", whether a failure to parse the value as True or False will be reported as an error or not, and so on. The user typically can't really know these answers without reading the actual code, or running it to see what generates an error (or triggers whatever behavior they're looking for), which is certainly not ideal.

      By contrast, with --foo and --no-foo, the options are strict and clear. And supplying an argument will trigger a parse failure.

      For @mgilson's proposal, I think the main thing I find unsatisfactory (besides the fact that it takes 3 lines to define, and I'll have to come back to that SO answer every time to make sure I get them right...) is that the --help output can't be made clear. With the following specification:

          parser.add_argument('--foo', dest='foo', help="Do foo", action='store_true')
          parser.add_argument('--no-foo', dest='foo', help="Don't foo", action='store_false')
          parser.set_defaults(foo=True)

      we get the following --help text (when using ArgumentDefaultsHelpFormatter):

      --foo       Do foo (default: True)
      --no-foo    Don't foo (default: True)
      

      and that last line seems to be a contradiction, or at least very confusing. The only alternative I see is to turn off ArgumentDefaultsHelpFormatter, but I think the defaults are generally helpful information for the user.

      To fix that --help issue seems to require quite a bit more complicated registration of arguments, so it's probably not going to happen in most people's scripts.

      I should be clear: I haven't vetted the argparse_bool.py proposal in detail either, so I'm not specifically asking for it to be adopted. Just hoping for a nice resolution to the --foo --no-foo issue that codifies best-practices in a way that makes it trivial to get nice behavior in scripts.

      As for documentation - I had poked around in the code a bit and seen the register method, but I thought since it wasn't documented, I'd better not use it. Is it for public consumption? If so, I'd say it's better documented than undocumented, even if it provides more info than most people need. My guess is that enough people are probably using it to make it impossible to eliminate, which is a good test for whether something should be documented too.

    15. vstinner commented on Jan 4, 2019

      @vstinner
      Member

      I reopen the issue since it seems like there are people requesting the feature. (I also removed myself from the issue, I'm not interested to implement it.)

    16. remilapeyre commented on Jan 5, 2019

      remilapeyremannequin
      Mannequin

      I also removed myself from the issue, I'm not interested to implement it.

      I would like to try and implement the change. I will open a PR shortly.

    17. remilapeyre commented on Jan 9, 2019

      remilapeyremannequin
      Mannequin

      I made a first proposal for this feature in PR 11478, I had to adapt argparse.Action to customize how the action is represented in the usage string by adding a format_usage() method that would default to the current behavior if not overridden.

      Other methods to do this may be better but I did not find them.

    18. matrixise commented on Sep 13, 2019

      @matrixise
      Member

      I am also interested by this feature.

    19. matrixise commented on Sep 13, 2019

      @matrixise
      Member

      New changeset 6a517c6 by Stéphane Wirtel (Rémi Lapeyre) in branch 'master':
      bpo-8538: Add support for boolean actions to argparse (GH-11478)
      6a517c6

    20. matrixise commented on Sep 13, 2019

      @matrixise
      Member

      I have merged the PR 11478 and this feature will be included into 3.9.

      Thank you for your proposal of Remi and thank you to Eric for the idea of this feature.

    21. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.9 (EOL)end of lifeeasystdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions