且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Argparse:检查是否传递了任何参数

更新时间:2023-11-30 19:27:28

如果您的目标是检测何时没有参数被赋予命令,那么通过 argparse 执行此操作code> 是错误的方法(正如 Ben 很好地指出的那样).

If your goal is to detect when no argument has been given to the command, then doing this via argparse is the wrong approach (as Ben has nicely pointed out).

想的很简单!:-) 我相信 argparse 不会减少 sys.argv.所以,if not len(sys.argv) >1,则用户未提供任何参数.

Think simple! :-) I believe that argparse does not depopulate sys.argv. So, if not len(sys.argv) > 1, then no argument has been provided by the user.