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

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

Missing 3 required positional argument with sys.argv[]

问题

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

import sys

authentication = sys.argv[1]
style = sys.argv[2]
ip = sys.argv[3]
accounting = sys.argv[4]

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

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

def PrintTestCases(authentication, style, ip, accounting):
    match accounting:
        case "NOACC":
            match [authentication, style, ip]:
               case ['NONE', 'NONE', 'NONE']:
                    print("测试用例编号1")
                case _:
                        print("错误的测试用例")
        case "ACC":
              match [authentication, style, ip]:
                case ['PRIVATE', 'NONE', 'NONE']:
                    print("测试用例编号2")
                case _:
                        print("错误的测试用例")

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

python3 -u main NONE NONE NONE ACC

但我总是得到这个错误:

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:

import sys

authentication = sys.argv[1]
style = sys.argv[2]
ip = sys.argv[3]
accounting = sys.argv[4]

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

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

def PrintTestCases(authentication, style, ip, accounting):
    match accounting:
        case "NOACC":
            match [authentication, style, ip]:
               case ['NONE', 'NONE', 'NONE']:
                    print("Test Case Number 1")
                case _:
                        print("Wrong Test Case")
        case "ACC":
              match [authentication, style, ip]:
                case ['PRIVATE', 'NONE', 'NONE']:
                    print( "Test Case Number 2")
                case _:
                        print("Wrong Test Case")

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

python3 -u main NONE NONE NONE ACC

But I always get this error

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

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

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

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.

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

and

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 处理这些参数?你可以轻松地定义所需的参数,并将其中一些设置为可选。

import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("authentication", help="认证方法")
    parser.add_argument("style", help="样式")
    parser.add_argument("ip", help="IP 地址")
    parser.add_argument("accounting", help="记账方法")
    args = parser.parse_args()
    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.

import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("authentication", help="The authentication method")
    parser.add_argument("style", help="The style")
    parser.add_argument("ip", help="The IP address")
    parser.add_argument("accounting", help="The accounting method")
    args = parser.parse_args()
    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:

确定