问题
Python的argparser的功能非常强大,在复杂工程项目中应用十分广泛。vscode是当前最流行的IDE开发环境,那么如何在vscode中配置argparser呢?
方法
(1) 修改.vscode的launch.json文件
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": true, "args": [ "--name","Alice", "--age", "18", ], } ] }
(2) argparser中编程
from argparse import ArgumentParser parser = ArgumentParser(description='test argparser') parser.add_argument('--name', type=str, default='name', help='please specify your name in cmd') parser.add_argument('--age', type=int, default=18, help='please specify your age in cmd') arg = parser.parse_args() print(arg.name, arg.age)
(3) 输出结果
Alice 18