1
parser = argparse.ArgumentParser(description='Run the app')
skill_list = utils.read_yaml_file('skill_list.yml')
parser.add_argument('skill', choices=skill_list, help="Which skill?")

parser.add_argument('endpoints', default=None, help="Configuration file for the connectors as a yml file")

skill = parser.parse_args().skill
endpoints = parser.parse_args().endpoints 

In the above code, I can pass two parameters to as follows:

run.py joke endpoints.yml

If my 'skill' is a variable list, meaning that I don't know how much arguments users might pass. In such a case, I can do:

run.py joke command weather endpoints.yml

Here "joke command weather" will be passed by the 'skill' argument. How can I do that?

marlon
  • 6,029
  • 8
  • 42
  • 76
  • Possible duplicate of [Use of \*args and \*\*kwargs](https://stackoverflow.com/questions/3394835/use-of-args-and-kwargs) – Mason Caiby May 17 '19 at 22:44
  • @MasonCaiby how to use args with the "add_argument" method? – marlon May 17 '19 at 22:48
  • Can you use an option like `-e endpoints.yml` or does it need to be always the last argument? – solarc May 17 '19 at 22:52
  • Ah, I see... can you use this? `parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator')` see the argparse docs: https://docs.python.org/2/library/argparse.html – Mason Caiby May 17 '19 at 22:53

2 Answers2

1

It is preferable to use -foo or --foo for your endpoints. But If u want exactly same passing order of arguments. This should work

parser.add_argument('skill', choices=skill_list, help="Which skill?", nargs='+')

parser.add_argument('endpoints', default=None, help="Configuration file for the connectors as a yml file")
Akhil Batra
  • 581
  • 4
  • 16
0

You also need to provide nargs parameter in add_argument function.

parser.add_argument('--skill', nargs='+', help='List of skills', required=True)
parser.add_argument('--endpoints', default=None, help="Configuration file for the connectors as a yml file", required=True)

# Usage
# python run.py --skill joke command weather --endpoints endpoints.yml
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156