Tsang
Tsang

Reputation: 13

Alternative way to limit the number of inputs in command line

I want to write a code which limits the number of system arguments a user can enter using import sys/sys.argv

Lets say I want to limit the user to enter only 2 system arguments, no more or no less (3 if including the user program).

I have come up with a way using the len function, however I was wondering if there is another way to do this?

Here is what I came up with:

if len(sys.argv) < 3 or len(sys.argv) > 3:
    print "Please enter only two system arguments"
    sys.exit()

Again, is there anyway to do this without using the len function?

Thanks!

Upvotes: 1

Views: 318

Answers (1)

DYZ
DYZ

Reputation: 57075

Theoretically, you can do list unpacking with the exception handling, but why?

import sys
try:
    arg0, arg1, arg2 = sys.argv
except ValueError:
    # Your error handler

Upvotes: 1

Related Questions