argparse

cmd.py:

# 导入argparse包
import argparse
import math

# 创建ArgumentParser实例,设置解析器
parser = argparse.ArgumentParser(description='Find the sum of two parameters',)

# 添加参数项,默认类型为str
parser.add_argument('-r', '--radius', type=int, metavar='', required=True, help='Radius of Cylinder')
parser.add_argument('-H', '--heights', type=int, metavar='', required=True, help='Height of Cylinder')

args = parser.parse_args()

def cylinder_volume(radius, heights):
    vol = math.pi * (radius ** 2) * heights
    return vol
    
if __name__ == "__main__":
    volume = cylinder_volume(args.radius, args.heights)
    print(volume)

  命令行:

python cmd.py --help

  结果:

 

 

python cmd.py -r 1 -H 2

  结果:

 

posted @ 2020-06-28 11:56  耐烦不急  阅读(207)  评论(0编辑  收藏  举报