Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse.add_argument action parameter should allow value extend #67567

Closed
themulhern mannequin opened this issue Feb 2, 2015 · 5 comments
Closed

argparse.add_argument action parameter should allow value extend #67567

themulhern mannequin opened this issue Feb 2, 2015 · 5 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@themulhern
Copy link
Mannequin

themulhern mannequin commented Feb 2, 2015

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']

    @themulhern
    Copy link
    Mannequin Author

    themulhern mannequin commented Feb 2, 2015

    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.

    @themulhern themulhern mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 2, 2015
    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Mar 22, 2015

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

    @wm75
    Copy link
    Mannequin

    wm75 mannequin commented Mar 22, 2015

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

    @matrixise matrixise added the 3.8 only security fixes label May 14, 2019
    @gvanrossum
    Copy link
    Member

    I've felt the need for this myself.

    @miss-islington
    Copy link
    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

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants