python - argparse,创建如何在 python 中循环我的命令行?
问题描述
这是代码,函数 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
解决方案
如果你添加default=1
for-repeate
那么你可以运行循环
使用if
而不是elif
在一行中运行参数的所有函数。
但它们将始终按顺序执行write
,,,sleep
。local
要控制顺序,您可能必须更改所有参数。也许这可以帮助argparse 参数顺序
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, default=1, help='--Ex--')
A = parser.parse_args()
for x in range(A.r):
if A.w:
P.typewrite(A.w.replace("_"," "), 0.25)
if A.s: # use `if` instead of `elif`
time.sleep(A.s[0])
if A.l: # use `if` instead of `elif`
co = P.locateOnScreen(A.l[0])
print(f"{co}")
编辑:
您可以使用带有参数的列表
parse_args(["-write", "HELLO_WORLD", "-s", "2", "-l", "image.PNG", "-r", "2"])
所以你可以改变你的脚本像以前一样运行
class Automate:
def classify(self, arguments):
# ... code ...
A = parser.parse_args(arguments)
# ... code ...
if __name__ == "__main__":
B = Automate()
B.classify(sys.argv)
你也可以在其他脚本中使用它
from aut import Automate
B = Automate()
B.classify(["-write", "HELLO_WORLD", "-s", "2", "-l", "image.PNG", "-r", "2"])
B.classify(["-s", "2", "-w", "HELLO_WORLD", "-repeat", "3", "-l", "image.PNG"])
B.classify(["-r", "4", "-l", "image.PNG", "-w", "HELLO_WORLD", "-sleep", "2"])
或者split(" ")
如果您不在参数中使用空格,则使用
from aut import Automate
B = Automate()
B.classify("-write HELLO_WORLD -s 2 -l image.PNG -r 2".split(" "))
B.classify("-s 2 -w HELLO_WORLD -repeat 3 -l image.PNG".split(" "))
B.classify("-r 4 -l image.PNG -w HELLO_WORLD -sleep 2".split(" "))
或者简单地创建.bat
文件
python aut.py -write HELLO_WORLD -s 2 -l image.PNG -r 2 #times
python aut.py -s 2 -w HELLO_WORLD -repeat 3 -l image.PNG #times
python aut.py -r 4 -l image.PNG -w HELLO_WORLD -sleep 2 #times
推荐阅读
- python - 如何在没有 tts 读取的情况下使用 discord.py 发送消息?
- java - 使用 IntentService 和 PendingIntent 接收后台位置更新
- redis - 将 redis-rs 与 actix-web 一起使用
- mysql - 遵循说明后未创建 mysql docker 容器
- reactjs - 在 SetState 中响应循环
- javascript - 我是否必须加载 60 种字体才能在网站中支持 60 种语言?
- javascript - const funcName = (args) => { }; 有什么区别?和 const funcName = (args) => ( );?
- ios - 如何在选择器参数Swift之后添加函数参数
- php - PHP反转一个字节并打印它
- c++ - 如果派生类对象调用基类函数,会修改哪个类的静态变量?