Home > OS >  Getting TypeError when passing and setting command line argument in python
Getting TypeError when passing and setting command line argument in python

Time:06-20

Okay so I want to add command-line arguments for four different files I have done it by the following code. (suggest if I have done anything wrong here)

def parse_opt(known=False):
    parser = argparse.ArgumentParser()
    parser.add_argument(default=ROOT / 'courses.csv', help='path to course.csv file')
    parser.add_argument(default=ROOT / 'students.csv', help='path to students.csv file')
    parser.add_argument(default=ROOT / 'tests.csv', help='path to tests.csv file')
    parser.add_argument(default=ROOT / 'marks.csv', help='path to marks.csv file')
    parser.add_argument(default=ROOT / 'output.json', help='path to output.json file')
    
    return parser

next I'm just testing it by calling the function as mentioned below:

opt=parse_opt()    
#print(opt)
#json_data=prepareJsonData(opt)
#makeFile(json_data)

And I'm getting this error

 python readFile.py 'C:/Users/Mudassir/Downloads/Example1/Example1/courses.csv','C:/Users/Mudassir/Downloads/Example1/Example1/students.csv','C:/Users/Mudassir/Downloads/Example1/Example1/tests.csv','C:/Users/Mudassir/Downloads/Example1/Example1/marks.csv', C:\Users\Mudassir\Desktop\hatchways\output.json
Traceback (most recent call last):
  File "C:\Users\Mudassir\Desktop\hatchways\readFile.py", line 115, in <module>
    opt=parse_opt()
  File "C:\Users\Mudassir\Desktop\hatchways\readFile.py", line 107, in parse_opt
    parser.add_argument(default=ROOT / 'courses.csv', help='path to course.csv file')
  File "C:\Program Files\Python39\lib\argparse.py", line 1398, in add_argument
    kwargs = self._get_positional_kwargs(*args, **kwargs)
TypeError: _get_positional_kwargs() missing 1 required positional argument: 'dest'

Any idea what I'm doing wrong here?

I tried searching internet but not much info is available there.

CodePudding user response:

you are missing the nargs parameter from the add_argument call.

something like the following should give you optional positional command line arguments.

def parse_opt(known=False):
    parser = argparse.ArgumentParser()
    parser.add_argument('c', nargs='?', default='courses.csv', help='path to course.csv file')
    parser.add_argument('s', nargs='?', default='students.csv', help='path to students.csv file')
    parser.add_argument('t', nargs='?', default='tests.csv', help='path to tests.csv file')
    parser.add_argument('m', nargs='?', default='marks.csv', help='path to marks.csv file')
    parser.add_argument('o', nargs='?', default='output.json', help='path to output.json file')

    return parser

secondly, when making the command line call, you should not use commas to separate the command line arguments, instead:

python readFile.py /c/courses.tsv

the output for printing the args after making a call t parser.parse_args() is:

Namespace(c='C:/courses.tsv', m='marks.csv', o='output.json', s='students.csv', t='tests.csv')

notice how it only took my first supplied argument, and defaulted the rest to values provided in the add_argument call.

  • Related