Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a main function which runs with and without taking command line arguments. The given command goes to else logic and exits with value.
But my query is, if user runs without command line argument then logic should not got to exit so can run commands continuously.

Python
def main():

    versionPrompt = "linuxtest >>"
    parser=argparse.ArgumentParser(description='Linux commands')
    parser.add_argument('-t', required=False, help = 'Pass test command to run')
    args=parser.parse_args()

    print("Linux test")
    print(versionPrompt, end="")

    if len(sys.argv)>1:
        cmdfct = sys.argv[2]
    else :
        cmdfct = sys.stdin.readline()

    while not quitflag:
        print(versionPrompt, end=""'\n')
        cmdfct = cmdfct.strip()

        if "quit" in cmdfct.casefold():
            quitflag=1

        elif "help" in cmdfct.casefold():
             processHelp()
             break;

        else :
             cmdstr = cmdfct
             cmdargs = cmdstr.split('=')

             if cmdargs[0] in testHandlers.keys():
                 func_n = cmdargs[0][4:]

                 if len(cmdargs) > 1 :
                     rest =testHandlers[cmdargs[0]](func_n, cmdargs[1])
                     if rest ==0:
                         exit(0)
                     else :
                         exit(1)
                 else :
                     rest =testHandlers[cmdargs[0]]()
                     print(rest)
                     if rest ==0:
                         exit(0)
                     else :
                         exit(1)

if __name__ == "__main__":
    main()


What I have tried:

The code in else condition without exit codes looks like
Python
cmdstr = cmdfct
cmdargs = cmdstr.split('=')
if cmdargs[0] in testHandlers.keys():
    func_n = cmdargs[0][4:]
    if len(cmdargs) > 1 :
          testHandlers[cmdargs[0]](func_n, cmdargs[1])
    else :
          testHandlers[cmdargs[0]]()
Posted
Updated 19-Jul-22 18:24pm
v2
Comments
Member 15627495 20-Jul-22 1:08am    
you need one more loop, which contains all.
Richard MacCutchan 20-Jul-22 4:42am    
That code should fail, since quitflag has not been defined before use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900