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: Provide equivalent of optparse.OptionParser.{option_groups,option_list,get_option} #67348

Closed
emcd mannequin opened this issue Jan 4, 2015 · 5 comments
Closed
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@emcd
Copy link
Mannequin

emcd mannequin commented Jan 4, 2015

BPO 23159
Nosy @mcepl
Files
  • argparse_extended.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 2018-03-06.04:55:28.417>
    created_at = <Date 2015-01-04.07:07:43.926>
    labels = ['type-feature', 'library']
    title = 'argparse: Provide equivalent of optparse.OptionParser.{option_groups,option_list,get_option}'
    updated_at = <Date 2018-03-06.04:55:28.415>
    user = 'https://bugs.python.org/emcd'

    bugs.python.org fields:

    activity = <Date 2018-03-06.04:55:28.415>
    actor = 'emcd'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-03-06.04:55:28.417>
    closer = 'emcd'
    components = ['Library (Lib)']
    creation = <Date 2015-01-04.07:07:43.926>
    creator = 'emcd'
    dependencies = []
    files = ['38629']
    hgrepos = []
    issue_num = 23159
    keywords = []
    message_count = 5.0
    messages = ['233394', '238867', '239303', '313247', '313305']
    nosy_count = 4.0
    nosy_names = ['bethard', 'mcepl', 'paul.j3', 'emcd']
    pr_nums = []
    priority = 'normal'
    resolution = 'later'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue23159'
    versions = ['Python 3.5']

    @emcd
    Copy link
    Mannequin Author

    emcd mannequin commented Jan 4, 2015

    There are several use cases for having the equivalent of the optparse.OptionParser 'get_option' method and the 'option_groups' and 'option_list' properties in argparse.ArgumentParser class.

    (1) Easy alteration of the text of the default help option. With optparse, one could write:

    >>> oparser.get_option( "-h" ).help
    'show this help message and exit'
    >>> oparser.get_option( "-h" ).help = "Show this help message and exit."
    >>> oparser.get_option( "-h" ).help
    'Show this help message and exit.'

    The equivalent facility does not appear to exist in argparse. (Issue bpo-19462, http://bugs.python.org/issue19462, is about a different use case. And, while https://docs.python.org/3/library/argparse.html#add-help suggests a workaround with add_help=False and then creating a new option with the 'help' action, it is still less convenient than altering the existing help option.)

    (2) Inspection of all the argument declarations in an ArgumentParser object after it has been populated. This is particularly useful if you want to look for the equivalent of the available options in config files and don't want to have write explicit code which separately enumerates the available config file keys and how to handle them. With an OptionParser, one could use the 'option_groups' attribute to find all option groups (to correspond to config file sections) and the 'option_list' attribute to find all options (to correspond to config file keys); these are all part of the published interface and provide relatively simple data types to inspect. With an Argument Parser, one needs to rely upon the unpublished interface (the '_actions' attribute of a parser, etc...).

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

    paulj3 mannequin commented Mar 22, 2015

    The attached file subclasses ArgumentParser, explores how these optparse methods might be added to argparse. The __name__ section demonstrates how they might be used.

    argparse differs from optparse in a number of ways:

    • all actions are included in the parser._actions list.

    • the parser has one dictionary with all option_strings (both short and long)

    • argparse also has positionals, which don't have option_strings. They could be found by 'dest' (or some other attribute), but this does not have to be unique.

    • argparse does not use argument groups for parsing; just for formatting the help.

    • groups keep a list of their own actions in ._group_actions list. The ._actions list is shared among the parser and all groups.

    • every action is in an argument group, usually one of the 2 default groups. An action does not have a 'container' attribute.

    -------------------

    The idea of coordinating config sections with parser argument groups is interesting, but I question whether the stock argparser should be modified to facilitate this. It sounds like something that belongs in a custom subclass.

    Ipython populates its argparse parser with arguments derived from config files. This provides multiple config levels - default, custom file, and calling arguments. I don't recall whether it makes any use of argument groups.

    @emcd
    Copy link
    Mannequin Author

    emcd mannequin commented Mar 26, 2015

    @paul.j3, thanks for the sample code for argparse extension. I agree that subclassing is a way to go for use in third-party projects. But, if someone ever wanted to add an abstraction layer in front of argparse.ArgumentParser and configparser.ConfigParser in the standard library, then modification of the argparse module might be more convenient.

    However, in the intervening months since I originally filed this ticket, I ended up exploring an alternative to inspecting populated argparse.ArgumentParser instances for this use case (parser abstraction layer). The module I ended up writing is more of a framework in that it lets you supply argument and config parsers and then populate them via a common interface. While this design is perhaps higher maintenance, I think that it gives more flexibility and control than inspecting already-populated parsers. So, from my perspective, I no longer consider this use case to be a compelling reason for adding more optparse-like behavior to argparse.

    (If there interest in the little framework that I wrote, I can share my code and take the discussion to an appropriate mailing list.)

    Again, thanks for taking the time to write the argparse_extended.py code.

    @mcepl
    Copy link
    Mannequin

    mcepl mannequin commented Mar 5, 2018

    So, this bug report could be closed, right?

    @emcd
    Copy link
    Mannequin Author

    emcd mannequin commented Mar 6, 2018

    Yes, this issue could be closed. I think the concept is still valid and perhaps worthy of future consideration as it is a means of unifying two different configuration processing mechanisms in the standard library without having developers reinvent that wheel every time.

    I'll close it, though, if having a "stale" issue floating around is bothersome.

    @emcd emcd mannequin closed this as completed Mar 6, 2018
    @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
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants