I am making a program in which the click
module is used to parse the command line arguments.
The -r
and -t
parameters should be passed to the first function and -f
and -l
to the second function.
I gave the following inputs through command line arguments, but it is not accepting inputs for the second function and giving the error like "extra arguments".
How can I use the click
module with multiple functions?
Input:
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()
You should use @click
with function which gets all arguments and later it runs LoadMemory(mempercent, timesec)
and 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()