当前位置:   article > 正文

【VSCode】支持argparser/接受命令行参数_argparse vscode

argparse vscode

问题

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",
            ],
        }
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

(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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(3) 输出结果

Alice 18
  • 1
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号