'''
Handles all things command line - used from passman.py
'''

from login import handleLogin
from menu import welcomeMessage, listServicesPrompt,\
        addServicePrompt, editServicePrompt, removeServicePrompt,\
        getUrlPrompt, getNamePrompt

def printUsage(argv):
    print("Usage:",argv[0],"[{\nadd\t[service name] [service username] [service url]  \nremove\t[service name]  \nedit\t[service name] \nlist\t \npass\t[service name] \nuname\t[service name]  \nurl\t[service name]}]")
    quit()

def isOption(arg):
    optionList = ["add", "remove", "list", \
            "edit", "pass", "url", "uname"]
    return arg in optionList

def handleCLArgs(argv):
    if not isOption(argv[1]):
        printUsage(argv)

    welcomeMessage()
    handleLogin()

    if argv[1]=="add":
        handleCLAdd(argv)
    elif argv[1]=="remove":
        handleCLRemove(argv)
    elif argv[1]=="list":
        listServicesPrompt()
    elif argv[1]=="edit":
        handleCLEdit(argv)
    elif argv[1]=="pass":
        handleCLPass(argv)
    elif argv[1]=="uname":
        handleCLUName(argv)

    elif argv[1]=="url":
        handleCLURL(argv)
    else:
        printUsage(argv)

def handleCLAdd(argv):
    if len(argv)==2:
        addServicePrompt()
    elif len(argv)==3:
        addServicePrompt(argv[2])
    elif len(argv)==4:
        addServicePrompt(argv[2],argv[3])
    elif len(argv)==5:
        addServicePrompt(argv[2],argv[3],argv[4])
    else:
        printUsage(argv)

def handleCLRemove(argv):
    if len(argv)==2:
        removeServicePrompt()
    elif len(argv)==3:
        removeServicePrompt(argv[2])
    else:
        printUsage(argv)

def handleCLEdit(argv):
    if len(argv)==2:
        editServicePrompt()
    elif len(argv)==3:
        editServicePrompt(argv[2])
    else:
        printUsage(argv)

def handleCLPass(argv):
    print("TODO")

def handleCLUName(argv):
    if len(argv)==2:
        getNamePrompt()
    elif len(argv)==3:
        getNamePrompt(argv[2])
    else:
        printUsage(argv)

def handleCLURL(argv):
    if len(argv)==2:
        getUrlPrompt()
    elif len(argv)==3:
        getUrlPrompt(argv[5])
    else:
        printUsage(argv)


@@ -86,16 +86,36 @@
 dleLogin
+, handleOfflineLogin
 %0Afrom me
@@ -263,16 +263,162 @@
 mePrompt
+%0Afrom database import checkConnection%0Afrom offlinemenu import getPasswordOffline, listServicesOffline, %5C%0A        getUserNameOffline, getURLOffline
 %0A%0Adef pr
@@ -912,388 +912,1072 @@
   ha
-ndleLogin()%0A%0A    if argv%5B1%5D==%22add%22:%0A        handleCLAdd(argv)%0A    elif argv%5B1%5D==%22remove%22:%0A        handleCLRemove(argv)%0A    elif argv%5B1%5D==%22list%22:%0A        listServicesPrompt()%0A    elif argv%5B1%5D==%22edit%22:%0A        handleCLEdit(argv)%0A    elif argv%5B1%5D==%22pass%22:%0A        handleCLPass(argv)%0A    elif argv%5B1%5D==%22uname%22:%0A        handleCLUName(argv)%0A%0A    elif argv%5B1%5D==%22url%22:%0A        handleCLURL
+sConnection = checkConnection(%22test%22)%0A%0A    if hasConnection:%0A        handleLogin()%0A    else:%0A        handleOfflineLogin()%0A%0A    if argv%5B1%5D==%22add%22:%0A        if hasConnection:%0A            handleCLAdd(argv)%0A        else:%0A            print(%22Sorry, no connection%22)%0A    elif argv%5B1%5D==%22remove%22:%0A        if hasConnection:%0A            handleCLRemove(argv)%0A        else:%0A            print(%22Sorry, no connection%22)%0A    elif argv%5B1%5D==%22list%22:%0A        if hasConnection:%0A            listServicesPrompt()%0A        else:%0A            listServicesOffline()%0A    elif argv%5B1%5D==%22edit%22:%0A        if hasConnection:%0A            handleCLEdit(argv)%0A        else:%0A            print(%22Sorry, no connection%22)%0A    elif argv%5B1%5D==%22pass%22:%0A        if hasConnection:%0A            handleCLPass(argv)%0A        else:%0A            getPasswordOffline(argv)%0A    elif argv%5B1%5D==%22uname%22:%0A        if hasConnection:%0A            handleCLUName(argv)%0A        else:%0A            getUserNameOffline(argv)%0A%0A    elif argv%5B1%5D==%22url%22:%0A        if hasConnection:%0A            handleCLURL(argv)%0A        else:%0A            getURLOffline
 (arg

Check for connection in command line args