0

I want to give command line inputs to my script. which has input arguments as :

  1. my script.py
  2. mode name (which is id)
  3. id numbers ( passed by ',' separations first command, in which I would pass multiple ids through command line myscript.py id id1,id2,id3...

second command, where I would a '-f' flag at the end which denotes force and it must do that operation.

myscript.py id 123,21,1,900 -f

My question is, how should I read in and use these arguments in the script? Thank you !

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Anushka
  • 175
  • 1
  • 1
  • 7

1 Answers1

0

You can get argument from sys library.
Basic implementation

import sys

print(sys.argv)
if "-f" in sys.argv:
    print("force")
print(sys.argv[1], "=", sys.argv[2])
python file.py id id1,id2,id3 -f

> ['.\\file.py', 'id', 'id1,id2,id3', '-f']
> force
> id = id1,id2,id3

If you want as dictionary format you can view this

GodWin1100
  • 1,380
  • 1
  • 6
  • 14