Python Forum
i need a module for more involved command line parsing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i need a module for more involved command line parsing
#18
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
RE: i need a module for more involved command line parsing - by Skaperen - Aug-30-2019, 10:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  module either imported by a command or itself run as a command Skaperen 2 648 Dec-04-2023, 03:15 AM
Last Post: Skaperen
  review of command line parsers Skaperen 2 2,120 Mar-11-2021, 07:39 PM
Last Post: Skaperen
  command line options Skaperen 5 2,733 Aug-14-2020, 08:48 AM
Last Post: DeaD_EyE
  opening python from the command line takes a long time to load? abdulkaderanwar 4 3,090 Jun-22-2020, 03:42 AM
Last Post: abdulkaderanwar
  f-string in command line arguments Skaperen 0 1,640 May-05-2020, 11:49 PM
Last Post: Skaperen
  my own command line option parser Skaperen 0 1,690 Mar-27-2020, 04:14 AM
Last Post: Skaperen
  want suggested module for e-mail parsing Skaperen 0 1,567 Jul-26-2019, 08:52 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020