Parsing Named Arguments into Python Scripts

Posted by Dirk Nachbar on Tuesday, August 22, 2017
If you are coming from the Shell Script World, you are used to parse Named Arguments into your Shell Script, e.g "-u" for Username, "-p" for Password and so on. With Python Scripts you normally pass ordered arguments into your Python Script, so you are bond with the arguments in a fixed order, while passing Named Arguments you can reshuffle them in any order.

A typical Shell Script Snippet would look like that:

export UserName=""
export PassWord=""
export URL=""

while getopts u:p:c: CurOpt; do
    case ${CurOpt} in
        u) UserName="${OPTARG}"  ;;
        p) PassWord="${OPTARG}"  ;;
        c) URL="${OPTARG}"       ;;
        ?) Usage                 ;;
    esac
done

shift $((${OPTIND}-1))

# Call a function for checking of given Parameter Values
CheckInputParams

The same you can achieve for your Python Scripts to be used with the Oracle WebLogic Server command line tool wlst.

import sys

uname=''
pword=''
url=''

def helpUsage():
   print 'Usage: test.py [-help]'
   print '      [-username] Username for the connect'
   print '      [-password] Password for the connect User'
   print '      [-url] URL for the connect'
   exit()

for i in range(len(sys.argv)):
   if sys.argv[i] in ("-help"):
           helpUsage()
   elif sys.argv[i] in ("-url"):
           if i+1 < len(sys.argv):
                   url = sys.argv[i+1]
   elif sys.argv[i] in ("-username"):
           if i+1 < len(sys.argv):
                   uname = sys.argv[i+1]
   elif sys.argv[i] in ("-password"):
           if i+1 < len(sys.argv):
                   pword = sys.argv[i+1]

if len(uname)==0 or len(pword)==0 or len(url)==0:
   print 'Missing required arguments (-url, -username, -password)'
   print ' '
   helpUsage()


connect(uname,pword,url)

#
# Perform your Tasks
#

exit()

Now you can call your Python Script with wlst and provide the required Named Arguments in any order you like, e.g.:

# Using Named Argument -help
wlst.sh test.py -help

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Usage: test.py [-help]
      [-username] Username for the connect
      [-password] Password for the connect User
      [-url] URL for the connect

Exiting WebLogic Scripting Tool.


# Using Named Arguments -username / -password / -URL
wlst.sh test.py -username weblogic -url t3://localhost:7001 -password Welcome1

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:7001 with userid weblogic ...
. . .
. . .
Successfully connected to Admin Server "DEMOAdminServer" that belongs to domain "DEMODomain".

Exiting WebLogic Scripting Tool.


# Using Named Arguments, but forgot to pass a value for Argument -Password
wlst.sh test2.py -username weblogic -url t3://localhost:7001 -password

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Missing required arguments (-url, -username, -password)

Usage: test.py [-help]
      [-uname] Username for the connect
      [-password] Password for the connect User
      [-url] URL for the connect

Exiting WebLogic Scripting Tool.

With a simple small Python Block you can easily pass Named Arguments into your Python Scripts in the same way you know it with Shell Scripts.