In Python 2.7.13
We have the following python code to take the command line argument:
import sys
import csv
import os
import sys, getopt
import pandas as pd
print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)
def main():
SERVER_NAME=''
PORT_NAME=''
PASSWORD=''
try:
opts, args = getopt.getopt(sys.argv[1:],"hs:p:x:",["server=","port=","password="])
except getopt.GetoptError:
print 'help'
sys.exit(2)
for o, a in opts:
if o == '-h':
print '\n'+'-s / --server (required)'
print '\n'+'-p or --port (required)'
print '\n'+'-x or --password (required)'
sys.exit()
elif o in ("-s", "--server"):
SERVER_NAME = a
elif o in ("-p", "--port"):
PORT_NAME = a
elif o in ("-x", "--password"):
PASSWORD = a
else:
assert False, "No command line option. To see the options, plese use -h"
if __name__ == "__main__":
main()
print SERVER_NAME
dbServer=SERVER_NAME
However, when running the above code in the command line:
python test.py -s AAA -p BBB -x CCC
the following error will show up:
print SERVER_NAME
NameError: name 'SERVER_NAME' is not defined
Could any guru enlighten if anything is wrong here? Thanks.