Disable/Remove argument in argparse

前端 未结 4 1099
攒了一身酷
攒了一身酷 2021-01-07 18:15

Is it possible to remove or disable an argument in argparse, such that it does not show in the help? How?

It is easy to add new arguments:



        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-07 18:48

    Although hpaulj answer is great, in my case using just parser._remove_action(action) was not removing from the "positional arguments" help the removed action. My workaround was to remove it also from the _action_group

    def remove_option(parser, arg):
        for action in parser._actions:
            if (vars(action)['option_strings']
                and vars(action)['option_strings'][0] == arg) \
                    or vars(action)['dest'] == arg:
                parser._remove_action(action)
    
        for action in parser._action_groups:
            vars_action = vars(action)
            var_group_actions = vars_action['_group_actions']
            for x in var_group_actions:
                if x.dest == arg:
                    var_group_actions.remove(x)
                    return
    

提交回复
热议问题