2017-08-17 117 views
0

我有两个定义的命令(为简洁起见,我省略了解析器/子分析器部分,因为我认为他们不需要回答问题)。Python argparse组合命令和命令参数

#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 -abcrt R -run_args -sStTRb -host www.google.com -port 22 -user IBUser -pw IBpass -db IBpoken(lol)。

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

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

+0

第三个命令'sacs am full'具有get'和'run'的所有参数吗?您可以组织字典中的所有参数,并将它们添加到循环中的适当分析器中,以避免重复。 –

+0

我曾考虑过将gesun加入囊中,然后让相关函数调用am_get和am_run;我还会为原来的2个命令添加-e参数,以便有选择地发送电子邮件,以防止用户在单独运行这些电子邮件时仍想要。我用这种方法看到的唯一真正的问题是,我目前有一些参数重叠,需要更改一些字母作为新命令,这可能会混淆用户,如果他们使用了他们习惯的而不是帮助文件说,它可能会导致一些真正的问题......变量名称是理想的。 – gunslingor

+0

如果参数重叠可能导致真正的问题,通过重命名重叠的两个版本(例如,使用前缀“--get-x”)很容易避免它们。他们会因为使用错误的参数而出错,并且没有使用错误的参数运行的风险。当然,这并不能解决用户体验的问题,尽管它们将始终使用新的界面。 –

回答

0

停止寻求完美,让困难的事情只是发送电子邮件。