Home > Blockchain >  How to check user has put in correct number of arguments on command line in python
How to check user has put in correct number of arguments on command line in python

Time:11-26

I'm trying to check that the user has entered two arguments on the command line - the iface name and passive for a type of scan - I thought the script would just exit if the wrong arguments entered but it still prints out the error message no matter how many arguments entered - what am I missing ?

import sys
import os

def main():
    if len(sys.argv) != 2:
         print("not enough arguments")

         sys.exit(1)
    
    else:
        args = sys.argv

        if("-i" in args):
            i = args.index("-i") 1
            iface = args[i]
            print(iface)

        if("-p" in args):
            passive = args.index("-p") 1
            passive = args[passive]
            print(passive)
 
main()

CodePudding user response:

sys.argv also returns the name of the python file. Try running:

print(sys.argv)
  • Related