Aug-30-2019, 10:29 PM
the original question was indeed about command line parsing. i am currently writing a command that is not the original reason for asking, but it may be another case to consider because of the way it works.
it lists AMIs at AWS, either those that you created, or public ones. it takes a series of arguments that name AWS regions to be listed, or options to affect the command behavior. the options do not use "-" or "+" although i am thinking of adding that because people might type them in. if i do that, it might be to simply discard all non-alphanumeric characters until an alphanumeric one. so typing "--public" would be like typing "public" as would typing "/-=.,public". that would apply to typing regions, too. so "list_amis -us-east-2 //public ..eu-north-1 _ownerid" would be a valid command.
it's not complicated at all. a for loop scans sys.argv (after the command name was popped off) and each argument is checked to see if it is in the set of known regions (downloaded from AWS earlier) or in a dictionary of known options. if it is a region, it is appended to the list of regions. if it is an option, the name and value for an environment variable is obtained and set. all options are related to environment variables so that they can be preset and passed along when a new instance of itself is executed with subprocess.Popen().
if only one region is given, there are no child processes. if multiple regions are given, a child process runs the same command with just one region, for each region given (options are passed as environment variables).
to me, scanning the arguments for this is just too simple. i feel no parsing module is needed for this one.
the command that triggered the original question is a bit more complicated. in its C version i did not use getopt() because it would not handle it. and getopt() is the only one i found for C. but in Python, there are many choices. maybe one will work.
but i can't do debugging around decorators (yet) so that suggestion is outside of what i am prepared to do. i have to be able to debug my own code.
at this point i have a couple things active on my plate. one is too finish to program to list AWS AMIs. two is to go read those links about decorators.
it lists AMIs at AWS, either those that you created, or public ones. it takes a series of arguments that name AWS regions to be listed, or options to affect the command behavior. the options do not use "-" or "+" although i am thinking of adding that because people might type them in. if i do that, it might be to simply discard all non-alphanumeric characters until an alphanumeric one. so typing "--public" would be like typing "public" as would typing "/-=.,public". that would apply to typing regions, too. so "list_amis -us-east-2 //public ..eu-north-1 _ownerid" would be a valid command.
it's not complicated at all. a for loop scans sys.argv (after the command name was popped off) and each argument is checked to see if it is in the set of known regions (downloaded from AWS earlier) or in a dictionary of known options. if it is a region, it is appended to the list of regions. if it is an option, the name and value for an environment variable is obtained and set. all options are related to environment variables so that they can be preset and passed along when a new instance of itself is executed with subprocess.Popen().
if only one region is given, there are no child processes. if multiple regions are given, a child process runs the same command with just one region, for each region given (options are passed as environment variables).
to me, scanning the arguments for this is just too simple. i feel no parsing module is needed for this one.
the command that triggered the original question is a bit more complicated. in its C version i did not use getopt() because it would not handle it. and getopt() is the only one i found for C. but in Python, there are many choices. maybe one will work.
but i can't do debugging around decorators (yet) so that suggestion is outside of what i am prepared to do. i have to be able to debug my own code.
at this point i have a couple things active on my plate. one is too finish to program to list AWS AMIs. two is to go read those links about decorators.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.