0

I'm creating a menu driven python script but I also want to add the functionality for command line arguments, for example if user has to select 2nd option he can either do it normal way i.e pressing 2 when asked to enter choice or to directly choose 2 by passing command line argument.

For this, am using sys module. And checking if user has passed any command line argument if not, then calling the function which would print the options and ask user to enter his choice (the normal way as mentioned above)

As per example command would be $ python3 choice.py 2

But am getting error while doing this and can't figure out what is wrong, here is the way am doing.

import sys
.
.
.

if sys.argv[2]:
    if sys.argv1[2] == 1:
        choose_one()
    elif sys.argv[2] == 2:
        choose_two()
    elif sys.arg[2] == 3:
        choose_three()
    else:
        choose_option() #If none of these command line args then call the function which would print options and let user choose

Error:

  File "choice.py", line 3, in <module>
    if sys.argv[2]:
IndexError: list index out of range

Basically I am willing to give user both options if he wants to run things quickly he can pass command line arguments, bypassing the usual reading choice option and entering the choice.

Sam
  • 15
  • 3
  • Since there aren't any inputs, the list ends at index 0. You may probably use `exceptions`. Also I noticed you use `sys.argv1` in the first line indide `if`. Is it a typo? – Abhyudai Nov 20 '19 at 08:35
  • 2
    When you run your script as such: `python3 choice.py 2`, then `sys.argv[0]` will give you `'choice.py'`. `sys.argv[1]` will give you `'2'` **as a string**. –  Nov 20 '19 at 08:37
  • before posting here I already tried with sys.argv[1] same error. – Sam Nov 20 '19 at 08:38
  • In the code you posted there are some typos in your spelling of 'sys.argv'. For example, the phrase 'if sys.argv1[2]' has an extra 1 in it, and ' elif sys.arg[2]' has no v in it. – Asker Nov 20 '19 at 08:40
  • @Abhyudai oops! that's a typo no in the actual code but here. But there is input, it can't end at 0 this is the command am using `python3 choice.py 2` – Sam Nov 20 '19 at 08:40
  • @JustinEzequiel hey justion thanks, it got solved actually I had to enclose 2 inside quotes in if condition since sys.argv gives the user input as string. I didn't knew about that and assumed it would be an int. However, if there is no input i.e no `sys.agrv[1]` its giving error `Traceback (most recent call last): File "choice.py", line 4, in if sys.argv[1]: IndexError: list index out of range ` – Sam Nov 20 '19 at 08:52
  • @JustinEzequiel I do have a corresponding `else` block for `if sys.argv[1]:` so ideally if `sys.argv[1]` is not present it should do what its corresponding `else` block says but its not doing – Sam Nov 20 '19 at 08:54
  • `if sys.argv[1]:` will still raise an `IndexError` if there are no arguments as python will still need to evaluate `sys.argv[1]`. What you can check instead is the length of `sys.argv`, i.e., `len(sys.argv)` will equal one (1) if you call `python3 choice.py` –  Nov 20 '19 at 11:56
  • I got your point but its not possible to not evaluate `sys.argv[1]` anywhere in the code because if we don't do that how would we know what user passed as command line args? – Sam Nov 20 '19 at 13:16

0 Answers0