Skip to content

argparse.add_argument action parameter should allow value extend #67567

Closed

Description

@themulhern
mannequin
BPO 23378
Nosy @gvanrossum, @berkerpeksag, @wm75, @miss-islington
PRs
  • bpo-23378: Add an extend action to argparse #13305
  • 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-05-21.17:53:34.589>
    created_at = <Date 2015-02-02.14:23:10.471>
    labels = ['3.8', 'type-feature', 'library']
    title = 'argparse.add_argument action parameter should allow value extend'
    updated_at = <Date 2019-05-21.17:53:34.588>
    user = 'https://bugs.python.org/themulhern'

    bugs.python.org fields:

    activity = <Date 2019-05-21.17:53:34.588>
    actor = 'gvanrossum'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-05-21.17:53:34.589>
    closer = 'gvanrossum'
    components = ['Library (Lib)']
    creation = <Date 2015-02-02.14:23:10.471>
    creator = 'the.mulhern'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 23378
    keywords = ['patch']
    message_count = 5.0
    messages = ['235260', '238936', '238942', '342952', '343076']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'berker.peksag', 'paul.j3', 'wolma', 'the.mulhern', 'miss-islington']
    pr_nums = ['13305']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue23378'
    versions = ['Python 3.8']

    Activity

    1. themulhern commented on Feb 2, 2015

      themulhernmannequin
      MannequinAuthor

      As well as the append action it would be convenient for there to be an extend action.

      This is kind of useful to allow something like:

      parser.add_argument("--foo", action="extend", nargs="+", type=str)

      given

      parser.parse_args("--foo f1 --foo f2 f3 f4".split())

      to result in

      ["f1", "f2", "f3", "f4"].

      The action "append" results in

      [["f1"], ["f2", "f3", "f4"]]

      And action store in

      ["f2", "f3", "f4"].

      It is easy to write a custom action, but it feels like a fairly common requirement.

      Probably it would make sense to extend the default, similarly to how append behaves.

    2. added
      stdlib
      type-feature
      on Feb 2, 2015
    3. paulj3 commented on Mar 22, 2015

      paulj3mannequin
      Mannequin

      My opinion is that this is an unnecessary addition.

      Reasons:

      • it is easy to add as a custom action class

      • I haven't seen other requests for it

      • the append retains information about the argument strings that extend looses

      • it is easy flatten the appended attribute, e.g with a simple loop or a flattener like 'list(itertools.chain(*ll))'

    4. wm75 commented on Mar 22, 2015

      wm75mannequin
      Mannequin
      • I haven't seen other requests for it

      For the record, an Extend custom action class is one of very few such classes I have ever written for argparse for exactly the OP's usecase, i.e., it is useful for any parser that should accept the same option multiple times, while accepting multiple arguments each time.
      It *is* very simple to implement (and that's why I never asked for it as a feature), but only once you've understood custom action classes, which are not exactly for beginners.
      All in all, I'm +1 on the request.

    5. gvanrossum commented on May 20, 2019

      @gvanrossum
      Member

      I've felt the need for this myself.

    6. miss-islington commented on May 21, 2019

      @miss-islington
      Contributor

      New changeset aa32a7e by Miss Islington (bot) (Batuhan Taşkaya) in branch 'master':
      bpo-23378: Add an extend action to argparse (GH-13305)
      aa32a7e

    7. 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.8 (EOL)stdlibtype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions