argparse: some mutually exclusive arguments in required group

前端 未结 3 2367
难免孤独
难免孤独 2021-02-19 08:56

I have a set of arguments that can logically be separated in 2 groups:

  • Actions: A1, A2, A3, etc.
  • Informations:
3条回答
  •  不知归路
    2021-02-19 09:26

    Im i missing something or do you just want:

    import argparse
    import os
    
    def main():
        parser = argparse.ArgumentParser()
        actions = parser.add_mutually_exclusive_group()
        actions.add_argument("-A1", action="store_true")
        actions.add_argument("-A2", action="store_true")
        actions.add_argument("-A3", action="store_true")
        low = int(os.environ.get('LOWER_BOUNDS', 0))
        high = int(os.environ.get('UPPER_BOUNDS', 3)) + 1
        infos = parser.add_argument_group()
        for x in range(low, high):
            infos.add_argument("-I" + str(x), action="store_true")
    
        args = parser.parse_args()
        if not any(vars(args).values()):
            parser.error('No arguments provided.')
        print args
    
    if __name__ == '__main__':
        main()
    

    output:

    $ python test.py 
    usage: test.py [-h] [-A1 | -A2 | -A3] [-I0] [-I1] [-I2] [-I3]
    test.py: error: No arguments provided.
    $ python test.py -A1
    Namespace(A1=True, A2=False, A3=False, I1=False, I2=False, I3=False)
    $ python test.py -A1 -A2
    usage: test.py [-h] [-A1 | -A2 | -A3] [-I1] [-I2] [-I3]
    test.py: error: argument -A2: not allowed with argument -A1
    $ python test.py -A1 -I1
    Namespace(A1=True, A2=False, A3=False, I1=True, I2=False, I3=False)
    $ python test.py -A1 -I1 -I2
    Namespace(A1=True, A2=False, A3=False, I1=True, I2=True, I3=False)
    $ python test.py -A1 -I1 -I2 -I3
    Namespace(A1=True, A2=False, A3=False, I1=True, I2=True, I3=True)
    $ UPPER_BOUNDS=40 python test.py -A1 -I1 -I2 -I40
    Namespace(A1=True, A2=False, A3=False, I0=False, I1=True, I10=False, I11=False, I12=False, I13=False, I14=False, I15=False, I16=False, I17=False, I18=False, I19=False, I2=True, I20=False, I21=False, I22=False, I23=False, I24=False, I25=False, I26=False, I27=False, I28=False, I29=False, I3=False, I30=False, I31=False, I32=False, I33=False, I34=False, I35=False, I36=False, I37=False, I38=False, I39=False, I4=False, I40=True, I5=False, I6=False, I7=False, I8=False, I9=False)
    

    PS. I dont really suggest this "unlimited" -I# approach.. but here is an example of it.

提交回复
热议问题