{"id":886,"date":"2022-11-10T17:22:00","date_gmt":"2022-11-10T11:52:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=886"},"modified":"2023-08-15T15:57:50","modified_gmt":"2023-08-15T10:27:50","slug":"argparse-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/argparse-in-python","title":{"rendered":"How To Use argparse To Build Command-Line Interface In Python"},"content":{"rendered":"\n<p>Python has a rich and versatile collection of standard libraries, making it one of the better programming languages, so it needs to cover all the areas of programming.<\/p>\n\n\n\n<p>If developers want to write the scripts for the command line using Python, then there must be some libraries that allow them to write command line scripts conveniently. Python has a library called&nbsp;<code>argparse<\/code>&nbsp;that helps to create exemplary command-line interfaces for the command-line scripts.<\/p>\n\n\n\n<p>In this article, we&#8217;ll be going to learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>What is&nbsp;<code>argparse<\/code>&nbsp;in Python<\/strong><\/li>\n\n\n\n<li><strong>How to create a basic CLI in Python using&nbsp;<code>argparse<\/code><\/strong><\/li>\n\n\n\n<li><strong>What are the advanced features of Python&nbsp;<code>argparse<\/code><\/strong><\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-what-is-cli\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-what-is-cli\"><\/a>What is CLI<\/h1>\n\n\n\n<p>A command line interface, short for&nbsp;<strong>CLI<\/strong>, is a medium or type of interface that allows us to interact with a command line script. Python has many libraries that allow us to write the command line interface for our scripts. We&#8217;ll use a library called&nbsp;<code>argparse<\/code>&nbsp;to create a CLI in Python.<\/p>\n\n\n\n<p>It is a standard way to create a CLI in Python, and&nbsp;<code>argparse<\/code>&nbsp;came as a replacement for the modules like&nbsp;<a target=\"_blank\" href=\"https:\/\/docs.python.org\/3\/library\/optparse.html\" rel=\"noreferrer noopener\">optparse<\/a>&nbsp;and&nbsp;<a target=\"_blank\" href=\"https:\/\/docs.python.org\/3\/library\/getopt.html\" rel=\"noreferrer noopener\">getopt<\/a>&nbsp;because they lacked some significant features.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-getting-to-know-cli\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-getting-to-know-cli\"><\/a>Getting to know CLI<\/h1>\n\n\n\n<p>Before we proceed any further, we need to know how the command line interface works, so open up your terminal on your computer and execute a command&nbsp;<strong><em>&#8216;python&#8217;<\/em><\/strong>&nbsp;to start using Python commands in your command line.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python\n....\nPython 3.10.5 (tags\/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt; print(\"Hello Geeks!\")\nHello Geeks!\n&gt;&gt;&gt; 3 % 6\n3\n&gt;&gt;&gt; sum([4, 19, 111])\n134<\/pre><\/div>\n\n\n\n<p>Here we can execute the Python commands directly in our CLI. Fortunately, we don&#8217;t need any Python interpreter to get started. Still, we can add an&nbsp;<strong>option<\/strong>(type of argument) with the argument to get the extra information.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python --version\n....\nPython 3.10.5\n\n&gt;python -q\n....\n&gt;&gt;&gt; print(\"Python has started without prompting copyright and version messages.\")\nPython has started without prompting copyright and version messages.<\/pre><\/div>\n\n\n\n<p>Here we obtained the version of Python that we are currently using by just adding the&nbsp;<code>--version<\/code>&nbsp;option to the&nbsp;<code>python<\/code>&nbsp;argument, and in the second command, we used the&nbsp;<code>-q<\/code>&nbsp;option to start the Python interpreter without prompting the version and copyright messages that it usually shows on startup.<\/p>\n\n\n\n<p>To understand arguments, options, and parameters, consider the following example:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;dir \/d \/r D:\\SACHIN\\Pycharm\\cli_flask<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>dir:<\/strong>&nbsp;The name of the command we are executing<\/li>\n\n\n\n<li><strong>\/d:<\/strong>&nbsp;An option to display all directories in the current path<\/li>\n\n\n\n<li><strong>\/r:<\/strong>&nbsp;An option to display the read-only files<\/li>\n\n\n\n<li><strong>D:\\SACHIN\\Pycharm\\cli_flask:<\/strong>&nbsp;A parameter to list the directories in the specified path<\/li>\n<\/ul>\n\n\n\n<p>To explain precisely,<\/p>\n\n\n\n<p>An&nbsp;<strong>argument<\/strong>&nbsp;is a command that we want to execute.<\/p>\n\n\n\n<p>An&nbsp;<strong>option<\/strong>&nbsp;is part of an argument or type of argument used to modify the behavior of the command line.<\/p>\n\n\n\n<p>A&nbsp;<strong>parameter<\/strong>&nbsp;is a type of argument that provides additional information to the command.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-creating-a-basic-command-using-argparse\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-creating-a-basic-command-using-argparse\"><\/a>Creating a basic command using\u00a0argparse<\/h1>\n\n\n\n<p>We can create an essential command for CLI in a few steps using&nbsp;<code>argparse<\/code>. Let&#8217;s understand the measures included in the process of making the command.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Importing the&nbsp;<code>argparse<\/code>&nbsp;because it&#8217;s a Python library<\/strong><\/li>\n\n\n\n<li><strong>Creating the parser<\/strong><\/li>\n\n\n\n<li><strong>Adding the positional and optional arguments to the parser<\/strong><\/li>\n\n\n\n<li><strong>Executing the parser using the&nbsp;<code>parse_args()<\/code><\/strong><\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s assume we have a file named&nbsp;<code>command.py<\/code>&nbsp;in which we write the basic command to get the&nbsp;<strong>factorial<\/strong>&nbsp;of the integer specified in the&nbsp;<strong>command line interface<\/strong>&nbsp;or&nbsp;<strong>CLI<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Importing argparse library\nimport argparse\n\nfrom math import factorial\n\n# Creating the parser\nparser = argparse.ArgumentParser(description='Get the factorial of the integer.')\n\n# Adding the argument\nparser.add_argument('Factorial',\n                    metavar='integer',\n                    type=int,\n                    help='factorial of the integer')\n\n# Executing the parser using parse_args() method\nargs = parser.parse_args()\n\nfact = args.Factorial\nprint(factorial(fact))<\/pre><\/div>\n\n\n\n<p>We&#8217;ve coded our first-ever command for CLI and now let&#8217;s understand our code. We can divide our code into four parts.<\/p>\n\n\n\n<p>First, we imported the&nbsp;<code>argparse<\/code>&nbsp;library and then created the&nbsp;<strong><em>parser<\/em><\/strong>&nbsp;using the&nbsp;<code>ArgumentParser()<\/code>&nbsp;method in which we passed the&nbsp;<code>detail<\/code>&nbsp;argument to specify the detail of our command.<\/p>\n\n\n\n<p>Next, we added the argument to the&nbsp;<strong>parser<\/strong>&nbsp;using the&nbsp;<code>add_argument<\/code>&nbsp;method, which holds the necessary information passed into the arguments inside the method. Finally, we executed our&nbsp;<strong>parser<\/strong>&nbsp;using the&nbsp;<code>parse_args()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>If we run our command in the CLI without specifying any value, then we will get an&nbsp;<strong>error<\/strong>&nbsp;and a&nbsp;<strong>usage message<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python command.py  \nusage: command.py [-h] integer\ncommand.py: error: the following arguments are required: integer<\/pre><\/div>\n\n\n\n<p>The program detected that we needed a&nbsp;<strong>positional argument<\/strong>&nbsp;(<code>integer<\/code>) to complete the execution, but the execution was interrupted due to the absence of the positional argument.<\/p>\n\n\n\n<p>We can see that our command accepts an&nbsp;<strong>optional<\/strong>&nbsp;<code>-h<\/code>&nbsp;flag.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python command.py -h\nusage: command.py [-h] integer\n\nGet the factorial of the integer.\n\npositional arguments:\n  integer     factorial of the integer\n\noptions:\n  -h, --help  show this help message and exit<\/pre><\/div>\n\n\n\n<p>If we execute our command with the&nbsp;<strong>positional<\/strong>&nbsp;argument it needs, we&#8217;ll get the desired result.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python command.py 6 \n720<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-advanced-usage-of-argparse\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-advanced-usage-of-argparse\"><\/a>Advanced usage of\u00a0argparse<\/h2>\n\n\n\n<p>We learned the basic usage of the&nbsp;<code>argparse<\/code>&nbsp;library, and now we can create an essential command for CLI. However, this is not it, we can do many more things using this library. We&#8217;ll see some advanced usage of the&nbsp;<code>argparse<\/code>&nbsp;library.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-display-custom-help-message\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-display-custom-help-message\"><\/a>Display custom help message<\/h3>\n\n\n\n<p>The&nbsp;<code>argparse<\/code>&nbsp;library generates usage help if not provided, but we can&nbsp;<strong>customize the help message using the&nbsp;<code>usage<\/code>&nbsp;keyword<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">parser = argparse.ArgumentParser(\n    usage='command.py [options] integer',\n    description='Get the factorial of the integer.')<\/pre><\/div>\n\n\n\n<p>Now we can see the different&nbsp;<strong>usage messages<\/strong>&nbsp;if we execute the program.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python command.py\nusage: factorial [options] integer\nfactorial: error: the following arguments are required: integer<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-setting-the-program-name\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-setting-the-program-name\"><\/a>Setting the program name<\/h3>\n\n\n\n<p>In the previous section, we saw the&nbsp;<strong>Python script name<\/strong>&nbsp;when we executed the program in the CLI because the library uses the&nbsp;<code>sys.argv[0]<\/code>&nbsp;to set the&nbsp;<strong>program&#8217;s name<\/strong>. However, we can specify the program name using the&nbsp;<code><strong>prog<\/strong><\/code>&nbsp;keyword.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">parser = argparse.ArgumentParser(\n    prog='factorial',\n    description='Get the factorial of the integer.')<\/pre><\/div>\n\n\n\n<p>We will now see our specified name,&nbsp;<code>factorial<\/code>, of the program in the CLI instead of the Python script name.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python command.py\nusage: factorial [-h] integer\nfactorial: error: the following arguments are required: integer<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-displaying-text-before-and-after-the-arguments\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-displaying-text-before-and-after-the-arguments\"><\/a>Displaying text before and after the arguments<\/h3>\n\n\n\n<p>We can&nbsp;<strong>customize<\/strong>&nbsp;the text we want to display&nbsp;<strong>before<\/strong>&nbsp;and&nbsp;<strong>after<\/strong>&nbsp;in the&nbsp;<strong>help<\/strong>&nbsp;text by using the following keywords.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>description<\/strong>: for the text shown before the help text<\/li>\n\n\n\n<li><strong>epilog<\/strong>: for the text shown after the help text<\/li>\n<\/ul>\n\n\n\n<p>We have already seen the use of&nbsp;<code><strong>description<\/strong><\/code>. This time we&#8217;ll use the&nbsp;<code><strong>epilog<\/strong><\/code>&nbsp;to display the text after the&nbsp;<strong>help<\/strong>&nbsp;text.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">parser = argparse.ArgumentParser(\n    description='Get the factorial of the integer.',\n    epilog='Have you understood the process? ')<\/pre><\/div>\n\n\n\n<p>We will see the customized message after the&nbsp;<strong>help<\/strong>&nbsp;text.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python command.py -h\nusage: command.py [-h] integer\n\nGet the factorial of the integer.\n\npositional arguments:\n  integer     factorial of the integer\n\noptions:\n  -h, --help  show this help message and exit\n\nHave you understood the process?<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-customizing-prefix-character\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-customizing-prefix-character\"><\/a>Customizing prefix character<\/h3>\n\n\n\n<p>The&nbsp;<strong>optional arguments<\/strong>&nbsp;in the CLI command are generally prefixed with (<code>-<\/code>), a standard prefix char. The&nbsp;<code>argparse<\/code>&nbsp;library has a feature that allows us to customize the&nbsp;<strong>prefix chars<\/strong>.<\/p>\n\n\n\n<p>The&nbsp;<code><strong>prefix_chars<\/strong><\/code>&nbsp;keyword lets us customize the prefix character while defining the parser.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">parser = argparse.ArgumentParser(\n    description='Get the factorial of the integer.',\n    prefix_chars='$')<\/pre><\/div>\n\n\n\n<p>Now, in the output, we won&#8217;t see (<code>-<\/code>) as the prefix char for the optional argument instead, we will see the (<code>$<\/code>) as the prefix char.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python command.py   \nusage: command.py [$h] integer\ncommand.py: error: the following arguments are required: integer<\/pre><\/div>\n\n\n\n<p>Now our program does not support the&nbsp;<code>-h<\/code>&nbsp;flag but the&nbsp;<code>$h<\/code>&nbsp;flag. The help text has also changed accordingly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-enable-and-disable-help\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-enable-and-disable-help\"><\/a>Enable and disable help<\/h3>\n\n\n\n<p>Python&nbsp;<code>argparse<\/code>&nbsp;library automatically generates the help text without having to code anything. However, sometimes we wanted to disable the feature. We can add the&nbsp;<code>add_help<\/code>&nbsp;keyword when creating the parser.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">parser = argparse.ArgumentParser(\n    description='Get the factorial of the integer.',\n    add_help=False)<\/pre><\/div>\n\n\n\n<p>We&#8217;ve added the&nbsp;<code>add_help<\/code>&nbsp;keyword in the above code and set its value to&nbsp;<code>False<\/code>. It ensures that the program does not accept the&nbsp;<code>-h<\/code>&nbsp;flag anymore.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python command.py\nusage: command.py integer\ncommand.py: error: the following arguments are required: integer<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-allowing-and-disallowing-abbreviation\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-allowing-and-disallowing-abbreviation\"><\/a>Allowing and disallowing abbreviation<\/h3>\n\n\n\n<p>One of the features the Python&nbsp;<code>argparse<\/code>&nbsp;library provides is the ability to handle abbreviations. Consider the following example that prints the square of the specified integer.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--square', action='store', type=int)\nparser.add_argument('--sum', action='store', type=int)\n\nargs = parser.parse_args()\nprint(args.square**2)<\/pre><\/div>\n\n\n\n<p>We can shorten the optional argument until the abbreviation leads to the wrong interpretation.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python abbreviation.py --square 4 \n16\n\n&gt;python abbreviation.py --squar 4  \n16\n\n&gt;python abbreviation.py --squa 4  \n16\n\n&gt;python abbreviation.py --squ 4  \n16\n\n&gt;python abbreviation.py --sq 4  \n16<\/pre><\/div>\n\n\n\n<p>We can shorten the optional argument until&nbsp;<code>--sq 4<\/code>. What happens if we execute the program specifying&nbsp;<code>--s 4<\/code>? It might lead to an error because&nbsp;<code>argparse<\/code>&nbsp;doesn&#8217;t know whether we want to specify&nbsp;<strong>4<\/strong>&nbsp;to the&nbsp;<code>--square<\/code>&nbsp;or the&nbsp;<code>--sum<\/code>&nbsp;argument.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python abbreviation.py --s 4   \nusage: abbreviation.py [-h] [--square SQUARE] [--sum SUM]\nabbreviation.py: error: ambiguous option: --s could match --square, --sum<\/pre><\/div>\n\n\n\n<p>However, we can force users to specify the full name of the option by disabling the feature by adding the&nbsp;<code>allow_abbrev<\/code>&nbsp;while creating the parser.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser(allow_abbrev=False)\nparser.add_argument('--square', action='store', type=int)\nparser.add_argument('--sum', action='store', type=int)\n\nargs = parser.parse_args()\n\nprint(args.square**2)<\/pre><\/div>\n\n\n\n<p>Now, if we try to execute the program using an abbreviation, the program will throw an error.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python abbreviation.py --squa 4  \nusage: abbreviation.py [-h] [--square SQUARE]\nabbreviation.py: error: unrecognized arguments: --squa 4<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-setting-the-name-or-flag-of-the-arguments\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-setting-the-name-or-flag-of-the-arguments\"><\/a>Setting the name or flag of the arguments<\/h3>\n\n\n\n<p>There are two types of arguments that we can use in our command line interface:<\/p>\n\n\n\n<p><strong>Positional arguments<\/strong><\/p>\n\n\n\n<p>In the previous examples, we&#8217;ve already seen the positional arguments. In the example above,&nbsp;<code>Factorial<\/code>&nbsp;was the&nbsp;<strong>positional argument<\/strong>, and our program couldn&#8217;t work without it.<\/p>\n\n\n\n<p><strong>Positional arguments<\/strong>&nbsp;are the types of argument that we use in command to perform some operation. They are called&nbsp;<strong>positional<\/strong>&nbsp;arguments because their position defines their function.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('Square', type=int, help='square of the integer')\n\nargs = parser.parse_args()\n\nprint(args.Square**2)<\/pre><\/div>\n\n\n\n<p>Here,&nbsp;<code>Square<\/code>&nbsp;is the positional argument, and when we specify an integer, the program returns the square of that integer. By default, the positional argument is treated as a&nbsp;<strong>String<\/strong>, so we used the&nbsp;<code>type<\/code>&nbsp;keyword and set its value to&nbsp;<code>int<\/code>&nbsp;to typecast the&nbsp;<strong>string into the integer<\/strong>.<\/p>\n\n\n\n<p><strong>Optional arguments<\/strong><\/p>\n\n\n\n<p>The optional arguments are used to perform the additional tasks, and when they are used, they can modify the command&#8217;s behavior at runtime.<\/p>\n\n\n\n<p>For example, if we look at the&nbsp;<code>ls<\/code>&nbsp;command that lists the files in a current directory and if we use the&nbsp;<code>-l<\/code>&nbsp;argument, which is an optional argument, it modifies the output and returns the extra information.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:zsh decode:true \">$ ls -l<\/pre><\/div>\n\n\n\n<p>Optional arguments are not mandatory, and we won&#8217;t get any errors if we don&#8217;t specify them. They are generally prefixed with a (<code>-<\/code>) dash or (<code>--<\/code>) double dash.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\nimport math\n\nparser = argparse.ArgumentParser()\nparser.add_argument('-s', \n                    '--sq_root', \n                    type=int, \n                    help='square root of the integer')\n\nargs = parser.parse_args()\n\nsqrt = args.sq_root\n\nprint(math.sqrt(sqrt))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python op_arg.py --sq_root 9 \n3.0\n\n&gt;python op_arg.py -s 9        \n3.0<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-setting-the-action\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-setting-the-action\"><\/a>Setting the action<\/h3>\n\n\n\n<p>When we add an argument to the command line interface, we can also define the kind of action to take when the argument is specified.<\/p>\n\n\n\n<p>The&nbsp;<code>action<\/code>&nbsp;keyword argument specifies how the command-line arguments should be handled. Many actions are available and ready to use:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>store<\/strong>: stores the argument&#8217;s value. This is the default action.<\/li>\n\n\n\n<li><strong>store_const<\/strong>: stores a constant value when the optional arguments are specified.<\/li>\n\n\n\n<li><strong>store_true<\/strong>&nbsp;and&nbsp;<strong>store_false<\/strong>: used for storing the&nbsp;<strong>boolean<\/strong>&nbsp;values&nbsp;<code>True<\/code>&nbsp;and&nbsp;<code>False<\/code>&nbsp;respectively when the optional arguments are specified and stores a&nbsp;<strong>False<\/strong>&nbsp;and&nbsp;<strong>True<\/strong>&nbsp;elsewhere, respectively.<\/li>\n\n\n\n<li><strong>append<\/strong>: stores a list and appends each argument value to the list.<\/li>\n\n\n\n<li><strong>append_const<\/strong>: stores a list and appends the constant value to the list each time the option is provided.<\/li>\n\n\n\n<li><strong>count<\/strong>: counts the number of times the option is provided.<\/li>\n\n\n\n<li><strong>help<\/strong>: shows the help message and then exits.<\/li>\n\n\n\n<li><strong>version<\/strong>: shows the program&#8217;s version information and exits.<\/li>\n\n\n\n<li><strong>extend<\/strong>: stores a list and extends each argument value to the list. It was added in Python v3.8.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s understand what these actions can do with simple examples.<\/p>\n\n\n\n<p>We&#8217;ve used the&nbsp;<code><strong>action='store'<\/strong><\/code>&nbsp;in the following example, which will store the specified value.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--store', action='store')\n\nargs = parser.parse_args()\nprint(args.store)\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python action.py --store 23 \n23\n{'store': '23'}\n\n&gt;python action.py --store hello\nhello\n{'store': 'hello'}<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code><strong>action='store_const'<\/strong><\/code>&nbsp;stores the value specified by the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/argparse.html#const\" target=\"_blank\">const<\/a>&nbsp;keyword. We&#8217;ve defined the&nbsp;<code>const<\/code>&nbsp;keyword and set its value to&nbsp;<code>hello<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--sc', action='store_const', const='hello')\n\nargs = parser.parse_args()\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p>We just provided the&nbsp;<code>--sc<\/code>&nbsp;argument, and we got the value specified in the&nbsp;<code>const<\/code>&nbsp;keyword because the value of&nbsp;<code>args.sc<\/code>&nbsp;is now&nbsp;<code>hello<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python action.py --sc     \n{'sc': 'hello'}<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code><strong>action='store_true'<\/strong><\/code>&nbsp;stores the boolean&nbsp;<code>True<\/code>&nbsp;and stores the&nbsp;<code>False<\/code>&nbsp;elsewhere when the arguments are passed. We can use&nbsp;<code><strong>action='store_false'<\/strong><\/code>&nbsp;for the opposite behavior.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--st', action='store_true')\n\nargs = parser.parse_args()\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python action.py        \n{'st': False}\n\n&gt;python action.py --st\n{'st': True}<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code><strong>action='append'<\/strong><\/code>&nbsp;creates a list of values passed in the CLI.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--append', action='append')\n\nargs = parser.parse_args()\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python action.py --append hello --append world\n{'append': ['hello', 'world']}<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code><strong>action='append_const'<\/strong><\/code>&nbsp;is the same as&nbsp;<code>append<\/code>, but it appends the same constant value.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--ac', action='append_const', const='2')\n\nargs = parser.parse_args()\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python action.py --ac --ac --ac               \n{'ac': ['2', '2', '2']}<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code><strong>action='count'<\/strong><\/code>&nbsp;counts the number of times the argument is passed.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--count', action='count', default=0)\n\nargs = parser.parse_args()\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python action.py --count --count --count --count\n{'count': 4}<\/pre><\/div>\n\n\n\n<p>We&#8217;ve already seen the help message in our examples previously, which is enabled for the&nbsp;<code>-h<\/code>&nbsp;flag. The&nbsp;<code><strong>action='help'<\/strong><\/code>&nbsp;lets us use another flag for the help message.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('-x', action='help')\n\nargs = parser.parse_args()\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python action.py -x\nusage: action.py [-h] [-x]\n\noptions:\n  -h, --help  show this help message and exit\n  -x<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code><strong>action='version'<\/strong><\/code>&nbsp;helps us to get the program&#8217;s version, but it expects a&nbsp;<code>version<\/code>&nbsp;keyword.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('-z', action='version', version='1.11')\n\nargs = parser.parse_args()\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python action.py -z\n1.11<\/pre><\/div>\n\n\n\n<p>The&nbsp;<code><strong>action='extend'<\/strong><\/code>&nbsp;extends the argument values to the list.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('-e', action='extend', nargs='+')\n\nargs = parser.parse_args()\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python action.py -e 2 4 6 3 -e 0 9\n{'e': ['2', '4', '6', '3', '0', '9']}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-setting-the-default-value\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-setting-the-default-value\"><\/a>Setting the default value<\/h3>\n\n\n\n<p>Since the optional arguments are not mandatory, they can be omitted by the user in the command line interface. In this situation, the value is generally set to&nbsp;<code>None<\/code>.<\/p>\n\n\n\n<p>We can set the default value for an argument when it is not provided using the&nbsp;<code>default<\/code>&nbsp;keyword.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('-d', default=5)\n\nargs = parser.parse_args()\n\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p>We&#8217;ll get the following output if we execute the program without specifying the&nbsp;<code>-d<\/code>&nbsp;option.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python default.py     \n{'d': 5}<\/pre><\/div>\n\n\n\n<p>We didn&#8217;t receive any error because the option&nbsp;<code>-d<\/code>&nbsp;is set to&nbsp;<strong>5<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-setting-the-type-of-the-argument\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-setting-the-type-of-the-argument\"><\/a>Setting the type of the argument<\/h3>\n\n\n\n<p>By default, the input arguments are treated as a string, and we can typecast the input arguments in the desired datatype using the&nbsp;<code>type<\/code>&nbsp;keyword. We&#8217;ve already seen examples where we used the&nbsp;<code>type<\/code>&nbsp;keyword.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\nimport math\n\nparser = argparse.ArgumentParser()\nparser.add_argument('-f', type=int,)\n\nargs = parser.parse_args()\n\nprint(math.factorial(args.f))<\/pre><\/div>\n\n\n\n<p><code>type=int<\/code>&nbsp;will ensure that the argument&#8217;s value must be an integer instead of a string.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python type.py -f 2\n2<\/pre><\/div>\n\n\n\n<p>The value of the argument is checked at the runtime. If there is any datatype other than the&nbsp;<strong>integer<\/strong>&nbsp;provided in the command line, then the program will throw an error.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python type.py -f a \nusage: type.py [-h] [-f F]\ntype.py: error: argument -f: invalid int value: 'a'<\/pre><\/div>\n\n\n\n<p>The error clearly states that the value&nbsp;<code>a<\/code>&nbsp;is an invalid integer, and we need to pass an&nbsp;<strong>integer<\/strong>&nbsp;instead of a&nbsp;<strong>string<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-making-the-argument-to-be-required\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-making-the-argument-to-be-required\"><\/a>Making the argument to be required<\/h3>\n\n\n\n<p>The user can omit the optional arguments. However, we can force the user to specify the value for an optional argument by making the argument to be required by using the&nbsp;<code><strong>required<\/strong><\/code>&nbsp;keyword.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('-d', required=True)\n\nargs = parser.parse_args()\n\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p>We used the&nbsp;<code>required<\/code>&nbsp;keyword and set its value to&nbsp;<code>True<\/code>, which forces the user to specify the value for the argument.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python required.py\nusage: required.py [-h] -d D\nrequired.py: error: the following arguments are required: -d<\/pre><\/div>\n\n\n\n<p><strong>Note: It is a bad practice to make an optional argument to be required by the user because the name itself depicts that it is optional, and the user wouldn&#8217;t expect to set a value for the argument.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-setting-the-number-of-values-the-argument-can-take\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-setting-the-number-of-values-the-argument-can-take\"><\/a>Setting the number of values the argument can take<\/h3>\n\n\n\n<p>Usually, we specify the single value to the argument, which is the default behavior. We can change this default behavior by specifying the number of values an argument can consume using the&nbsp;<code>nargs<\/code>&nbsp;keyword.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('--value', type=int, nargs=2)\n\nargs = parser.parse_args()\n\nprint(args.value)<\/pre><\/div>\n\n\n\n<p>The program will accept only two values, not more or less than that.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python nargs.py --value 1\nusage: nargs.py [-h] [--value VALUE VALUE]\nnargs.py: error: argument --value: expected 2 arguments\n\n&gt;python nargs.py --value 1 2\n[1, 2]\n\n&gt;python nargs.py --value 1 2 34\nusage: nargs.py [-h] [--value VALUE VALUE]\nnargs.py: error: unrecognized arguments: 34<\/pre><\/div>\n\n\n\n<p>As we can see, when we provided a&nbsp;<strong>single<\/strong>&nbsp;and&nbsp;<strong>more than two values<\/strong>, the program threw an&nbsp;<strong>error<\/strong>, but when we provided the&nbsp;<strong>two<\/strong>&nbsp;values, the program&nbsp;<strong>returned the list containing the two values<\/strong>.<\/p>\n\n\n\n<p>Instead of using any integer, the&nbsp;<code>nargs<\/code>&nbsp;keyword also accepts the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>?<\/strong>: a single value will be consumed, which can be optional<\/li>\n\n\n\n<li><strong>*<\/strong>: an extensible number of values, which will be gathered into a list<\/li>\n\n\n\n<li><strong>+<\/strong>: much like&nbsp;<strong><em>*<\/em><\/strong>, but it requires at least one value<\/li>\n\n\n\n<li><strong>argparse.REMAINDER<\/strong>: all the values that are remaining in the command line<\/li>\n<\/ul>\n\n\n\n<p>In the following example, the&nbsp;<strong>positional argument<\/strong>&nbsp;<code>value<\/code>&nbsp;accepts a single value. If the value is not provided, it returns the value set in the&nbsp;<code>default<\/code>&nbsp;keyword.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('value',\n                    nargs='?',\n                    default='This is a default value.')\n\nargs = parser.parse_args()\n\nprint(args.value)<\/pre><\/div>\n\n\n\n<p>We can now set the specific value for the&nbsp;<code>value<\/code>&nbsp;argument. If the value is not provided, it will prompt the default value in the command line.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python nargs.py 2\n2\n\n&gt;python nargs.py          \nThis is a default value.<\/pre><\/div>\n\n\n\n<p>If we wanted to provide a flexible number of values and gather them into a list, then we need to use the&nbsp;<strong>*<\/strong>&nbsp;value for the&nbsp;<code>nargs<\/code>&nbsp;keyword.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('value',\n                    nargs='*',\n                    default='This is a default value.')\n\nargs = parser.parse_args()\n\nprint(args.value)<\/pre><\/div>\n\n\n\n<p>The above code allows us to set a flexible number of values, which we can see in the following output.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python nargs.py more than one value\n['more', 'than', 'one', 'value']\n\n&gt;python nargs.py                    \nThis is a default value.<\/pre><\/div>\n\n\n\n<p>We can use the&nbsp;<strong>+<\/strong>&nbsp;value for the&nbsp;<code>nargs<\/code>&nbsp;keyword when we want a variable number of values, but we need to ensure that at least one value is specified.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('value',\n                    nargs='+',)\n\nargs = parser.parse_args()\n\nprint(args.value)<\/pre><\/div>\n\n\n\n<p>The above code can accept a variable number of values but make sure at least one value is specified. Otherwise, it will throw an error.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python nargs.py needs at least one value\n['needs', 'at', 'least', 'one', 'value']\n\n&gt;python nargs.py                         \nusage: nargs.py [-h] value [value ...]\nnargs.py: error: the following arguments are required: value<\/pre><\/div>\n\n\n\n<p>As we can see, when we executed the program without any&nbsp;<strong>value<\/strong>, it threw an error, while when we provided multiple values, it returned the list containing the values.<\/p>\n\n\n\n<p>Consider the following example, where we&#8217;re using the&nbsp;<code>argparse.REMAINDER<\/code>&nbsp;value to the&nbsp;<code>nargs<\/code>&nbsp;for the&nbsp;<code>remaining<\/code>&nbsp;argument to grab all the remaining values that have been specified in the command line and put them in a list.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('value')\nparser.add_argument('remaining', nargs=argparse.REMAINDER)\n\nargs = parser.parse_args()\n\nprint(f'First value: {args.value}')\nprint(f'Other values: {args.remaining}')<\/pre><\/div>\n\n\n\n<p>If we execute the program, the&nbsp;<strong>first value<\/strong>&nbsp;will be assigned to the&nbsp;<strong>first argument<\/strong>, and the&nbsp;<strong>remaining<\/strong>&nbsp;will be assigned to the&nbsp;<strong>second argument<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python nargs.py hey there geeks\nFirst value: hey\nOther values: ['there', 'geeks']<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-displaying-an-alternate-name-for-the-argument\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-displaying-an-alternate-name-for-the-argument\"><\/a>Displaying an alternate name for the argument<\/h3>\n\n\n\n<p>Using the&nbsp;<code>metavar<\/code>&nbsp;keyword, we can set the name for our argument that will show in the usage message.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument('-s',\n                    '--square',\n                    metavar='Square',)\n\nargs = parser.parse_args()\n\nprint(vars(args))<\/pre><\/div>\n\n\n\n<p>If we execute the program with the&nbsp;<code>-h<\/code>&nbsp;flag, we can see the name&nbsp;<strong>Square<\/strong>&nbsp;will be assigned to the&nbsp;<code>-s<\/code>&nbsp;and&nbsp;<code>--square<\/code>&nbsp;options in the help text.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:ps decode:true \">&gt;python metavar.py -h\nusage: metavar.py [-h] [-s Square]\n\noptions:\n  -h, --help            show this help message and exit\n  -s Square, --square Square<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/argparse-in-python#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>This tutorial is a bit lengthy, but it is meant to be because it has detailed information about the&nbsp;<code>argparse<\/code>&nbsp;library in Python, allowing us to build the command-line interface.<\/p>\n\n\n\n<p>We&#8217;ve covered the basic and advanced usage of the&nbsp;<code>argparse<\/code>&nbsp;with simple examples. We are now able to build our command-line interface conveniently.<\/p>\n\n\n\n<p>Let&#8217;s review what we&#8217;ve learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>what is Python&nbsp;<code>argparse<\/code>&nbsp;library and how to use it<\/li>\n\n\n\n<li>created the basic command-line interface<\/li>\n\n\n\n<li>what are the advanced usage of the Python&nbsp;<code>argparse<\/code>&nbsp;library<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has a rich and versatile collection of standard libraries, making it one of the better programming languages, so it needs to cover all the areas of programming. If developers want to write the scripts for the command line using Python, then there must be some libraries that allow them to write command line scripts [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":888,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2],"tags":[12,31],"class_list":["post-886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=886"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/886\/revisions"}],"predecessor-version":[{"id":1345,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/886\/revisions\/1345"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/888"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}