parse_args() 的功能是什么,我需要在哪里传递参数

What is the function of parse_args() and where do i need to pass the argument

这是我的代码,

我正在尝试学习 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 将设置为高度。