{"id":3811,"date":"2023-02-07T15:00:05","date_gmt":"2023-02-07T09:30:05","guid":{"rendered":"https:\/\/trysiteprice.com\/blog\/?p=3811"},"modified":"2023-02-07T15:00:05","modified_gmt":"2023-02-07T09:30:05","slug":"python-command-line-arguments-examples","status":"publish","type":"post","link":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/","title":{"rendered":"Python Command Line Arguments Examples"},"content":{"rendered":"<p>Python has a built-in module called <code>argparse<\/code> which makes it easy to write user-friendly command-line interfaces. The following are some examples of using <code>argparse<\/code> to handle command-line arguments:<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-python\"><span class=\"hljs-keyword\">import<\/span> argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=<span class=\"hljs-string\">'Process some integers.'<\/span>)<br \/>\nparser.add_argument(<span class=\"hljs-string\">'integers'<\/span>, metavar=<span class=\"hljs-string\">'N'<\/span>, <span class=\"hljs-built_in\">type<\/span>=<span class=\"hljs-built_in\">int<\/span>, nargs=<span class=\"hljs-string\">'+'<\/span>,<br \/>\n                    <span class=\"hljs-built_in\">help<\/span>=<span class=\"hljs-string\">'an integer for the accumulator'<\/span>)<br \/>\nparser.add_argument(<span class=\"hljs-string\">'--sum'<\/span>, dest=<span class=\"hljs-string\">'accumulate'<\/span>, action=<span class=\"hljs-string\">'store_const'<\/span>,<br \/>\n                    const=<span class=\"hljs-built_in\">sum<\/span>, default=<span class=\"hljs-built_in\">max<\/span>,<br \/>\n                    <span class=\"hljs-built_in\">help<\/span>=<span class=\"hljs-string\">'sum the integers (default: find the max)'<\/span>)<\/p>\n<p>args = parser.parse_args()<br \/>\n<span class=\"hljs-built_in\">print<\/span>(args.accumulate(args.integers))<br \/>\n<\/code><\/div>\n<\/div>\n<p>This code will define a command-line interface that takes in a list of integers and either calculates the sum or finds the maximum value, depending on the <code>--sum<\/code> flag. To run this script and sum the values <code>1 2 3<\/code>, you would run:<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-python\">python script.py <span class=\"hljs-number\">1<\/span> <span class=\"hljs-number\">2<\/span> <span class=\"hljs-number\">3<\/span> --<span class=\"hljs-built_in\">sum<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>For more information on <code>argparse<\/code>, see the official Python documentation: <a href=\"https:\/\/docs.python.org\/3\/howto\/argparse.html\" target=\"_new\" rel=\"noopener\">https:\/\/docs.python.org\/3\/howto\/argparse.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has a built-in module called argparse which makes it easy to write user-friendly command-line interfaces. The following are some examples of using argparse to handle command-line arguments: import argparse parser = argparse.ArgumentParser(description=&#8217;Process some integers.&#8217;) parser.add_argument(&#8216;integers&#8217;, metavar=&#8217;N&#8217;, type=int, nargs=&#8217;+&#8217;, help=&#8217;an integer for the accumulator&#8217;) parser.add_argument(&#8216;&#8211;sum&#8217;, dest=&#8217;accumulate&#8217;, action=&#8217;store_const&#8217;, const=sum, default=max, help=&#8217;sum the integers (default: find the &#8230; <a title=\"Python Command Line Arguments Examples\" class=\"read-more\" href=\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/\" aria-label=\"Read more about Python Command Line Arguments Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3811","post","type-post","status-publish","format-standard","hentry","category-best-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Command Line Arguments Examples - TrySitePrice<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Command Line Arguments Examples - TrySitePrice\" \/>\n<meta property=\"og:description\" content=\"Python has a built-in module called argparse which makes it easy to write user-friendly command-line interfaces. The following are some examples of using argparse to handle command-line arguments: import argparse parser = argparse.ArgumentParser(description=&#039;Process some integers.&#039;) parser.add_argument(&#039;integers&#039;, metavar=&#039;N&#039;, type=int, nargs=&#039;+&#039;, help=&#039;an integer for the accumulator&#039;) parser.add_argument(&#039;--sum&#039;, dest=&#039;accumulate&#039;, action=&#039;store_const&#039;, const=sum, default=max, help=&#039;sum the integers (default: find the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"TrySitePrice\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-07T09:30:05+00:00\" \/>\n<meta name=\"author\" content=\"Rahul Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/\"},\"author\":{\"name\":\"Rahul Sahu\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4\"},\"headline\":\"Python Command Line Arguments Examples\",\"datePublished\":\"2023-02-07T09:30:05+00:00\",\"dateModified\":\"2023-02-07T09:30:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/\"},\"wordCount\":86,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#organization\"},\"articleSection\":[\"Best\/Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/\",\"url\":\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/\",\"name\":\"Python Command Line Arguments Examples - TrySitePrice\",\"isPartOf\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#website\"},\"datePublished\":\"2023-02-07T09:30:05+00:00\",\"dateModified\":\"2023-02-07T09:30:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trysiteprice.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Command Line Arguments Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#website\",\"url\":\"https:\/\/trysiteprice.com\/blog\/\",\"name\":\"TrySitePrice\",\"description\":\"Free Website Value Calculator Tool\",\"publisher\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trysiteprice.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#organization\",\"name\":\"TrySitePrice\",\"url\":\"https:\/\/trysiteprice.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png\",\"contentUrl\":\"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png\",\"width\":395,\"height\":268,\"caption\":\"TrySitePrice\"},\"image\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4\",\"name\":\"Rahul Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g\",\"caption\":\"Rahul Sahu\"},\"sameAs\":[\"https:\/\/trysiteprice.com\/blog\"],\"url\":\"https:\/\/trysiteprice.com\/blog\/author\/rsahu4242_trysiteprice\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Command Line Arguments Examples - TrySitePrice","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/","og_locale":"en_US","og_type":"article","og_title":"Python Command Line Arguments Examples - TrySitePrice","og_description":"Python has a built-in module called argparse which makes it easy to write user-friendly command-line interfaces. The following are some examples of using argparse to handle command-line arguments: import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the ... Read more","og_url":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/","og_site_name":"TrySitePrice","article_published_time":"2023-02-07T09:30:05+00:00","author":"Rahul Sahu","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/#article","isPartOf":{"@id":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/"},"author":{"name":"Rahul Sahu","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4"},"headline":"Python Command Line Arguments Examples","datePublished":"2023-02-07T09:30:05+00:00","dateModified":"2023-02-07T09:30:05+00:00","mainEntityOfPage":{"@id":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/"},"wordCount":86,"commentCount":0,"publisher":{"@id":"https:\/\/trysiteprice.com\/blog\/#organization"},"articleSection":["Best\/Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/","url":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/","name":"Python Command Line Arguments Examples - TrySitePrice","isPartOf":{"@id":"https:\/\/trysiteprice.com\/blog\/#website"},"datePublished":"2023-02-07T09:30:05+00:00","dateModified":"2023-02-07T09:30:05+00:00","breadcrumb":{"@id":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/trysiteprice.com\/blog\/python-command-line-arguments-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trysiteprice.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Command Line Arguments Examples"}]},{"@type":"WebSite","@id":"https:\/\/trysiteprice.com\/blog\/#website","url":"https:\/\/trysiteprice.com\/blog\/","name":"TrySitePrice","description":"Free Website Value Calculator Tool","publisher":{"@id":"https:\/\/trysiteprice.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trysiteprice.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/trysiteprice.com\/blog\/#organization","name":"TrySitePrice","url":"https:\/\/trysiteprice.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png","contentUrl":"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png","width":395,"height":268,"caption":"TrySitePrice"},"image":{"@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4","name":"Rahul Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g","caption":"Rahul Sahu"},"sameAs":["https:\/\/trysiteprice.com\/blog"],"url":"https:\/\/trysiteprice.com\/blog\/author\/rsahu4242_trysiteprice\/"}]}},"_links":{"self":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts\/3811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/comments?post=3811"}],"version-history":[{"count":1,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts\/3811\/revisions"}],"predecessor-version":[{"id":3812,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts\/3811\/revisions\/3812"}],"wp:attachment":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/media?parent=3811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/categories?post=3811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/tags?post=3811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}