缺少3个必需的位置参数,使用sys.argv[]。

huangapple go评论99阅读模式
英文:

Missing 3 required positional argument with sys.argv[]

问题

我有一个关于我从Jenkins流水线中调用的Python脚本的问题。
在主脚本中,我有以下内容:

  1. import sys
  2. authentication = sys.argv[1]
  3. style = sys.argv[2]
  4. ip = sys.argv[3]
  5. accounting = sys.argv[4]
  6. PrintTestCases([str(authentication), str(style), str(ip), str(accounting)])

函数PrintTestCases在另一个文件中,并使用匹配-情况结构:

  1. def PrintTestCases(authentication, style, ip, accounting):
  2. match accounting:
  3. case "NOACC":
  4. match [authentication, style, ip]:
  5. case ['NONE', 'NONE', 'NONE']:
  6. print("测试用例编号1")
  7. case _:
  8. print("错误的测试用例")
  9. case "ACC":
  10. match [authentication, style, ip]:
  11. case ['PRIVATE', 'NONE', 'NONE']:
  12. print("测试用例编号2")
  13. case _:
  14. print("错误的测试用例")

然后我从Jenkins流水线中这样调用主脚本:

  1. python3 -u main NONE NONE NONE ACC

但我总是得到这个错误:

  1. PrintTestCases()缺少3个必需的位置参数'style''ip''accounting'

根据我的理解,我是通过sys.argv将参数传递给函数的。为什么函数看不到所需的参数?

英文:

I have a problem with a python script I call from a Jenkins pipeline.
In the main script I have this:

  1. import sys
  2. authentication = sys.argv[1]
  3. style = sys.argv[2]
  4. ip = sys.argv[3]
  5. accounting = sys.argv[4]
  6. PrintTestCases([str(authentication), str(style), str(ip), str(accounting)])

The function PrintTestCases is in another file and use a match-case structure

  1. def PrintTestCases(authentication, style, ip, accounting):
  2. match accounting:
  3. case "NOACC":
  4. match [authentication, style, ip]:
  5. case ['NONE', 'NONE', 'NONE']:
  6. print("Test Case Number 1")
  7. case _:
  8. print("Wrong Test Case")
  9. case "ACC":
  10. match [authentication, style, ip]:
  11. case ['PRIVATE', 'NONE', 'NONE']:
  12. print( "Test Case Number 2")
  13. case _:
  14. print("Wrong Test Case")

Then I call the main script from a Jenkins pipeline like this

  1. python3 -u main NONE NONE NONE ACC

But I always get this error

  1. PrintTestCases() missing 3 required positional arguments: 'style', 'ip', and 'accounting'

Based on my understanding, I am passing the argument to the function via the sys.argv
Why the function does not see the required arguments?

答案1

得分: 3

您正在传递一个包含所有参数的列表。
注意这两者之间的区别。

  1. PrintTestCases([str(authentication), str(style), str(ip), str(accounting)])

  1. PrintTestCases(str(authentication), str(style), str(ip), str(accounting))

在第一种情况下,您正在传递一个单一的对象。请使用第二种方式。

PrintTestCases 需要 4 个不同的参数,但通过传递 [str(authentication), str(style), str(ip), str(accounting)],实际上您只将此列表作为 authentication 参数传递,其他参数未填充。

英文:

You are passing a list that contains all the arguments.
Note how these two differ.

  1. PrintTestCases([str(authentication), str(style), str(ip), str(accounting)])

and

  1. PrintTestCases(str(authentication), str(style), str(ip), str(accounting))

In the first case, you're passing a single object. Use the second.

PrintTestCases is expecting 4 distincts arguments, but by passing [str(authentication), str(style), str(ip), str(accounting)] you're actually giving this list as the authentication argument alone, with the others unfilled.

答案2

得分: 1

你正在将一个列表作为输入传递,这被视为仅为第一个输入。您应该改为将它们作为参数传递给函数。将PrintTestCases([str(authentication), str(style), str(ip), str(accounting)])替换为PrintTestCases(str(authentication), str(style), str(ip), str(accounting)),它应该可以工作。

英文:

You are passing a list as the input, which is counted as only first input. You should instead pass them as arguments to the function. Replace the PrintTestCases([str(authentication), str(style), str(ip), str(accounting)]) with PrintTestCases(str(authentication), str(style), str(ip), str(accounting)) and it should work.

答案3

得分: 1

只是因为你将一个单一的列表传递给PrintTestCases函数。移除括号,问题就会解决。

英文:

It's just because you are passing a single list to the PrintTestCases function. Remove the brackets and it will be fixed.

答案4

得分: 0

为什么不使用 argparse 处理这些参数?你可以轻松地定义所需的参数,并将其中一些设置为可选。

  1. import argparse
  2. if __name__ == "__main__":
  3. parser = argparse.ArgumentParser()
  4. parser.add_argument("authentication", help="认证方法")
  5. parser.add_argument("style", help="样式")
  6. parser.add_argument("ip", help="IP 地址")
  7. parser.add_argument("accounting", help="记账方法")
  8. args = parser.parse_args()
  9. PrintTestCases([str(args.authentication), str(args.style), str(args.ip), str(args.accounting)])
英文:

Why don't you use argparse for those args? You can define the args you need so easy and set some of them as optional if you need.

  1. import argparse
  2. if __name__ == "__main__":
  3. parser = argparse.ArgumentParser()
  4. parser.add_argument("authentication", help="The authentication method")
  5. parser.add_argument("style", help="The style")
  6. parser.add_argument("ip", help="The IP address")
  7. parser.add_argument("accounting", help="The accounting method")
  8. args = parser.parse_args()
  9. PrintTestCases([str(args.authentication), str(args.style), str(args.ip), str(args.accounting)])

huangapple
  • 本文由 发表于 2023年7月24日 18:05:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定