【问题标题】:Python argparse combine commands and command argumentsPython argparse 结合命令和命令参数
【发布时间】:2018-01-25 21:36:22
【问题描述】:

我定义了两个命令(为简洁起见,我省略了解析器/子解析器部分,因为我认为不需要它们来回答问题)。

#COMMAND ARGS: Add Arguments to the final command "sacs am run [args]"
am_run_parser.add_argument("-s", action='store_const', const='s', dest='s', help="Output statistical reporting to the console.")
am_run_parser.add_argument("-S", action='store_const', const='S', dest='S', help="Output statistical reporting to the standard archive.")
am_run_parser.add_argument("-t", action='store_const', const='t', dest='t', help="Output timing reporting to the console.")
am_run_parser.add_argument("-T", action='store_const', const='T', dest='T', help="Output timing reporting to the standard archive.")
am_run_parser.add_argument("-R", action='store_const', const='R', dest='R', help="Output reject reporting to the standard archive.")
am_run_parser.add_argument("-b", "--bulk", type=int, nargs=1, help="MySQL Bulk Insert Quantity. Currently the default is set at " + str(defaults.bulk) + ". Theoretically, changing this value can affect how quickly inserts are performed.")
am_run_parser.add_argument("-host", nargs=1, help="The MySQL server host to output to.")
am_run_parser.add_argument("-port", nargs=1, help="The MySQL server port to output to.")
am_run_parser.add_argument("-user", nargs=1, help="The MySQL server username to use.")
am_run_parser.add_argument("-pw", nargs=1, help="The MySQL server password to use.")
am_run_parser.add_argument("-db", nargs=1, help="The MySQL server database to use.")
am_run_parser.set_defaults(func=am_cli_tools.am_run)

#COMMAND ARGS: Add Arguments to the final command "sacs am get [args]"
am_get_parser.add_argument("-a", action='store_const', const='a', dest='a', help="Get source A data for sacs am processing.")
am_get_parser.add_argument("-b", action='store_const', const='b', dest='b', help="Get source B data for sacs am processing.")
am_get_parser.add_argument("-c", action='store_const', const='c', dest='c', help="Get source C data for sacs am processing.")
am_get_parser.add_argument("-r", action='store_const', const='r', dest='r', help="Get source D data for sacs am processing.")
am_get_parser.add_argument("-t", action='store_const', const='t', dest='t', help="Get source E data for sacs am processing.")
am_get_parser.add_argument("-R", action='store_const', const='t', dest='t', help="Run the sacs am processor using the arguments specified after the run_args flag. This will perform the 'sacs am run' command after all specified sources are gathered (review the help file for this command for additional information about argument options).")
am_get_parser.set_defaults(func=am_cli_tools.am_get)

如您所见,我正在将 -R 参数添加到 sacs am get 命令。基本上,我希望能够在 get 完成后告诉 sacs am get 运行 sacs am run 命令,并且我想公开 sacs am run 的所有相关参数。例如sacs am get -abcrtR -run_args -sStTRb -host www.google.com -port 22 -user IBUser -pw IBpass -db IBpoken(lol)。

当然,这些 run_args 只适用于 -R 参数。

我怀疑有更好的方法来解决这个问题。所有选项都摆在桌面上。这些命令在运行时确实需要发送电子邮件(通常通过 cron 作业);这是我希望选择组合命令的主要原因之一,因为如果我不这样做,我将不得不发送 2 封电子邮件而不是一封,因为这些命令必须单独运行。我想我可以做一些其他的事情,比如将它写入/附加到一个文件中,并创建另一个 sac am sendemail 命令让 cron 运行......但这种方法会使事情变得更加有序和简洁。

【问题讨论】:

  • 第三个命令sacs am full 怎么样,它包含getrun 的所有参数?您可以组织字典中的所有参数,并将它们循环添加到适当的解析器中,以避免重复。
  • 我曾考虑添加 sacs am getrun,然后让相关函数调用 am_get 和 am_run;我还会在原始的 2 个命令中添加一个 -e 参数以选择性地发送电子邮件,以防用户在单独运行这些命令时仍然想要。我在该方法中看到的唯一真正问题是我目前有一些参数重叠,并且需要为新命令更改一些字母,这可能会使用户感到困惑,如果他们使用他们习惯的东西而不是帮助文件说,它可能会导致一些真正的问题......变量名是理想的。
  • 如果参数重叠会导致真正的问题,很容易通过重命名重叠版本的两个版本来避免它们(例如,使用前缀--get-x)。他们会因为使用错误的参数而得到错误,并且没有使用错误参数运行的风险。当然,这并不能解决用户体验问题,尽管他们将始终使用新界面。

标签: python arguments argparse


【解决方案1】:

停止追求完美,让这该死的东西只发送两封电子邮件。

【讨论】:

    猜你喜欢
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2020-05-04
    • 2013-07-10
    • 2018-12-03
    • 2012-03-19
    相关资源
    最近更新 更多