python - argparse one argument instead of several others
Question: For example I have options:

    
    
    parser.add_argument('-b', action="store_true")
    parser.add_argument('-c', action="store_true")
    parser.add_argument('-d', action="store_true")
    

I want to enable all of them with synonym option "-a". Is it possible?


Answer: I think it is simplest to do this after `parse_args()`:

    
    
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('-b', action="store_true")
    parser.add_argument('-c', action="store_true")
    parser.add_argument('-d', action="store_true")
    parser.add_argument('-a', action="store_true")
    args = parser.parse_args()
    if args.a: args.b = args.c = args.d = True
    

Alternatively, you could do it with a custom action, but I think it is
overkill:

    
    
    import argparse
    class AllAction(argparse.Action):
        def __call__(self, parser, args, values, option_string = None):
            # print '{n} {v} {o}'.format(n = args, v = values, o = option_string)
            for param in ['a', 'b', 'c', 'd']:
                setattr(args, param, True)
    
    parser = argparse.ArgumentParser()
    parser.add_argument('-b', action="store_true")
    parser.add_argument('-c', action="store_true")
    parser.add_argument('-d', action="store_true")
    parser.add_argument('-a', action = AllAction, nargs = '?')
    args = parser.parse_args()
    print(args)
    

