Execute Functions with Multiple Arguments at a Terminal



In Python, you can execute functions with multiple arguments at a terminal by using the argparse module, which allows you to define and parse command-line arguments in a flexible and user-friendly way.

Using the argparse module, you can easily create command-line interfaces for functions with various arguments in Python. This article will cover several examples demonstrating how to use argparse to execute functions with multiple arguments from the terminal.

Adding Two Numbers

The following code defines a function that takes two integers, x and y and returns their sum. The argparse module is used to create a command-line interface.

import argparse  

def add_numbers(x, y):  
   return x + y  

if __name__ == '__main__':  
   parser = argparse.ArgumentParser()  
   parser.add_argument('x', type=int, help='the first number')  
   parser.add_argument('y', type=int, help='the second number')  
   args = parser.parse_args()  

   result = add_numbers(args.x, args.y)  
   print(result)

Execution

To execute this script in the terminal:

#script.py
$ python script.py 2 3

Following is the output for the above code.

5

Multiplying Three Numbers

The following script defines a function, which takes three integers and returns their product. The argparse setup is similar to the first example, but here we add a third argument, z.

import argparse  

def multiply_numbers(x, y, z):  
   return x * y * z  

if __name__ == '__main__':  
   parser = argparse.ArgumentParser()  
   parser.add_argument('x', type=int, help='the first number')  
   parser.add_argument('y', type=int, help='the second number')  
   parser.add_argument('z', type=int, help='the third number')  
   args = parser.parse_args()  

   result = multiply_numbers(args.x, args.y, args.z)  
   print(result)

Execution

To run this script:

$ python script.py 2 3 4

Following is the output for the above code.

5

Checking for Prime Numbers

In the following example, the code defines a function that checks if a given integer is prime. The argparse module is used to accept a single integer argument.

import argparse  

def is_prime(n):  
   if n < 2:  
      return False  
   for i in range(2, int(n ** 0.5) + 1):  
      if n % i == 0:  
         return False  
   return True  

if __name__ == '__main__':  
   parser = argparse.ArgumentParser()  
   parser.add_argument('n', type=int, help='the number to test for primality')  
   args = parser.parse_args()  

   result = is_prime(args.n)  
   if result:  
      print(f'{args.n} is prime')  
   else:  
      print(f'{args.n} is not prime')

Execution

To check if a number is prime:

$ python script.py 7

Following is the output for the above code.

7 is prime

Repeating a String

Here, a function that prints a string repeated times. The argparse module is used to define two positional arguments.

import argparse  

def repeat_string(string, n):  
   print(string * n)  

if __name__ == '__main__':  
   parser = argparse.ArgumentParser()  
   parser.add_argument('string', type=str, help='the string to repeat')  
   parser.add_argument('n', type=int, help='the number of times to repeat the string')  
   args = parser.parse_args()  

   repeat_string(args.string, args.n)

Execution

To repeat a string:

$ python script.py Hello 3

Following is the output for the above code.

HelloHelloHello

Extracting a Substring

This script defines a function that takes a string and two integers, calculates their sum, and prints the substring of the string containing the first n characters, where n is the sum of the two integers.

import argparse  

def substring(string, x, y):  
   n = x + y  
   print(string[:n])  

if __name__ == '__main__':  
   parser = argparse.ArgumentParser()  
   parser.add_argument('string', type=str, help='the string to extract a substring from')  
   parser.add_argument('x', type=int, help='the first index of the substring')  
   parser.add_argument('y', type=int, help='the second index of the substring')  
   args = parser.parse_args()  

   substring(args.string, args.x, args.y)

Execution

To extract a substring:

$ python script.py HelloWorld 3 4

Following is the output for the above code.

Hell
Updated on: 2025-02-25T16:35:36+05:30

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements