如何根据用户输入调用以下功能?
例如,如果他们键入:
python test.py cmd1 -n主机名
它将调用def cmd1并将-n参数(主机名)传递给函数
import argparse
def cmd1(node):
print('cmd1')
def cmd1_option1():
print('cmd2')
def cmd1_option1(template):
print('cmd3')
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='Functions')
command_1 = subparsers.add_parser('cmd1', help='...')
command_1.add_argument('-n', "--node", type=str, help='...')
command_2 = subparsers.add_parser('cmd2', help='...')
command_3 = subparsers.add_parser('cmd3', help='...')
command_3.add_argument('-t', "--template", type=int, help='...')
args = parser.parse_args()