paul j3 <ajipa...@gmail.com> added the comment:

prefix_chars is a parameter of the parent _ActionsContainer

class _ActionsContainer(object):

    def __init__(self,
                 description,
                 prefix_chars,
                 argument_default,
                 conflict_handler):
        super(_ActionsContainer, self).__init__()

        self.description = description
        self.argument_default = argument_default
        self.prefix_chars = prefix_chars
        self.conflict_handler = conflict_handler

add_argument is also a method in this class.  It uses its own prefix_chars to 
categorize arguments as optional/positional

        chars = self.prefix_chars
        if not args or len(args) == 1 and args[0][0] not in chars: 

see also _get_optional_kwargs method.

class _ArgumentGroup(_ActionsContainer):

inherits from _ActionsContainer, and usually gets the prefix_chars from its 
container (the parser)

        update('prefix_chars', container.prefix_chars)

The parser is created with

class ArgumentParser(_AttributeHolder, _ActionsContainer):

and documents the use of prefix_chars.

In addition to passing prefix_chars to its Super, it uses

        default_prefix = '-' if '-' in prefix_chars else prefix_chars[0]

to define the -h help argument.

Early in parsing, args strings are categorized as 'O' or 'A'.  That is done in, 
which uses:

    def _parse_optional(self, arg_string):

        # if it doesn't start with a prefix, it was meant to be positional
        if not arg_string[0] in self.prefix_chars:
            return None

def _get_option_tuples(self, option_string):

also uses the parser's own prefix_chars.

During parsing (several layers down)

    def consume_optional(start_index):

                    # if the action is a single-dash option and takes no
                    # arguments, try to parse more single-dash options out
                    # of the tail of the option string
                    chars = self.prefix_chars
                    if arg_count == 0 and option_string[1] not in chars:

Here, the parser's own prefix_chars is used to handle the chained short-dash 
options.


---

In sum, while Argument_Group can have its own prefix_chars, that is only used 
for help formatting.  It's the parser's prefix_chars that is used when parsing. 
 The group's chars is ignored.

The _ArgumentGroup__init__  suggests something more than simple inheritance may 
have been intended for prefix_chars, but in practice all it affects is the 
help.  

I haven't seen this issue raised before, and since this group parameter is  not 
documented, I think it's safe to ignore it.  

An alternative is to have add_argument_group (or _ArgumentGroup__init__) 
explicitly reject the prefix_chars parameter.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45656>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to