Getopt module in Python Last Updated : 04 May, 2020 Comments Improve Suggest changes Like Article Like Report The getopt module is a parser for command-line options based on the convention established by the Unix getopt() function. It is in general used for parsing an argument sequence such as sys.argv. In other words, this module helps scripts to parse command-line arguments in sys.argv. It works similar to the C getopt() function for parsing command-line parameters. Python getopt function The first function provided by this module is of the same name i.e. getopt(). Its primary functionality is to parse command-line options and parameter list. The syntax of the function is as below: Syntax: getopt.getopt(args, options, [long_options]) Parameters: args: List of arguments to be passed. options: String of option letters that the script wants to recognize. Options that require an argument should be followed by a colon (:). long_options: List of the string with the name of long options. Options that require arguments should be followed by an equal sign (=). Return Type: Returns value consisting of two elements: the first is a list of (option, value) pairs. The second is the list of program arguments left after the option list was stripped. Example 1: Python3 1== import sys import getopt def full_name(): first_name = None last_name = None argv = sys.argv[1:] try: opts, args = getopt.getopt(argv, "f:l:") except: print("Error") for opt, arg in opts: if opt in ['-f']: first_name = arg elif opt in ['-l']: last_name = arg print( first_name +" " + last_name) full_name() Output: Here we have created a function full_name(), which prints the full name after getting first name and last name from the command line. We have also abbreviated first name as 'f' and last name as 'l'. Example 2: Now lets look into the case where instead of short-form like 'f' or 'l', we could use full forms as 'first_name' and 'last_name'.The below code uses full forms to print full name; Python3 1== import sys import getopt def full_name(): first_name = None last_name = None argv = sys.argv[1:] try: opts, args = getopt.getopt(argv, "f:l:", ["first_name =", "last_name ="]) except: print("Error") for opt, arg in opts: if opt in ['-f', '--first_name']: first_name = arg elif opt in ['-l', '--last_name']: last_name = arg print( first_name +" " + last_name) full_name() Output: Note: The single dash('-') for short forms and double dash('--') for long form of the argument in the code should be used. Comment More infoAdvertise with us Next Article Getopt module in Python R RajuKumar19 Follow Improve Article Tags : Python Write From Home python-modules Practice Tags : python Similar Reads Docopt module in Python Docopt is a command line interface description module. It helps you define a interface for a command-line application and generates parser for it. The interface message in docopt is a formalized help message. Installation You can install docopt module in various ways, pip is one of the best ways to 3 min read Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st 3 min read External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m 5 min read Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there 4 min read Python datetime module In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. In this article, we will explore How DateTime in Python 14 min read Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext 9 min read Python Fire Module Python Fire is a library to create CLI applications. It can automatically generate command line Interfaces from any object in python. It is not limited to this, it is a good tool for debugging and development purposes. With the help of Fire, you can turn existing code into CLI. In this article, we w 3 min read Python Math Module Math Module consists of mathematical functions and constants. It is a built-in module made for mathematical tasks. The math module provides the math functions to deal with basic operations such as addition(+), subtraction(-), multiplication(*), division(/), and advanced operations like trigonometric 13 min read Create and Import modules in Python In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and pac 3 min read Inspect Module in Python The inspect module in Python is useful for examining objects in your code. Since Python is an object-oriented language, this module helps inspect modules, functions and other objects to better understand their structure. It also allows for detailed analysis of function calls and tracebacks, making d 4 min read Like