Home > Mobile >  in python how to count the number of arguments passed after a specific argument?
in python how to count the number of arguments passed after a specific argument?

Time:02-08

in python how to count the number of arguments passed after a specific argument ?

Explanation :

[user]$ command1 -h 192.168.1.1 -p 8888 -t 50 -u -c ...etc  main.py arg1 arg2 arg3

here when using sys.argv in main.py the number of arguments is 12 ( or more, it may differ every time )

i want to count only arguments after main.py so only : arg1 arg2 arg3 which is "3" .

Any solution ?

Thanks.

CodePudding user response:

You could get the index of main.py within the sys.argv, and then slice accordingly:

main_index = sys.argv.index('main.py')
arguments_after_main = sys.argv[main_index:] # Should contain only arg1 arg2 arg3
  •  Tags:  
  • Related