Ciro
Ciro

Reputation: 41

Missing 3 required positional argument with 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?

Upvotes: 0

Views: 162

Answers (4)

Biskweet
Biskweet

Reputation: 139

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 distinct 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.

Upvotes: 3

Ivan Corcoles
Ivan Corcoles

Reputation: 21

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)])

Upvotes: 0

Sadegh Moayedizadeh
Sadegh Moayedizadeh

Reputation: 116

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

Upvotes: 1

Lemon Reddy
Lemon Reddy

Reputation: 637

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.

Upvotes: 1

Related Questions