Python argparse结合了命令和命令参数

2024-09-27 21:30:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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

#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)

如您所见,我正在向sacs am get命令添加-R参数。基本上,我希望能够告诉sacs am get在get完成后运行sacs am run命令,并公开sacs am run的所有相关参数。e、 g.sacs am get-abcrtR-运行参数-sStTRb-hostwww.google.com-端口22-用户IBUser-pw IBpass-db IBpoken(lol)。在

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

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


Tags: thetostorerun命令addparserget

热门问题