python 的sys.exit(1) 和sys.exit(2)的区别

if not cmdname:

    _print_commands(settings, inproject)
    sys.exit(0)

elif cmdname not in cmds:

    _print_unknown_command(settings, cmdname, inproject)
    sys.exit(2)
    

这里的sys.exit(2)和sys.exit(1)的区别
不知道为啥这么写?

阅读 17.1k
1 个回答

sys.exit([args])的参数解析

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like.

意思就是参数为数字的时候,和 shell 退出码意义是一样的,sys.exit(2)sys.exit(1)只是为了区分结束原因

  • 0 :成功结束
  • 1 :通用错误  
  • 2 :误用Shell命令
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题