修改命令行参数解析示例 (aTB_ parse.py),解析命令行参数所输入的长和宽的值,计算并输出长方形的周长和面积。arB_parse.py 文件的代码如下:
时间: 2023-07-14 21:13:34 浏览: 56
```python
import argparse
parser = argparse.ArgumentParser(description='Calculate the perimeter and area of a rectangle.')
parser.add_argument('-l', '--length', type=float, metavar='', required=True, help='the length of the rectangle')
parser.add_argument('-w', '--width', type=float, metavar='', required=True, help='the width of the rectangle')
args = parser.parse_args()
perimeter = 2 * (args.length + args.width)
area = args.length * args.width
print(f"The perimeter of the rectangle is {perimeter:.2f}")
print(f"The area of the rectangle is {area:.2f}")
```
运行示例:
```bash
python arB_parse.py -l 5 -w 3
```
输出:
```
The perimeter of the rectangle is 16.00
The area of the rectangle is 15.00
```