使用具有多个功能的点击时出现错误 "unexpected extra arguments" 和 "got an unexpected keyword argument"

Getting errors "unexpected extra arguments" and "got an unexpected keyword argument" when using click with multiple functions

我正在编写一个程序,其中 click 模块用于解析命令行参数。

-r-t 参数应传递给第一个函数,-f-l 传递给第二个函数。

我通过命令行参数提供了以下输入,但它不接受第二个函数的输入并给出类似“额外参数”的错误。

如何使用click模块的多种功能?

输入:

PS D:\FinIQ\LoadTest> python ram.py -r 80 -t 10 --firstname john --lastname doe
Usage: ram.py [OPTIONS]
Try 'ram.py --help' for help.

Error: Got unexpected extra arguments (john doe)
TypeError: LoadMemory() got an unexpected keyword argument 'firstname'
import sys, psutil
from multiprocessing import Pool
from multiprocessing import cpu_count
from psutil import virtual_memory
import time
from functools import partial
import click

@click.command()
@click.option("--mempercent", "-r",default=70, prompt="how much ram you want to consume:" ,help="RAM %:")
@click.option("--timesec","-t", prompt="time for which ram to be consumed:",
              help="enter time in seconds")

@click.option("--firstname", "-f" , prompt="enter firstname" ,help="firstname")
@click.option("--lastname", "-l", prompt="enter lastname" ,help="lastname")


def LoadMemory(mempercent, timesec):
    currentMemory = virtual_memory().percent
    print('Current utilized memory = ', currentMemory, "%")

    while mempercent <= currentMemory:
        print('Already utilzed! ')
        print('How much memory to be stressed(%)?')
        mempercent = int(input())
    MemToFillMore = int(mempercent - currentMemory)
    new_list = [0] * 10485760 * MemToFillMore
    print('After loading memory utilized=', virtual_memory().percent, "%")
    timeint = int(timesec)
    time.sleep(timeint)
    new_list.clear()
    print('After clearing memory utilized=', virtual_memory().percent, "%")


def name(firstname, lastname):
    print("full name is:" + firstname + " " +  lastname)


if __name__ == '__main__':
    LoadMemory()
    name()

您应该将 @click 与获取所有参数的函数一起使用,然后它会运行 LoadMemory(mempercent, timesec)name(firstname, lastname)

import sys, psutil
from multiprocessing import Pool
from multiprocessing import cpu_count
from psutil import virtual_memory
import time
from functools import partial
import click

@click.command()
@click.option("--mempercent", "-r", default=70, prompt="how much ram you want to consume:", help="RAM %:")
@click.option("--timesec","-t", prompt="time for which ram to be consumed:", help="enter time in seconds")
@click.option("--firstname", "-f", prompt="enter firstname", help="firstname")
@click.option("--lastname", "-l", prompt="enter lastname", help="lastname")
def myfunction(mempercent, timesec, firstname, lastname):
    LoadMemory(mempercent, timesec)
    name(firstname, lastname)
    
def LoadMemory(mempercent, timesec):
    currentMemory = virtual_memory().percent
    print('Current utilized memory = ', currentMemory, "%")
    while mempercent <= currentMemory:
        print('Already utilzed! ')
        print('How much memory to be stressed(%)?')
        mempercent = int(input())
    MemToFillMore = int(mempercent - currentMemory)
    new_list = [0] * 10485760 * MemToFillMore
    print('After loading memory utilized=', virtual_memory().percent, "%")
    timeint = int(timesec)
    time.sleep(timeint)
    new_list.clear()
    print('After clearing memory utilized=', virtual_memory().percent, "%")

def name(firstname, lastname):
    print("full name is:" + firstname + " " +  lastname)

if __name__ == '__main__':
    myfunction()