Argparse"ArgumentError: argument -h/–help: conflicting option string(s): -h, –help"

Question:

Recently, I am learning argparse module, Argument error occurred below the code

import argparse
import sys


class ExecuteShell(object):
    def create(self, args):
        """aaaaaaa"""
        print('aaaaaaa')
        return args

    def list(self, args):
        """ccccccc"""
        print('ccccccc')
        return args

    def delete(self, args):
        """ddddddd"""
        print('ddddddd')
        return args


class TestShell(object):
    def get_base_parser(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('-h',
                            '--help',
                            action='store_true',
                            help=argparse.SUPPRESS)

        parser.add_argument('-c', action='store',
                            dest='create_value',
                            help='create a file')

        parser.add_argument('-d', action='store',
                            dest='delete_value',
                            help='delete a file')

        parser.add_argument('-l', action='store',
                            dest='list_value',
                            help='list a dir')

        return parser

    def _find_actions(self, subparsers, actions_module):
        for attr in (action for action in dir(actions_module) if not  action.startswith('__')):
            callback = getattr(actions_module, attr)
            desc = callback.__doc__ or ''
            subparser = subparsers.add_parser(attr, description=desc)
            subparser.add_argument('-h', '--help', action='help',
                                   help=argparse.SUPPRESS)
            self.subcommands[attr] = subparser
            subparser.set_defaults(func=callback)

    def main(self, args):
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(args)
        subparsers = parser.add_subparsers(metavar='<subcommand>')
        a = ExecuteShell()
        self.subcommands = {}
        subcommand_parser = self._find_actions(subparsers, a)


if __name__ == "__main__":
    a = TestShell()
    a.main(sys.argv[1:])

Why do I get this error and how can I fix it?

Asked By: changzhi

||

Answers:

argparse adds --help and -h options by default. If you don’t want to use the built-in help feature, you need to disable it with:

parser = argparse.ArgumentParser(add_help=False)

See the documentation

Answered By: Barmar

The same error pop-ups in 2 other scenarios:

1) Repeated code

parser.add_argument('-h',
                        '--help',
                        action='store_true',
                        help=argparse.SUPPRESS)

parser.add_argument('-h',
                        '--help',
                        action='store_true',
                        help=argparse.SUPPRESS)

2) When you execute the code multiple times on the same kernel

I’m leaving it just in case if someone had simillar problem.

Answered By: Jakub Bielan

Error: argparse.ArgumentError: argument --email: conflicting option string: --email

If anyone here by this error from django-rest-framework

So, this error occurs because the field ’email’ is required, but still in Model’s REQUIRED_FIELDS list.

Just remove it from REQUIRED_FIELDS, it should work.

This scenario also possible in other attributes also.


class UserAccount(AbstractUser):
    
    first_name = ...
    last_name = ...
    email = models.EmailField(_("Email address"), unique=True, blank=False)
    

    REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ['first_name', 'last_name', 'email']

    class Meta(AbstractUser.Meta):
        swappable = "AUTH_USER_MODEL"
        verbose_name = _("UserAccount")
        verbose_name_plural = _("UserAccounts")


Answered By: Akash Shendage
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.