【发布时间】:2020-05-27 18:44:01
【问题描述】:
这是代码,函数 r 传递了 1 个参数 int。
import pyautogui as P
from colorama import *
import sys
import os
import time
import argparse
init(autoreset=True)
class Automate:
def classify(self):
parser = argparse.ArgumentParser(description='test')
parser.add_argument('-w', '-write', type=str, nargs=1, help='--Ex--')
parser.add_argument('-s', '-sleep', type=int, nargs=1 , help='--Ex--')
parser.add_argument('-l', '-local', type=str, nargs=1, help='--Ex--')
parser.add_argument('-r', '-repeat', type=int, help='--Ex--')
A = parser.parse_args()
if A.w:
P.typewrite(A.w.replace("_"," "), 0.25)
elif A.s:
time.sleep(A.s[0])
elif A.l:
co = P.locateOnScreen(A.l[0])
print(f"{co}")
elif A.r:
pass
B = Automate()
B.classify()
帮助我获取 r 命令以重复您通过命令行输入的命令(命令将根据用户的需要而有所不同),而不会创建无限循环。感谢您的关注和帮助。
F:\>python aut.py -write HELLO_WORLD -s 2 -l image.PNG -r 2 #times
F:\>python aut.py -s 2 -w HELLO_WORLD -repeat 3 -l image.PNG #times
F:\>python aut.py -r 4 -l image.PNG -w HELLO_WORLD -sleep 2 #times
【问题讨论】:
-
我不明白你想做什么,但如果你想重复一些事情,那么使用
for-loop 或者range(A.r)- 即。for x in range(A.r):?也许使用default=1代替--repeate然后你可以运行for-loop,即使你不使用-repeate,因为它的价值是1 -
简单,每个命令都有不同的开销,所以 -r 必须多次重复该任务。
Ex: input: -w Hello_World -r 2output: Hello_World Hello_World -
在其他测试之前放置一个循环,由
A.r值控制。for i in range(A.r): <do the rest>。你可能应该给-r一个default=1。 -
我会试试的,另一个问题我做了测试,但是当放置几个命令时,显然只有一个被无缘无故地执行。有没有办法逐行执行我通过的所有命令
标签: python python-3.x argparse