I'm trying to collect variables from the command line arguments with this script
import requests
import json
import sys, getopt
UNKNOWN = -1
OK = 0
WARNING = 1
CRITICAL = 2
usage = 'usage: python artifactory-move.py -u/--user -p/--password '
def command_line_validate(argv):
try:
opts, args = getopt.getopt(argv, 'u:p', ['user=' ,'password='])
except getopt.GetoptError:
print (usage)
try:
for opt, arg in opts:
if opt in ('-u', '--user'):
try:
user = arg
except:
print ('***user value must be an entered***')
sys.exit(CRITICAL)
elif opt in ('-p', '--password'):
try:
password = arg
except:
print ('***password value must be entered***')
sys.exit(CRITICAL)
else:
print (usage)
try:
isinstance(user, str)
#print 'user level:', user
except:
print ('***user is required***')
print (usage)
sys.exit(CRITICAL)
try:
isinstance(password, str)
#print 'password level:', password
except:
print ('***pass is required***')
print (usage)
sys.exit(CRITICAL)
except:
sys.exit(CRITICAL)
return user, password
argv = sys.argv[1:]
user, password= command_line_validate(argv)
print (user)
print (password)
Can't find the reason why the it only prints user, I suspect it's the first "try" but i'm not sure how else to do this. I'm new to python perhaps i'm missing something very obvious here
You wrote:
opts, args = getopt.getopt(argv, 'u:p', ['user=' ,'password='])
The u:p means:
-u take an argument (because ':' after u)
-p take no argument (no ':' after p)
Hope this will help you