0
def parseCommand():
    args = None
    if len (sys.argv) < 2:
        print('no command entered')
        return

    command = sys.argv[1]
    print ('command executed: %s' %command)
    if len(sys.argv) == 3:
        args = sys.argv[2]
    if len(sys.argv) > 3:
        args = sys.argv[2:]

    commandList = {
                'fA'              : fA(),
                'fB'              : fB(),
                }

    if args == None:
        return commandList[command]
    else:
        return commandList[command](args)

if __name__ == "__main__": 
    parseCommand()

I am running this script from linux. However when I run python scriptname.py fA or python scriptname.py fB, the script seems to run just from top to bottom, executing both functions When I change it to

if __name__ == "__main__": 
    fA()

or

if __name__ == "__main__": 
    fB()

and run python scriptname.py frtom linux it only executes the function I am calling.

Could anyone tell me what s wrong with my approach? Thanks

0

1 Answer 1

8

You're creating commandList wrong; the parentheses after fA and fB are calling the functions:

commandList = {
            'fA'              : fA(),
            'fB'              : fB(),
            }                     ~~ <-- remove these parentheses
Sign up to request clarification or add additional context in comments.

1 Comment

In addition, you'll need to change the line "return commandList[command]" to "return commandList[command]()"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.