SoFunction
Updated on 2024-11-10

Detailed summary of python's argpare and click modules

I. argparse module

1. Module description

# argparse is a module in python's standard library for parsing command-line arguments, replacing the obsolete optparse module. argparse parses arguments as they are defined in the program.

# and automatically generates help and usage information

2. Commonly used parameters of the module

# Parameter Description:
# name/flag: name of the parameter
# action: the action that encounters the parameter, the default value is store
# nargs: the number of parameters, can be a specific number, or + or *, * means 0 or more parameters, + sign means 1 or more parameters
# default:Default value when no parameters are specified.
# type:# The type of the parameter
# choice: the value allowed for the parameter
# required:Optional parameter can be omitted or not
# help: Help information for the parameter
# dest:name of the parsed parameter


3. Usage

import argparse
def _argparse():
 parseobj = (description="This is script help")
 # Parameter Description:
 # name/flag: name of the parameter
 # action: the action that encounters the parameter, the default value is store
 # nargs: the number of parameters, can be a specific number, or + or *, * means 0 or more parameters, + sign means 1 or more parameters
 # default:Default value when no parameters are specified.
 # type:# The type of the parameter
 # choice: the value allowed for the parameter
 # required:Optional parameter can be omitted or not
 # help: Help information for the parameter
 # dest:name of the parsed parameter
 parseobj.add_argument("--host",action='store',dest='host',required=True,default="127",help="This is a host ip address",type=int)

 parseobj.add_argument("--P",'--passwd',action='store', dest='pwd', required=True, default="admin123.",help="This is a host password", type=str)
 parseobj.add_argument("--V", '--version', action='version', version="%(prog)s 0.1")
 return parseobj.parse_args()
if __name__ == '__main__':
 res = _argparse()
 print()
 print()

4, finally we test the module

a. Test the -h option, here -h and --help have the same effect

b. Testing the --V option and the --version option

c. Test the correct parameters entered

II. click module

1. Introduction to the module

The author of the click module is the author of Flask, (Armin Ronacher) developed a third-party module for quickly creating command lines. It works the same way as argparse in the python standard library, but with the exception that

It's much easier to use. click is the argparse of the standard library, just as the requests library is the urllib equivalent of the standard library. click is a third library, so you need to install it before you can use it.

2、Module Installation

E:\python3\Scripts>pip3. install click

3. Steps of use

a. Use @() to decorate a function to make it an interface to the command line

b. Use decorative functions such as @(), add command line options to them, etc.

c. First look at an official example

import click

 

The # click module is a third-party module developed by the author of Flask (Armin Ronacher) to quickly create command lines. It works the same way as argparse in the python standard library, but with the exception that

# Use more simple, click relative to the standard library argparse, just as the requests library is equivalent to the standard library urllib library, click is a third library, so before using the need to install the

 

@()

@('--count',default=1,help='Number of greetings')

@('--name',prompt='your name',help='The person to greet')

def hello(count,name):

 for x in range(count):

  ("hello {name}".format(name = name))

 

if __name__ == '__main__':

 hello()

The rest should be readable by everyone, what this prompt does is actually if we don't pass a parameter for name, he will give a prompt

The following example is the full pass parameter

4. Commonly used parameters

Common Parameters

default: set the default value of the command line parameter

help: parameter description

type: the type of the parameter, it can be string, int, float.

prompt: when no corresponding parameter is entered on the command line, the user will be prompted for input based on prompt.

nargs: specifies the number of values accepted by the command line arguments

a. Test the nargs parameter

@()
@('--post',nargs=2,help='Number of post')
def hello(post):
 print(post)

if __name__ == '__main__':
 hello()

Test results

b. Testing options

@()
@('--hash',type=(["md5","sha1"]),help='type of hash')
def hello(hash):
 print(hash)
if __name__ == '__main__':
 hello()

Test results

c. If you use the command line to enter a password, the default situation is a big security risk, because the command to enter the password in the history, other users will be able to pass the history list of commands, to get our passwords, click can be a solution to this problem for us!

@()
@('--pwd',prompt=True,hide_input=True,help='passwd of user',confirmation_prompt=True)
def hello(pwd):
 print(pwd)
if __name__ == '__main__':
 hello()

prompt: to be set to True

hide_input: to be set to True

confirmation_prompt: automatically performs a second verification of the password for us

The test results are as follows

Incorrect input

Correct input

This is the whole content of this article.