mardi 26 novembre 2019

Argparse :: ask 1 or 3 arguments

I'm writing a script in order to search for factorized numbers.

I want my script to accept 1 or 3 arguments, it should work only with:

  • python myscript.py -i
  • python myscript.py -e -n -o

Otherwise it should print --help.

Here is my code:

# --- Here we organize the choices command line aruments                
parser = argparse.ArgumentParser()                                      
parser.add_argument('-i', dest='input', help="input a pubilc key .pem file", metavar=None) 
parser.add_argument('-n', dest='modulus', help="value of modulus n (please input also -e and -o)", metavar=None)
parser.add_argument('-e', dest='exponent', help="value of exponent e (please input also -n and -o)", metavar=None)  
parser.add_argument('-o',dest='output', help="output a file name (please input also -n and -e)", metavar=None)                  
args = parser.parse_args()                                              

# --- Guide the user to the right choice ---                
if (len(sys.argv) == 1 and args.input != None):    # <-- problem must be around here I believe
    pass
elif (len(sys.argv) == 3 and args.modulus != None and args.exponent != None and args.output != None ):
    pass
else:
    parser.print_help()

But when I run python myscript.py -i first it prints the --help and then it execute the code.

It shouldn't print --help: everything is fine, I'm giving 1 argument and it's input

Aucun commentaire:

Enregistrer un commentaire