Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
722 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...