Python 3.x parse_args()的函数是什么?我需要在哪里传递参数

Python 3.x parse_args()的函数是什么?我需要在哪里传递参数,python-3.x,argparse,Python 3.x,Argparse,这是我的密码 我正在学习argparse,有人能给我解释一下代码吗 # enter code here import argparse parser= argparse.ArgumentParser() parser.add_argument("radius",type=int,help="radius") parser.add_argument("height",type=int,help="height") args=parser.parse_args() def add(radius,he

这是我的密码

我正在学习argparse,有人能给我解释一下代码吗

# enter code here
import argparse
parser= argparse.ArgumentParser()
parser.add_argument("radius",type=int,help="radius")
parser.add_argument("height",type=int,help="height")
args=parser.parse_args()
def add(radius,height):
   return radius+height
so=add(args.radius,args.height)
print("the sum is",so)
**
#this was the output
#usage: arg_parsedemo.py [-h] radius height
#arg_parsedemo.py: error: the following arguments are required:
#radius,height

parse_args将接受您在运行程序时在命令行上提供的参数,并根据您添加到ArgumentParser对象的参数对其进行解释

您已经在解析器中添加了两种参数类型,半径和高度,它们是位置型的,因为在它们的名称上没有包含“-”,这意味着您需要在运行程序时提供它们。为了运行您的程序,您需要按如下方式运行: python arg_parsedemo.py 50 100


50将设置为半径,100将设置为高度。

您是如何调用此脚本的?