An example Python script which uses argsvCPython library to process command-line arguments

#!/usr/bin/python -O

# argsv.py
#Written by, Sohail Qayum Malik[sqm@cyberian.pk]

"""
argsv module allows the use of functions print, str and len on its type instances
"""

import sys

try:
   import argsv

   # argsv module allows several names of one single command
   # Here we are asking argsv to process three different commands
   # Each command has more than few names
   args=argsv.argsv(sys.argv, "?,-?,/?,h,-h,help,--help#d,-d,dir,--dir#v,version")

   # str(args) throws TypeError exception when no command line arguments are given
   print "User has given following commands... " + str(args)

   h=None # Optional assignment to None

   # Iteration over args(a type instance)
   #internally arg and args both reference the same type instance
   for arg in args:
      # The mapped key can be ?, -?, /?, h, -h, help, or --help
      # The h has same type as arg or args has but it references a new type instance
      h=arg["/?"] # h=None when mapping fails, else h is a type instance
      if h: # If h is given(found) then next statement must be break
            # Otherwise at next iteration h will be None(assigned None to it)
         break

   del args

   if h != None:
      # This will put out all the names of the command
      # The str(h) also works and returns same names as a python string(possibly for assignment)
      print h
   else:
      print "!help"

   # Because internally arg and args both reference the same type instance
   args = arg

   for arg in args:
      d=arg["dir"]
      if d:
         break

   if d:
      i=1 # Assigned 1 and not 0(argsv.COMMON_ARGUMENTS_INDEX), command instances origin at 1
          # Arguments common to all commands(and all of their instances)...
          # origin at 0(argsv.COMMON_ARGUMENTS_INDEX)
      # argsv module can handle multiple instances of the same short or long command
      # Each instance is returned as a list
      while i:
         # l will be None if i is more than len(d)
         l=d(i)
         for arg in l:
            print arg
         del l # l is merely a python list, deling it here is most probably unnecessary
         i = i + 1
      else:
         print "No dir"

   # The list of all common command line arguments
   i=argsv.COMMON_ARGUMENTS_INDEX # i = 0
   # This can never be None when i is 0, the list atleast has a single entry for the script's name
   l=args(i)
   for arg in l:
      print arg
   del l

   del arg
   del args

   # Now process the first instance of d command
   # All the above detail(comments) is applied here(all over again)
   if d:
      i=1
      l=d(i)
      args=argsv.argsv(l,"l,-l#s,-s")
      print args
      k=args(argsv.COMMON_ARGUMENTS_INDEX)
      if k != None: # Optional, the list k will atleast have a single item
                    # Out of many, the one name which the user used at the command line
         for arg in k:
            print arg

except (ImportError, MemoryError, TypeError), e:
   print e

Last edited Sep 3, 2012 at 10:05 AM by cyberian, version 3

Comments

No comments yet.