Why is argparse with python 3.8 failing for me with flags with same prefix?

Viewed 246

This question gets obsolete with the next freeipa release as @abbra mentioned.

Can someone give me a hint why I get the following error?

An unexpected error occurred:
argparse.ArgumentError: argument --ipa--h/--host_name/--host: conflicting option strings: --host_name, --host

Code:

@zope.interface.implementer(certbot.interfaces.IAuthenticator)
@zope.interface.provider(certbot.interfaces.IPluginFactory)
class Authenticator(certbot.plugins.dns_common.DNSAuthenticator):
    """ FreeIPA / Red Hat Enterprise Linux IdM authentication using DNS challenges """

    description = __doc__.strip().split("\n", 1)[0]

    def __init__(self, *args, **kwargs):
        # Maps provided domains to IPA zones
        self.zone_map = {}
        super(Authenticator, self).__init__(*args, **kwargs)


    @classmethod
    def add_parser_arguments(cls, add, default_propagation_seconds=10):
        super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds)
        add('-h', '--host_name', '--host', dest='ipa_host', help='Hostname or IP Address of the IPA (Satellite) server')
        add('-d', '--ipa_domain_name', '--ipa_domain', dest='ipa_domain', help='Domain in IPA (Satellite) to register under')
        add('-H', '--xml_rpc_url', '--xmlrpc-url', dest='ipa_xml_rpc_url', help='XML RPC service location')
        add('-C', '--ca_path', '--capath', dest='ca_path', help='Path do a directory containing PEM encoded CA files')
        add('-c', '--ca_file', '--cafile', dest='ca_file', help='Path do a file containing PEM encoded CA certificates')
        add('-t', '--keytab_name', '--keytab-name', dest='keytab_file', help='Path to a keytab file containing credentials for IPA server authentication')
        add('-k', '--submitter_principal', '--submitter-principal', dest='submitter_principal', help='Kerberos principal for IPA server authentication')
        add('-K', '--use_ccache_creds', '--use-ccache-creds', dest='use_ccache_creds', default=False, action='store_true', help='Use default ccache for authorization instead of authenticating')
        add('-P', '--request_principal', '--principal-of-request', dest='request_principal', help='Principal(s) (FQDN) used in signing request, comma separated')
        add('-T', '--request_profile', '--profile', dest='request_profile', help='Use a specific profile when requesting enrollment')

The complete code is hosted on Github. I'm trying to port this to python 3.8 as this is the recent fedora 32 python version.

Thanks in advance and please be kind. I'm an absolute Python newbie.

1 Answers

You need to create your instance of ArgumentParser with allow_abbrev=False.

Explanation: --host is an abbreviation of --host-name. By default, abbreviations are allowed. So, --host is already covered by --host-name.

Alternatively, just leave out --host, as it will be matched by --host-name anyway.

See the docs: https://docs.python.org/3/library/argparse.html#allow-abbrev

Related