
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to use one or more same positional arguments in Python?
Introduction..
If we were writing a program that performs arithematic operations on two numbers, we could define them as two positional arguments. But since they are the same kinds/python data types of arguments, it might make more sense to use the nargs option to tell argparse that you want exactly two of same types.
How to do it..
1. Let's write a program to substract two numbers (both the arguments are of same type).
Example
import argparse def get_args(): """ Function : get_args parameters used in .add_argument 1. metavar - Provide a hint to the user about the data type. - By default, all arguments are strings. 2. type - The actual Python data type - (note the lack of quotes around str) 3. help - A brief description of the parameter for the usage 4. nargs - require exactly nargs values. """ parser = argparse.ArgumentParser( description='Example for nargs', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('numbers', metavar='int', nargs=2, type=int, help='Numbers of type int for subtraction') return parser.parse_args() def main(): args = get_args() num1, num2 = args.numbers print(f" *** Subtracting two number - {num1} - {num2} = {num1 - num2}") if __name__ == '__main__': main()
The nargs=2 will require exactly two values.
Each value must be send as an integer value else our program will error out.
Let us run the program by passing different values.
Output
<<< python test.py 30 10 *** Subtracting two number - 30 - 10 = 40 <<< python test.py 30 10 *** Subtracting two number - 30 - 10 = 20 <<< python test.py 10 30 *** Subtracting two number - 10 - 30 = -20 <<< python test.py 10 10 30 usage: test.py [-h] int int test.py: error: unrecognized arguments: 30 <<< python test.py usage: test.py [-h] int int test.py: error: the following arguments are required: int
- Related Articles
- How to implement a constructor reference with one or more arguments in Java?
- How to use variable-length arguments in a function in Python?
- Python - Remove positional rows
- How to increment one or more counters with JavaScript?
- How to use typeof with arguments in JavaScript?
- How to use named arguments in JavaScript functions?
- Write a program in Python to remove one or more than one columns in a given DataFrame
- How to use spread operator to join two or more arrays in JavaScript?
- How are arguments passed by value or by reference in Python?
- How to use unlimited arguments in a JavaScript function?
- How to find the position of one or more values in a vector into another vector that contains same values in R?
- How to use jQuery.slice() method with no arguments?
- How to subset one or more sub-elements of a list in R?
- How to send one or more files to an API using axios in ReactJS?
- How to use Spread Syntax with arguments in JavaScript functions?

Advertisements