-2

I copied this code from Github, but its not working to me.

Sample Code:

ap = argparse.ArgumentParser()
ap.add_argument("-s", "--source", required=True, help="Path to the source of shapes")
ap.add_argument("-t", "--target", required=True, help="Path to the target image")
args = vars(ap.parse_args())

and this output

usage: detect_leaf.py [-h] -s SOURCE -t TARGET
detect_leaf.py: error: argument -s/--source is required

please help me. thanks

2 Answers2

0

The error message in your question title is pretty obvious.

ap.add_argument("-s", "--source", required=True, help="Path to the source of shapes")

You put required=True on this parameter. That means it's required. So, if you try to run this script, and don't put a --source (or -s) argument on the command line, you will get an error.

If you don't want it to be required, don't put required=True.


The output you show at the end of the question, on the other hand, cannot possibly come from this code. There is no -src or -trg parameter in your argparse spec. Maybe you're running a completely different program? If so, we can't debug that program by seeing the code to this one.

abarnert
  • 354,177
  • 51
  • 601
  • 671
-1

I think you didn't put the argument -s in the python excute command. Assuming the python code is saved into a file called detect_leaf.py You have to put the argument -s like below

python detect_leaf.py -s SOURCE -t TARGET

and There are two ways to access arguments value like below

import argparse
ap = argparse.ArgumentParser(description='Process some integers.')
ap.add_argument("-s", "--source", required=True,  help="Path to the source of shapes")
ap.add_argument("-t", "--target", required=True,  help="Path to the target image")

#It's Dictionary
vars_args = vars(ap.parse_args())

print(vars_args['source'])
print(vars_args['target'])

#It's NameSpace object
parsed_args = ap.parse_args()

print(parsed_args.source)
print(parsed_args.target)
yoon
  • 30
  • 3