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])
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.
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;
Note: The single dash(‘-‘) for short forms and double dash(‘–‘) for long form of the argument in the code should be used.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- MySQL-Connector-Python module in Python
- twitter-text-python (ttp) module - Python
- Import module in Python
- OS Module in Python with Examples
- struct module in Python
- Secrets | Python module to Generate secure random numbers
- Python calendar module | formatmonth() method
- Python | Writing to an excel file using openpyxl module
- Count frequencies of all elements in array in Python using collections module
- OS Path module in Python
- Stack and Queue in Python using queue Module
- Testing in Python using doctest module
- Python | ASCII art using pyfiglet module
- Python | Adjusting rows and columns of an excel file using openpyxl module
- Python | Plotting charts in excel sheet using openpyxl module | Set - 1
- Python | Convert Image to Pdf using img2pdf module
- Python | Plotting charts in excel sheet using openpyxl module | Set – 2
- Python | Plotting charts in excel sheet using openpyxl module | Set 3
- Python | Create and write on excel file using xlsxwriter module
- spwd module in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.