0

I have a scenario. I have to pass the parameter values to the methods in script and methods to execute.

Example:

File name app.py

value1 = sys.argv[1]
value2 = sys.argv[2]
def method1():
    print(value1)

def method2():
    print(value2)

Command I am trying to execute: python3 app.py 10 12 method2

In the above the output should be method2() print value is 12

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • `argv` would be `["app.py", "10", "12", "method2"]`. so if you want access the `"method2"` argument pass `argv[3]` not `argv[2]` – Tibebes. M Sep 23 '20 at 09:17
  • You should check [argparse module](https://docs.python.org/3.6/howto/argparse.html) it is pretty easy to use and will become handy as soon as you have multiple command line arguments to retrieve – Jean-Marc Volle Sep 23 '20 at 09:22
  • But it's still unclear to me what exactly you're trying to achieve. are you trying to choose which function to invoke via the command line arguments? – Tibebes. M Sep 23 '20 at 09:22
  • @Tibebes.M Basically I need to pass the argument to execute either method1 function or method2 function alone or both functions. When we pass the argument as method1, It should execute only method1 function. If we pass method 1 and method 2 in command arguments . It should execute both the functions – friends forever Sep 23 '20 at 09:30

2 Answers2

2
import sys
def method1():
    print(sys.argv[1])
def method2():
    print(sys.argv[2])
if __name__ == "__main__":
    if sys.argv[3] == 'method1':
        method1()
    elif sys.argv[3] == 'method2':
        method2()
    else:
        print("Invalid argument")

Executed as :

python app.py 10 12 method1
>> 10
python app.py 10 12 method2
>> 12
Suraj
  • 2,253
  • 3
  • 17
  • 48
  • Can we call something like this if __name__ == "__main__": if sys.argv[3] == 'method1': sys.argv[3] () – friends forever Sep 23 '20 at 11:40
  • no because `argv[3]` is a **string** - not a `Callable` type. Although not recommended you can get a reference to the function from the lookup table by making use of [`locals()`](https://docs.python.org/3/library/functions.html#locals) or `globals()` – Tibebes. M Sep 23 '20 at 11:43
  • see the answer I've posted to see how – Tibebes. M Sep 23 '20 at 11:51
0

Here is how you can reference the function to call dynamically (from the local symbol table using locals())

import sys


def method1(value1, value2):
    print(value1)


def method2(value1, value2):
    print(value2)


def method_not_found(*args, **kwargs):
    print('[-] function not found')


if __name__ == "__main__":
    locals().get(sys.argv[3], 'method_not_found')(sys.argv[1], sys.argv[2])
user@pc: python a.py 10 12 method1

10
user@pc: python a.py 10 12 method13

[-] function not found

Explanation: the way the line:

locals().get(sys.argv[3], 'method_not_found')(sys.argv[1], sys.argv[2])

works is that locals() return a dict type and we're calling .get() method to get refer to the function we're gonna call (if not found, get method_not_found function instead).

Then after we get the function we want to call, we execute it by passing sys.argv[1], sys.argv[2] as arguments.

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36