Search code examples
pythonargparse

Store values from mutually exclusive group


After declaring a mutually exclusive group, how are these value accessed. If the user specifies --predict, only 4 values should be allowed and once they are attached, how are they accessed? Here is my code that is not allowing me to access the list of attached numbers:

    parser = argparse.ArgumentParser(description='Video game preferences: train or predict mode.')
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--train', action='store', help='')
    group.add_argument('--predict', nargs=4, type=float, action='store', help=''represented by 1s)')
    arg = parser.parse_args()
    print(arg)

And here is how I am calling the program from cmd:

main.py --predict 1 8 7 6


Solution

  • add_mutually_exclusive_group does not change the syntax for how the arguments are accessed, it only adds additional checks at runtime to raise errors if multiple exclusive args are provided. You access the value like you do for any other arg, e.g. arg.train or arg.predict in this example.