2

How do I call the following functions below based on user input?

For example, if they type:

python test.py cmd1 -n hostname

  • it will call def cmd1 and pass the -n parameter (hostname) to function

    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()
    
python_newbie
  • 111
  • 1
  • 9

0 Answers0