![]() There's no builtin comprehensive help display. The current setup is to have separate help displays for the main parser and for each of the subparsers. ![]() The help display for subparsers is not as flexible as it could be, but reworking it takes a fair amount of work (there are SO questions about this). Without looking at it carefully your subparsers code looks right, and should parse the input as you want. But you are interested in assigning values based on value, or rather restricting values to a set of choices. It can't tell, based just on position, whether you want to assign the string to foo or to bar. Two mutually exclusive positionals doesn't make logical sense `` But the action taken is to deligate the parsing of the rest of the arguments to the subparser. The comma is most often used just before namely: They brought lunch, namely sandwiches and soda. The comma can be used in every possible combination with the word namely, but it's worth looking closely at each of them. To the main parser the subparser argument is just a positional with choices. The adverb namely is often set off by the use of a comma. ![]() Type must be a function, one that converts as string to something you want, eg int('1'), float('12.343'). Or as a positional build_parser.add_argument('project', # type = project_name, # doesn't make sense In the first scenario, you could give -p choices build_parser.add_argument('-p', '-project',Ĭhoices = , I know about the ArgumentParser.add_mutually_exclusive_group(required=False) method, but using it didn't help achieving what I wanted, since 1) the arguments must be optional when using it, and 2) the usage format is not like I want. Where query and build are mutually exclusive. Query_parser = command_parser.add_parser('query', help = "Query current state")īuild_parser = command_parser.add_parser('build', help = "Build project")īut this produces the following: usage: test.py. I tried using subparsers as follows: def parse_arguments():Ĭommand_parser = arg_parser.add_subparsers(help = "Command") p project_A|project_B|project_C, -project project_A|project_B|project_Cīut what I really want is for query and build to be two mutually exclusive positional arguments. h, -help show this help message and exit Which gives the following: usage: test.py -p Metavar = 'project_A|project_B|project_C', Query_parser.add_argument('-u', '-users',īuild_parser.add_argument('-f', '-force',īuild_parser.add_argument('-c', '-clean',īuild_parser.add_argument('-v', '-verbosity',īuild_parser.add_argument('-p', '-project', Query_parser.add_argument('-s', '-servers', Query_parser = arg_parser.add_argument_group('query', "Query current state")īuild_parser = arg_parser.add_argument_group('build', "Build project") This is what I currently have: def parse_arguments():Īrg_parser = argparse.ArgumentParser(description = 'Project Builder') I want my program to accept mutually exclusive positional arguments, and the usage to be displayed as a group of arguments.Ĭurrently I could only achieve one or the other, but not both.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |