擅长:python、mysql、java
<p>我是这样做的。这正是我想要的。你知道吗</p>
<pre><code>if os.path.isdir(TOOLS_PATH):
name_arg = next(os.walk(TOOLS_PATH))[1]
for element in name_arg:
module_pars = 'parser_'+element
module_pars = subparsers.add_parser(element, help = 'Modules from tools')
module_pars.add_argument(element, nargs='?', help=element+' description')
</code></pre>
<p>我没有测试它,因为我没有测试模块,但是<code>./jsh.py md_script</code>进入<code>elif element in args: print('md_script')</code>并打印字符串。所以看起来很管用。
谢谢你的回复。你知道吗</p>
<p>编辑:我测试过了。在add_参数中,我必须更改nargs='?'for nargs='*'以捕获多个参数。
为了从命令行捕获参数,我使用了以下命令:</p>
<pre><code>elif args:
for element in name_arg:
if element in args:
element_arg = sys.argv[2:]
done_cmd,msg = opt_exec_module(element,*element_arg)
my_logger(done_cmd,msg)
</code></pre>
<p>不是很优雅,但很管用。你知道吗</p>