Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use argparse Library in Python

Shulou Source: shulou.com Published: 2022-06-02 08:14:00 10月29日 Update

This article shows you how to use the argparse library in Python, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Argsparse is the standard module for command line parsing of python.

Code

Import argparseparser = argparse.ArgumentParser (a number is passed in the description=' command line') parser.add_argument ("square", help= "Please enter a number", type=int) parser.add_argument ("input_str", help= "Please enter a letter", type=str) args = parser.parse_args () print (args.square * * 2) print (args.input_str) # parser = argparse.ArgumentParser (multiple digits in description=' command line') # parser.add_argument ("more_int") Help= "Please enter a letter", nargs='+', type=int) # args= parser.parse_args () # print (args.more_int) # whether setting parameter is required required=True# setting multiple parameters nargs='+'# setting parameter default value default='ha'

Execute:

Python3 test2.py 5k 25k

The above is how to use the argparse library in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Parameters commands numbers input content multiple letters skills knowledge concise concise code that is articles more standards modules articles industries information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Xiaomi Shulou Information Shulou Technology