When running a python script in IDLE, is there a way to pass in command line arguments (args)?

前端 未结 11 837
猫巷女王i
猫巷女王i 2020-12-02 13:08

I\'m testing some python code that parses command line input. Is there a way to pass this input in through IDLE? Currently I\'m saving in the IDLE editor and running from a

11条回答
  •  佛祖请我去吃肉
    2020-12-02 13:33

    This code works great for me, I can use "F5" in IDLE and then call again from the interactive prompt:

    def mainf(*m_args):
        # Overrides argv when testing (interactive or below)
        if m_args:
            sys.argv = ["testing mainf"] + list(m_args)
    

    ...

    if __name__ == "__main__":
        if False: # not testing?
            sys.exit(mainf())
        else:
            # Test/sample invocations (can test multiple in one run)
            mainf("--foo=bar1", "--option2=val2")
            mainf("--foo=bar2")
    

提交回复
热议问题