1

I'm working on a project where I need a company name and a related number as inputs for my script.

An example of this would be as follow: python myscript.py "Company 1" 5

The above works fine for a single company but I'd like to add multiple at the same time (not limited to 3) like this: python myscript.py "Company 1" 5 "Company 2" 9 "Company 3" 15

I've been searching but I have the feeling argparse does not support this feature and I'm not sure where to look next.

Krowi
  • 1,565
  • 3
  • 20
  • 40
  • https://stackoverflow.com/questions/15753701/how-can-i-pass-a-list-as-a-command-line-argument-with-argparse Look into the append functionality. – BlueBuffalo73 Sep 20 '21 at 17:41
  • Or if you need the pairs to be separate arguments, see [Handling indefinite paired arguments with argparse](/q/17126062/4518341), although the only answer is to do it manually. – wjandrea Sep 20 '21 at 17:46
  • @Blue Where does that cover pairs of arguments? I skimmed it but didn't see anything relevant. – wjandrea Sep 20 '21 at 17:48
  • You get most control over the parsing when using `optionals`, flagged arguments. Otherwise parsing strictly by position, and there's nothing in your input to identify "sets". To the parser your input is just a long list of strings,which it can handle with `nargs='*'`. But that's no different from parsing `sys.argv` directly. – hpaulj Sep 20 '21 at 18:12

0 Answers0