【问题标题】:argparse, create how to loop my command line in python?argparse,创建如何在 python 中循环我的命令行?
【发布时间】: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


【解决方案1】:

如果你为-repeate 添加default=1 那么你可以运行循环

使用if 而不是elif 在一行中运行所有参数函数。

但它们将始终按writesleeplocal 的顺序执行。要控制顺序,您可能必须更改所有参数。也许这可以帮助argparse argument order

  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

【讨论】:

  • 谢谢你的回答解决了我的主要问题,既然你在这里,我想问你另一种情况,那就是只有一个命令正在执行,而不是我如何链接它们:例如@ 987654337@ 只需运行-write。我正在阅读文档,但找不到解决方案。谢谢
  • 使用if而不是elif
猜你喜欢
  • 2014-06-06
  • 2020-09-16
  • 1970-01-01
  • 2016-02-19
  • 2018-11-29
  • 2018-01-05
  • 2011-09-12
  • 1970-01-01
  • 2014-12-06
相关资源
最近更新 更多