Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Is argparse the command line parameter parsing package that comes with python?

Shulou Source: shulou.com Published: 2022-06-02 09:47:10 09月27日 Update

This article mainly explains "argparse is the command line parameter parsing package that comes with python", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "is argparse the command line parameter parsing package that comes with python?"

Argparse is a command line parameter parsing package that comes with python, which can be used to easily read command line parameters.

Cat test.py

Import argparseversion = "1.0.0" parser = argparse.ArgumentParser (description= "BestYunYan- WebSocket-based fortress machine"); # description describes what this parameter parser does, which is printed first when-- help. # usage: string describing program usage (default: generated from parser parameters) parser.add_argument ('--port','-p',type=int,default=8000,help= "server port, default is 8000"); # add parameter setting parser.add_argument ('--host','-H',default= "0.0.0.0", help=' server IP, default is 0.0.0.0') # add parameter settings args = parser.parse_args () print ('args:',args) # print instance # args: Namespace (host='192.168.100.0', port=222) # instantiate # print (' BestYunYan- WebSocket-based fortress machine is running.') print ("current version [version]: {}\ nHost IP [host]: {}\ nHost port [port]: {}" .format (version,args.host) Args.port) # print ("current version [version]:% s\ nHost IP [host]:% s\ nHost Port [port]:% d"% (version,args.host,args.port)) # get the incoming data from printing

# encoding:utf-8import argparse# is used to pass the parameter if _ _ name__ ='_ _ main__': parser=argparse.ArgumentParser () parser.add_argument ('- Haft _ main__': parser=argparse.ArgumentParser () parser.add_argument) # configure the passed parameter,-H indicates that the passed parameter is-Handicap 192.154.23.1' or-- host='192.167.1.1',help is the help message Default is the default value of parser.add_argument ('- type type is the type that passes parameters). Parser.add_argument means that every parameter passed must be a string, and nargs='+' means at least one parameter must be passed. In this case, the passed parameters will be displayed as a list. By default, it must be set to the list parser.add_argument ('- axiomagery copyright conversation plugins2), which means that any number of parameters can be passed, and it can be 0. Nargs='*' indicates 0 or more, and + indicates that at least one must be passed. Parser.add_argument # indicates that the parameter passed is of bool type, and the ture/faluse type # is False by default. Note that if you use unwritten-v, it returns False, and if you add the-v parameter, it returns True args=parser.parse_args () print (args). The runtime parameter is passed using spaces. Cannot use = pass # use the default value [root@bestyunyan tmp] # python3 1.pyNamespace (host='127.0.0.1', plugins= ['01'], plugins2= [], port=9999, version=False) [root@bestyunyan tmp] # python3 1.py-H' 127.0.0.1'- p 8888-I 12 3 4-An a b-vNamespace when running python 1.py (host='127.0.0.1', plugins= ['1,'2,'3,'4'] Plugins2= ['averse,' b'], port=8888, version=True) [root@bestyunyan tmp] # python3 1.py-H '127.0.0.1'-p 8888-I 1234-An a bNamespace (host='127.0.0.1', plugins= [' 1','2','3','4'], plugins2= ['A','b'], port=8888 Version='False') # encoding:utf-8import argparse# command parameter is passed if _ _ name__ = ='_ _ main__': parser=argparse.ArgumentParser () parser.add_argument ('- Hanzhiko pachymakyo) parser.add_argument ('- vakety 'service IP'' service port') parser.add_argument ('- vakety language') Action='store_true', help='DEBUG INFO') args=parser.parse_args () print (args.host) print (args.port) print (args.verbose) so far I believe you have a deeper understanding of "argparse is the command line parameter parsing package that comes with python", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Parameters commands hosts ports services types content fortresses characters strings instances servers versions learning running practical deeper information interests multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker Linux Apple vpn