i'm trying to make a python program that has a complex argument parser . the program (let call it MyProg ) need to work as follow
1-MyProg wil take some files as input so i used a -f flage in the main parser (this is already a subparser so i can't use a subparser).
2-the program has 2 argument (-a and -s)that shoudent be present together and one of them is required so i used a mutualexclusion group.
3--a need to have a -k argument with it so i put the -k alone in a normal group inside the mutualexclusion group i made i the 2nd step and made it (-k) a required argument.
4-the problem is that the -s argument need either -k or another --e argument (not both) so i don't know where to put this -e argument so it can't be with -a or with -k .
the code :
description = "Prog"
# Initialize parser
parser = argparse.ArgumentParser(description=description,)
subparsers = parser.add_subparsers(required=True)
parser_MyProg = subparsers.add_parser('MyProg')
group_mut = parser_MyProg.add_mutually_exclusive_group()
group_k = group_mut.add_argument_group()
group_mut.add_argument('-a', type=int)
group_k.add_argument('-k', '--key', required=True, type=type=argparse.FileType('r'))
group_mut.add_argument('-s', type=int,)
group_mut.add_argument('-e',type=int)
parser_MyProg.add_argument( '-f', nargs='+', type=type=argparse.FileType('r'))
#some valid usecases (Should be valid)
args = parser.parse_args(
'MyProg -a 8 -k k.txt -f file1.txt file2.txt '.split())
args = parser.parse_args(
'MyProg -s 8 -k k.txt -f file1.txt file2.txt '.split())
args = parser.parse_args(
'MyProg -s 8 -e 5 -f file1.txt file2.txt '.split())
#some invalid usecases (Should not be valid)
args = parser.parse_args('MyProg -a 8 -f file1.txt file2.txt '.split()) #need a -k
args = parser.parse_args('MyProg -s 8 -f file1.txt file2.txt '.split()) #need a -k or -e
args = parser.parse_args('MyProg -s 8 -e 5 -k k.txt -f file1.txt file2.txt '.split()) # -k and -e can't be present together
in the code above i used -e in the mutual exclusion groupe but that doesent work because : 1- i can't use it with -s 2- i can use it with -k 3- the argument -k is required even with -s argument (with -s i want either -e or -k not both)
i simplified the code a little so you can see the problem . the program is an encryption and decryption and hashing program but i just wanted to make it clear that's why i did simplify it a little.
usageas well. Instead it looks like I'll have to spend several minutes studying your code and description to figure out that out. Latter...add_argument_groupis used only for help formatting; it does not form a "any/and" group within the mutually_exclusive_group.argparsedoes not provide that kind of logic. Only the simple "xor" logic of an exclusive group.destto theadd_subparsersso it can format an error message.argument_groupandmutually_exclusive_groupare not designed to be nested. Due to inheritance, the commands don't raise errors, but the nesting doesn't do anything useful. Here-kgets added to theMyProgsubparser, but not togroup_mut.