[{"data":1,"prerenderedAt":2009},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-argument-parser-example-argparse":3},{"id":4,"title":5,"body":6,"description":2002,"extension":2003,"meta":2004,"navigation":75,"path":2005,"seo":2006,"stem":2007,"__hash__":2008},"content\u002Fexamples\u002Fpython-argument-parser-example-argparse.md","Python Argument Parser Example (argparse)",{"type":7,"value":8,"toc":1957},"minimark",[9,13,21,24,40,45,48,262,265,287,290,298,302,305,308,325,331,335,341,344,365,371,382,396,400,403,411,442,445,449,530,538,548,551,555,574,577,581,608,617,621,624,780,783,818,824,839,843,848,852,855,866,869,893,896,910,914,917,934,936,959,961,982,985,993,997,1003,1245,1249,1252,1270,1273,1296,1300,1312,1314,1320,1324,1340,1342,1348,1351,1355,1361,1364,1368,1382,1384,1389,1393,1409,1421,1425,1439,1441,1446,1453,1457,1462,1466,1474,1488,1493,1499,1504,1518,1528,1532,1545,1548,1558,1561,1653,1656,1743,1747,1753,1755,1769,1772,1778,1781,1806,1817,1821,1824,1827,1849,1853,1857,1862,1866,1872,1876,1881,1885,1892,1904,1908,1914,1918,1953],[10,11,5],"h1",{"id":12},"python-argument-parser-example-argparse",[14,15,16,20],"p",{},[17,18,19],"code",{},"argparse"," helps you build Python programs that accept values from the command line.",[14,22,23],{},"This example shows one small, practical script so you can learn the basic idea without learning the whole module at once. You will see how to:",[25,26,27,31,34,37],"ul",{},[28,29,30],"li",{},"accept arguments after the script name",[28,32,33],{},"convert values to the right type",[28,35,36],{},"show built-in help",[28,38,39],{},"use an optional flag",[41,42,44],"h2",{"id":43},"quick-example","Quick example",[14,46,47],{},"Here is a simple script that adds two numbers:",[49,50,55],"pre",{"className":51,"code":52,"language":53,"meta":54,"style":54},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import argparse\n\nparser = argparse.ArgumentParser(description=\"Add two numbers\")\nparser.add_argument(\"x\", type=int, help=\"First number\")\nparser.add_argument(\"y\", type=int, help=\"Second number\")\nargs = parser.parse_args()\n\nprint(args.x + args.y)\n","python","",[17,56,57,70,77,120,168,209,228,233],{"__ignoreMap":54},[58,59,62,66],"span",{"class":60,"line":61},"line",1,[58,63,65],{"class":64},"sVHd0","import",[58,67,69],{"class":68},"su5hD"," argparse\n",[58,71,73],{"class":60,"line":72},2,[58,74,76],{"emptyLinePlaceholder":75},true,"\n",[58,78,80,83,87,90,94,98,101,105,107,111,115,117],{"class":60,"line":79},3,[58,81,82],{"class":68},"parser ",[58,84,86],{"class":85},"smGrS","=",[58,88,89],{"class":68}," argparse",[58,91,93],{"class":92},"sP7_E",".",[58,95,97],{"class":96},"slqww","ArgumentParser",[58,99,100],{"class":92},"(",[58,102,104],{"class":103},"s99_P","description",[58,106,86],{"class":85},[58,108,110],{"class":109},"sjJ54","\"",[58,112,114],{"class":113},"s_sjI","Add two numbers",[58,116,110],{"class":109},[58,118,119],{"class":92},")\n",[58,121,123,126,128,131,133,135,138,140,143,146,148,152,154,157,159,161,164,166],{"class":60,"line":122},4,[58,124,125],{"class":68},"parser",[58,127,93],{"class":92},[58,129,130],{"class":96},"add_argument",[58,132,100],{"class":92},[58,134,110],{"class":109},[58,136,137],{"class":113},"x",[58,139,110],{"class":109},[58,141,142],{"class":92},",",[58,144,145],{"class":103}," type",[58,147,86],{"class":85},[58,149,151],{"class":150},"sZMiF","int",[58,153,142],{"class":92},[58,155,156],{"class":103}," help",[58,158,86],{"class":85},[58,160,110],{"class":109},[58,162,163],{"class":113},"First number",[58,165,110],{"class":109},[58,167,119],{"class":92},[58,169,171,173,175,177,179,181,184,186,188,190,192,194,196,198,200,202,205,207],{"class":60,"line":170},5,[58,172,125],{"class":68},[58,174,93],{"class":92},[58,176,130],{"class":96},[58,178,100],{"class":92},[58,180,110],{"class":109},[58,182,183],{"class":113},"y",[58,185,110],{"class":109},[58,187,142],{"class":92},[58,189,145],{"class":103},[58,191,86],{"class":85},[58,193,151],{"class":150},[58,195,142],{"class":92},[58,197,156],{"class":103},[58,199,86],{"class":85},[58,201,110],{"class":109},[58,203,204],{"class":113},"Second number",[58,206,110],{"class":109},[58,208,119],{"class":92},[58,210,212,215,217,220,222,225],{"class":60,"line":211},6,[58,213,214],{"class":68},"args ",[58,216,86],{"class":85},[58,218,219],{"class":68}," parser",[58,221,93],{"class":92},[58,223,224],{"class":96},"parse_args",[58,226,227],{"class":92},"()\n",[58,229,231],{"class":60,"line":230},7,[58,232,76],{"emptyLinePlaceholder":75},[58,234,236,240,242,245,247,250,253,256,258,260],{"class":60,"line":235},8,[58,237,239],{"class":238},"sptTA","print",[58,241,100],{"class":92},[58,243,244],{"class":96},"args",[58,246,93],{"class":92},[58,248,137],{"class":249},"skxfh",[58,251,252],{"class":85}," +",[58,254,255],{"class":96}," args",[58,257,93],{"class":92},[58,259,183],{"class":249},[58,261,119],{"class":92},[14,263,264],{},"Run it like this:",[49,266,270],{"className":267,"code":268,"language":269,"meta":54,"style":54},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python app.py 3 5\n","bash",[17,271,272],{"__ignoreMap":54},[58,273,274,277,280,284],{"class":60,"line":61},[58,275,53],{"class":276},"sbgvK",[58,278,279],{"class":113}," app.py",[58,281,283],{"class":282},"srdBf"," 3",[58,285,286],{"class":282}," 5\n",[14,288,289],{},"Output:",[49,291,296],{"className":292,"code":294,"language":295,"meta":54},[293],"language-text","8\n","text",[17,297,294],{"__ignoreMap":54},[41,299,301],{"id":300},"what-this-example-does","What this example does",[14,303,304],{},"This example is small on purpose.",[14,306,307],{},"It shows how to build a real command line program that:",[25,309,310,316,319,322],{},[28,311,312,313,315],{},"uses ",[17,314,19],{}," to read values passed after the script name",[28,317,318],{},"expects two numbers from the user",[28,320,321],{},"converts those values to integers",[28,323,324],{},"prints the result",[14,326,327,328,330],{},"If you want a fast, working ",[17,329,19],{}," example, this is a good place to start.",[41,332,334],{"id":333},"when-to-use-argparse","When to use argparse",[14,336,337,338,340],{},"Use ",[17,339,19],{}," when you run a Python script from the terminal and want to pass extra values to it.",[14,342,343],{},"It is useful for scripts that need:",[25,345,346,349,352,355],{},[28,347,348],{},"file names",[28,350,351],{},"numbers",[28,353,354],{},"names",[28,356,357,358,361,362],{},"flags such as ",[17,359,360],{},"--verbose"," or ",[17,363,364],{},"--uppercase",[14,366,367,368,370],{},"For beginners, ",[17,369,19],{}," is usually better than reading raw arguments manually because it gives you:",[25,372,373,376,379],{},[28,374,375],{},"automatic help messages",[28,377,378],{},"type conversion",[28,380,381],{},"clearer error messages",[14,383,384,385,390,391,395],{},"If you are new to terminal arguments, you may also want to read ",[386,387,389],"a",{"href":388},"\u002Fhow-to\u002Fhow-to-use-command-line-arguments-in-python\u002F","how to use command line arguments in Python"," and ",[386,392,394],{"href":393},"\u002Fstandard-library\u002Fsys.argv-explained","sys.argv explained"," later.",[41,397,399],{"id":398},"main-parts-of-the-example","Main parts of the example",[14,401,402],{},"The script has four main steps.",[404,405,407,408,410],"h3",{"id":406},"_1-create-an-argumentparser-object","1. Create an ",[17,409,97],{}," object",[49,412,414],{"className":51,"code":413,"language":53,"meta":54,"style":54},"parser = argparse.ArgumentParser(description=\"Add two numbers\")\n",[17,415,416],{"__ignoreMap":54},[58,417,418,420,422,424,426,428,430,432,434,436,438,440],{"class":60,"line":61},[58,419,82],{"class":68},[58,421,86],{"class":85},[58,423,89],{"class":68},[58,425,93],{"class":92},[58,427,97],{"class":96},[58,429,100],{"class":92},[58,431,104],{"class":103},[58,433,86],{"class":85},[58,435,110],{"class":109},[58,437,114],{"class":113},[58,439,110],{"class":109},[58,441,119],{"class":92},[14,443,444],{},"This creates the parser and sets a short description.",[404,446,448],{"id":447},"_2-add-expected-arguments","2. Add expected arguments",[49,450,452],{"className":51,"code":451,"language":53,"meta":54,"style":54},"parser.add_argument(\"x\", type=int, help=\"First number\")\nparser.add_argument(\"y\", type=int, help=\"Second number\")\n",[17,453,454,492],{"__ignoreMap":54},[58,455,456,458,460,462,464,466,468,470,472,474,476,478,480,482,484,486,488,490],{"class":60,"line":61},[58,457,125],{"class":68},[58,459,93],{"class":92},[58,461,130],{"class":96},[58,463,100],{"class":92},[58,465,110],{"class":109},[58,467,137],{"class":113},[58,469,110],{"class":109},[58,471,142],{"class":92},[58,473,145],{"class":103},[58,475,86],{"class":85},[58,477,151],{"class":150},[58,479,142],{"class":92},[58,481,156],{"class":103},[58,483,86],{"class":85},[58,485,110],{"class":109},[58,487,163],{"class":113},[58,489,110],{"class":109},[58,491,119],{"class":92},[58,493,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522,524,526,528],{"class":60,"line":72},[58,495,125],{"class":68},[58,497,93],{"class":92},[58,499,130],{"class":96},[58,501,100],{"class":92},[58,503,110],{"class":109},[58,505,183],{"class":113},[58,507,110],{"class":109},[58,509,142],{"class":92},[58,511,145],{"class":103},[58,513,86],{"class":85},[58,515,151],{"class":150},[58,517,142],{"class":92},[58,519,156],{"class":103},[58,521,86],{"class":85},[58,523,110],{"class":109},[58,525,204],{"class":113},[58,527,110],{"class":109},[58,529,119],{"class":92},[14,531,532,533,537],{},"These lines define two ",[534,535,536],"strong",{},"positional arguments",":",[25,539,540,544],{},[28,541,542],{},[17,543,137],{},[28,545,546],{},[17,547,183],{},[14,549,550],{},"They are required, and the user must provide them in the correct order.",[404,552,554],{"id":553},"_3-parse-the-command-line-input","3. Parse the command line input",[49,556,558],{"className":51,"code":557,"language":53,"meta":54,"style":54},"args = parser.parse_args()\n",[17,559,560],{"__ignoreMap":54},[58,561,562,564,566,568,570,572],{"class":60,"line":61},[58,563,214],{"class":68},[58,565,86],{"class":85},[58,567,219],{"class":68},[58,569,93],{"class":92},[58,571,224],{"class":96},[58,573,227],{"class":92},[14,575,576],{},"This reads the values typed in the terminal.",[404,578,580],{"id":579},"_4-use-the-values","4. Use the values",[49,582,584],{"className":51,"code":583,"language":53,"meta":54,"style":54},"print(args.x + args.y)\n",[17,585,586],{"__ignoreMap":54},[58,587,588,590,592,594,596,598,600,602,604,606],{"class":60,"line":61},[58,589,239],{"class":238},[58,591,100],{"class":92},[58,593,244],{"class":96},[58,595,93],{"class":92},[58,597,137],{"class":249},[58,599,252],{"class":85},[58,601,255],{"class":96},[58,603,93],{"class":92},[58,605,183],{"class":249},[58,607,119],{"class":92},[14,609,610,611,390,614,93],{},"After parsing, you can access the values with names like ",[17,612,613],{},"args.x",[17,615,616],{},"args.y",[41,618,620],{"id":619},"example-walkthrough","Example walkthrough",[14,622,623],{},"Here is the full script again:",[49,625,626],{"className":51,"code":52,"language":53,"meta":54,"style":54},[17,627,628,634,638,664,702,740,754,758],{"__ignoreMap":54},[58,629,630,632],{"class":60,"line":61},[58,631,65],{"class":64},[58,633,69],{"class":68},[58,635,636],{"class":60,"line":72},[58,637,76],{"emptyLinePlaceholder":75},[58,639,640,642,644,646,648,650,652,654,656,658,660,662],{"class":60,"line":79},[58,641,82],{"class":68},[58,643,86],{"class":85},[58,645,89],{"class":68},[58,647,93],{"class":92},[58,649,97],{"class":96},[58,651,100],{"class":92},[58,653,104],{"class":103},[58,655,86],{"class":85},[58,657,110],{"class":109},[58,659,114],{"class":113},[58,661,110],{"class":109},[58,663,119],{"class":92},[58,665,666,668,670,672,674,676,678,680,682,684,686,688,690,692,694,696,698,700],{"class":60,"line":122},[58,667,125],{"class":68},[58,669,93],{"class":92},[58,671,130],{"class":96},[58,673,100],{"class":92},[58,675,110],{"class":109},[58,677,137],{"class":113},[58,679,110],{"class":109},[58,681,142],{"class":92},[58,683,145],{"class":103},[58,685,86],{"class":85},[58,687,151],{"class":150},[58,689,142],{"class":92},[58,691,156],{"class":103},[58,693,86],{"class":85},[58,695,110],{"class":109},[58,697,163],{"class":113},[58,699,110],{"class":109},[58,701,119],{"class":92},[58,703,704,706,708,710,712,714,716,718,720,722,724,726,728,730,732,734,736,738],{"class":60,"line":170},[58,705,125],{"class":68},[58,707,93],{"class":92},[58,709,130],{"class":96},[58,711,100],{"class":92},[58,713,110],{"class":109},[58,715,183],{"class":113},[58,717,110],{"class":109},[58,719,142],{"class":92},[58,721,145],{"class":103},[58,723,86],{"class":85},[58,725,151],{"class":150},[58,727,142],{"class":92},[58,729,156],{"class":103},[58,731,86],{"class":85},[58,733,110],{"class":109},[58,735,204],{"class":113},[58,737,110],{"class":109},[58,739,119],{"class":92},[58,741,742,744,746,748,750,752],{"class":60,"line":211},[58,743,214],{"class":68},[58,745,86],{"class":85},[58,747,219],{"class":68},[58,749,93],{"class":92},[58,751,224],{"class":96},[58,753,227],{"class":92},[58,755,756],{"class":60,"line":230},[58,757,76],{"emptyLinePlaceholder":75},[58,759,760,762,764,766,768,770,772,774,776,778],{"class":60,"line":235},[58,761,239],{"class":238},[58,763,100],{"class":92},[58,765,244],{"class":96},[58,767,93],{"class":92},[58,769,137],{"class":249},[58,771,252],{"class":85},[58,773,255],{"class":96},[58,775,93],{"class":92},[58,777,183],{"class":249},[58,779,119],{"class":92},[14,781,782],{},"What each part does:",[25,784,785,791,797,806,812],{},[28,786,787,790],{},[17,788,789],{},"description=\"Add two numbers\""," sets the help text shown to the user",[28,792,793,796],{},[17,794,795],{},"type=int"," converts the input from text to integers automatically",[28,798,799,390,802,805],{},[17,800,801],{},"help=\"First number\"",[17,803,804],{},"help=\"Second number\""," describe each argument",[28,807,808,811],{},[17,809,810],{},"args = parser.parse_args()"," reads the command line input",[28,813,814,817],{},[17,815,816],{},"print(args.x + args.y)"," uses the parsed values",[14,819,820,821,823],{},"Without ",[17,822,795],{},", the arguments would be read as text.",[14,825,826,827,390,830,833,834,836,837,93],{},"For example, ",[17,828,829],{},"\"3\"",[17,831,832],{},"\"5\""," are strings at first. ",[17,835,19],{}," converts them for you when you use ",[17,838,795],{},[41,840,842],{"id":841},"positional-arguments-vs-optional-arguments","Positional arguments vs optional arguments",[14,844,845,846,93],{},"There are two common kinds of arguments in ",[17,847,19],{},[404,849,851],{"id":850},"positional-arguments","Positional arguments",[14,853,854],{},"Positional arguments:",[25,856,857,860,863],{},[28,858,859],{},"are usually required",[28,861,862],{},"are given by order",[28,864,865],{},"do not use dashes",[14,867,868],{},"Example:",[49,870,872],{"className":51,"code":871,"language":53,"meta":54,"style":54},"parser.add_argument(\"name\")\n",[17,873,874],{"__ignoreMap":54},[58,875,876,878,880,882,884,886,889,891],{"class":60,"line":61},[58,877,125],{"class":68},[58,879,93],{"class":92},[58,881,130],{"class":96},[58,883,100],{"class":92},[58,885,110],{"class":109},[58,887,888],{"class":113},"name",[58,890,110],{"class":109},[58,892,119],{"class":92},[14,894,895],{},"Run:",[49,897,899],{"className":267,"code":898,"language":269,"meta":54,"style":54},"python app.py Alice\n",[17,900,901],{"__ignoreMap":54},[58,902,903,905,907],{"class":60,"line":61},[58,904,53],{"class":276},[58,906,279],{"class":113},[58,908,909],{"class":113}," Alice\n",[404,911,913],{"id":912},"optional-arguments","Optional arguments",[14,915,916],{},"Optional arguments:",[25,918,919,928,931],{},[28,920,921,922,361,925],{},"usually start with ",[17,923,924],{},"-",[17,926,927],{},"--",[28,929,930],{},"are often used for settings or switches",[28,932,933],{},"do not have to be provided unless you make them required",[14,935,868],{},[49,937,939],{"className":51,"code":938,"language":53,"meta":54,"style":54},"parser.add_argument(\"--uppercase\")\n",[17,940,941],{"__ignoreMap":54},[58,942,943,945,947,949,951,953,955,957],{"class":60,"line":61},[58,944,125],{"class":68},[58,946,93],{"class":92},[58,948,130],{"class":96},[58,950,100],{"class":92},[58,952,110],{"class":109},[58,954,364],{"class":113},[58,956,110],{"class":109},[58,958,119],{"class":92},[14,960,895],{},[49,962,964],{"className":267,"code":963,"language":269,"meta":54,"style":54},"python app.py Alice --uppercase yes\n",[17,965,966],{"__ignoreMap":54},[58,967,968,970,972,975,979],{"class":60,"line":61},[58,969,53],{"class":276},[58,971,279],{"class":113},[58,973,974],{"class":113}," Alice",[58,976,978],{"class":977},"stzsN"," --uppercase",[58,980,981],{"class":113}," yes\n",[14,983,984],{},"For beginners, the main idea is simple:",[25,986,987,990],{},[28,988,989],{},"use positional arguments for required input",[28,991,992],{},"use optional arguments for extra behavior",[41,994,996],{"id":995},"second-example-with-an-optional-flag","Second example with an optional flag",[14,998,999,1000,1002],{},"Now let's build a second script that accepts a name and an optional ",[17,1001,364],{}," flag.",[49,1004,1006],{"className":51,"code":1005,"language":53,"meta":54,"style":54},"import argparse\n\nparser = argparse.ArgumentParser(description=\"Greet a user\")\nparser.add_argument(\"name\", help=\"The person's name\")\nparser.add_argument(\n    \"--uppercase\",\n    action=\"store_true\",\n    help=\"Show the greeting in uppercase\"\n)\n\nargs = parser.parse_args()\n\nmessage = f\"Hello, {args.name}\"\n\nif args.uppercase:\n    message = message.upper()\n\nprint(message)\n",[17,1007,1008,1014,1018,1045,1076,1087,1099,1115,1130,1135,1140,1155,1160,1189,1194,1210,1228,1233],{"__ignoreMap":54},[58,1009,1010,1012],{"class":60,"line":61},[58,1011,65],{"class":64},[58,1013,69],{"class":68},[58,1015,1016],{"class":60,"line":72},[58,1017,76],{"emptyLinePlaceholder":75},[58,1019,1020,1022,1024,1026,1028,1030,1032,1034,1036,1038,1041,1043],{"class":60,"line":79},[58,1021,82],{"class":68},[58,1023,86],{"class":85},[58,1025,89],{"class":68},[58,1027,93],{"class":92},[58,1029,97],{"class":96},[58,1031,100],{"class":92},[58,1033,104],{"class":103},[58,1035,86],{"class":85},[58,1037,110],{"class":109},[58,1039,1040],{"class":113},"Greet a user",[58,1042,110],{"class":109},[58,1044,119],{"class":92},[58,1046,1047,1049,1051,1053,1055,1057,1059,1061,1063,1065,1067,1069,1072,1074],{"class":60,"line":122},[58,1048,125],{"class":68},[58,1050,93],{"class":92},[58,1052,130],{"class":96},[58,1054,100],{"class":92},[58,1056,110],{"class":109},[58,1058,888],{"class":113},[58,1060,110],{"class":109},[58,1062,142],{"class":92},[58,1064,156],{"class":103},[58,1066,86],{"class":85},[58,1068,110],{"class":109},[58,1070,1071],{"class":113},"The person's name",[58,1073,110],{"class":109},[58,1075,119],{"class":92},[58,1077,1078,1080,1082,1084],{"class":60,"line":170},[58,1079,125],{"class":68},[58,1081,93],{"class":92},[58,1083,130],{"class":96},[58,1085,1086],{"class":92},"(\n",[58,1088,1089,1092,1094,1096],{"class":60,"line":211},[58,1090,1091],{"class":109},"    \"",[58,1093,364],{"class":113},[58,1095,110],{"class":109},[58,1097,1098],{"class":92},",\n",[58,1100,1101,1104,1106,1108,1111,1113],{"class":60,"line":230},[58,1102,1103],{"class":103},"    action",[58,1105,86],{"class":85},[58,1107,110],{"class":109},[58,1109,1110],{"class":113},"store_true",[58,1112,110],{"class":109},[58,1114,1098],{"class":92},[58,1116,1117,1120,1122,1124,1127],{"class":60,"line":235},[58,1118,1119],{"class":103},"    help",[58,1121,86],{"class":85},[58,1123,110],{"class":109},[58,1125,1126],{"class":113},"Show the greeting in uppercase",[58,1128,1129],{"class":109},"\"\n",[58,1131,1133],{"class":60,"line":1132},9,[58,1134,119],{"class":92},[58,1136,1138],{"class":60,"line":1137},10,[58,1139,76],{"emptyLinePlaceholder":75},[58,1141,1143,1145,1147,1149,1151,1153],{"class":60,"line":1142},11,[58,1144,214],{"class":68},[58,1146,86],{"class":85},[58,1148,219],{"class":68},[58,1150,93],{"class":92},[58,1152,224],{"class":96},[58,1154,227],{"class":92},[58,1156,1158],{"class":60,"line":1157},12,[58,1159,76],{"emptyLinePlaceholder":75},[58,1161,1163,1166,1168,1172,1175,1178,1180,1182,1184,1187],{"class":60,"line":1162},13,[58,1164,1165],{"class":68},"message ",[58,1167,86],{"class":85},[58,1169,1171],{"class":1170},"sbsja"," f",[58,1173,1174],{"class":113},"\"Hello, ",[58,1176,1177],{"class":282},"{",[58,1179,244],{"class":68},[58,1181,93],{"class":92},[58,1183,888],{"class":249},[58,1185,1186],{"class":282},"}",[58,1188,1129],{"class":113},[58,1190,1192],{"class":60,"line":1191},14,[58,1193,76],{"emptyLinePlaceholder":75},[58,1195,1197,1200,1202,1204,1207],{"class":60,"line":1196},15,[58,1198,1199],{"class":64},"if",[58,1201,255],{"class":68},[58,1203,93],{"class":92},[58,1205,1206],{"class":249},"uppercase",[58,1208,1209],{"class":92},":\n",[58,1211,1213,1216,1218,1221,1223,1226],{"class":60,"line":1212},16,[58,1214,1215],{"class":68},"    message ",[58,1217,86],{"class":85},[58,1219,1220],{"class":68}," message",[58,1222,93],{"class":92},[58,1224,1225],{"class":96},"upper",[58,1227,227],{"class":92},[58,1229,1231],{"class":60,"line":1230},17,[58,1232,76],{"emptyLinePlaceholder":75},[58,1234,1236,1238,1240,1243],{"class":60,"line":1235},18,[58,1237,239],{"class":238},[58,1239,100],{"class":92},[58,1241,1242],{"class":96},"message",[58,1244,119],{"class":92},[404,1246,1248],{"id":1247},"how-this-works","How this works",[14,1250,1251],{},"The important new part is:",[49,1253,1255],{"className":51,"code":1254,"language":53,"meta":54,"style":54},"action=\"store_true\"\n",[17,1256,1257],{"__ignoreMap":54},[58,1258,1259,1262,1264,1266,1268],{"class":60,"line":61},[58,1260,1261],{"class":68},"action",[58,1263,86],{"class":85},[58,1265,110],{"class":109},[58,1267,1110],{"class":113},[58,1269,1129],{"class":109},[14,1271,1272],{},"This means:",[25,1274,1275,1288],{},[28,1276,1277,1278,1280,1281,1284,1285],{},"if ",[17,1279,364],{}," is included, ",[17,1282,1283],{},"args.uppercase"," becomes ",[17,1286,1287],{},"True",[28,1289,1290,1291,1284,1293],{},"if it is not included, ",[17,1292,1283],{},[17,1294,1295],{},"False",[404,1297,1299],{"id":1298},"run-it-without-the-flag","Run it without the flag",[49,1301,1302],{"className":267,"code":898,"language":269,"meta":54,"style":54},[17,1303,1304],{"__ignoreMap":54},[58,1305,1306,1308,1310],{"class":60,"line":61},[58,1307,53],{"class":276},[58,1309,279],{"class":113},[58,1311,909],{"class":113},[14,1313,289],{},[49,1315,1318],{"className":1316,"code":1317,"language":295,"meta":54},[293],"Hello, Alice\n",[17,1319,1317],{"__ignoreMap":54},[404,1321,1323],{"id":1322},"run-it-with-the-flag","Run it with the flag",[49,1325,1327],{"className":267,"code":1326,"language":269,"meta":54,"style":54},"python app.py Alice --uppercase\n",[17,1328,1329],{"__ignoreMap":54},[58,1330,1331,1333,1335,1337],{"class":60,"line":61},[58,1332,53],{"class":276},[58,1334,279],{"class":113},[58,1336,974],{"class":113},[58,1338,1339],{"class":977}," --uppercase\n",[14,1341,289],{},[49,1343,1346],{"className":1344,"code":1345,"language":295,"meta":54},[293],"HELLO, ALICE\n",[17,1347,1345],{"__ignoreMap":54},[14,1349,1350],{},"This is a common pattern for command line tools.",[41,1352,1354],{"id":1353},"how-to-run-the-script","How to run the script",[14,1356,1357,1358,93],{},"Save one of the examples in a file named ",[17,1359,1360],{},"app.py",[14,1362,1363],{},"Then open a terminal in the same folder.",[404,1365,1367],{"id":1366},"example-1-add-two-numbers","Example 1: add two numbers",[49,1369,1370],{"className":267,"code":268,"language":269,"meta":54,"style":54},[17,1371,1372],{"__ignoreMap":54},[58,1373,1374,1376,1378,1380],{"class":60,"line":61},[58,1375,53],{"class":276},[58,1377,279],{"class":113},[58,1379,283],{"class":282},[58,1381,286],{"class":282},[14,1383,289],{},[49,1385,1387],{"className":1386,"code":294,"language":295,"meta":54},[293],[17,1388,294],{"__ignoreMap":54},[404,1390,1392],{"id":1391},"example-2-wrong-type","Example 2: wrong type",[49,1394,1396],{"className":267,"code":1395,"language":269,"meta":54,"style":54},"python app.py hello 5\n",[17,1397,1398],{"__ignoreMap":54},[58,1399,1400,1402,1404,1407],{"class":60,"line":61},[58,1401,53],{"class":276},[58,1403,279],{"class":113},[58,1405,1406],{"class":113}," hello",[58,1408,286],{"class":282},[14,1410,1411,1412,1414,1415,1418,1419,93],{},"If your script expects integers, ",[17,1413,19],{}," will show an error because ",[17,1416,1417],{},"hello"," cannot be converted to ",[17,1420,151],{},[404,1422,1424],{"id":1423},"example-3-greeting-script","Example 3: greeting script",[49,1426,1427],{"className":267,"code":1326,"language":269,"meta":54,"style":54},[17,1428,1429],{"__ignoreMap":54},[58,1430,1431,1433,1435,1437],{"class":60,"line":61},[58,1432,53],{"class":276},[58,1434,279],{"class":113},[58,1436,974],{"class":113},[58,1438,1339],{"class":977},[14,1440,289],{},[49,1442,1444],{"className":1443,"code":1345,"language":295,"meta":54},[293],[17,1445,1345],{"__ignoreMap":54},[14,1447,1448,1449,93],{},"If you are not sure how terminal input differs from normal keyboard input in a script, compare this with ",[386,1450,1452],{"href":1451},"\u002Fhow-to\u002Fhow-to-get-user-input-in-python\u002F","how to get user input in Python",[41,1454,1456],{"id":1455},"common-beginner-mistakes","Common beginner mistakes",[14,1458,1459,1460,93],{},"These are some common problems when learning ",[17,1461,19],{},[404,1463,1465],{"id":1464},"forgetting-required-positional-arguments","Forgetting required positional arguments",[14,1467,1468,1469,390,1471,1473],{},"If your script expects ",[17,1470,137],{},[17,1472,183],{},", this will fail:",[49,1475,1477],{"className":267,"code":1476,"language":269,"meta":54,"style":54},"python app.py 3\n",[17,1478,1479],{"__ignoreMap":54},[58,1480,1481,1483,1485],{"class":60,"line":61},[58,1482,53],{"class":276},[58,1484,279],{"class":113},[58,1486,1487],{"class":282}," 3\n",[14,1489,1490,1492],{},[17,1491,19],{}," will show an error because one required argument is missing.",[404,1494,1496,1497],{"id":1495},"passing-text-when-the-script-expects-int","Passing text when the script expects ",[17,1498,151],{},[14,1500,1501,1502,1473],{},"If you use ",[17,1503,795],{},[49,1505,1506],{"className":267,"code":1395,"language":269,"meta":54,"style":54},[17,1507,1508],{"__ignoreMap":54},[58,1509,1510,1512,1514,1516],{"class":60,"line":61},[58,1511,53],{"class":276},[58,1513,279],{"class":113},[58,1515,1406],{"class":113},[58,1517,286],{"class":282},[14,1519,1520,1521,1523,1524,93],{},"That happens because ",[17,1522,1417],{}," cannot be converted to an integer. A related error you may see in other code is ",[386,1525,1527],{"href":1526},"\u002Ferrors\u002Fvalueerror-invalid-literal-for-int-with-base-10-fix\u002F","ValueError: invalid literal for int() with base 10",[404,1529,1531],{"id":1530},"misspelling-an-optional-flag","Misspelling an optional flag",[14,1533,1534,1535,1537,1538,361,1541,1544],{},"If the flag is ",[17,1536,364],{},", then ",[17,1539,1540],{},"--upper-case",[17,1542,1543],{},"--upper"," will not work unless you defined them.",[14,1546,1547],{},"Always type the flag name exactly as written in the script.",[404,1549,1551,1552,1554,1555],{"id":1550},"trying-to-use-args-before-calling-parse_args","Trying to use ",[17,1553,244],{}," before calling ",[17,1556,1557],{},"parse_args()",[14,1559,1560],{},"This is wrong:",[49,1562,1564],{"className":51,"code":1563,"language":53,"meta":54,"style":54},"import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument(\"x\", type=int)\n\nprint(args.x)  # args does not exist yet\nargs = parser.parse_args()\n",[17,1565,1566,1572,1576,1590,1616,1620,1639],{"__ignoreMap":54},[58,1567,1568,1570],{"class":60,"line":61},[58,1569,65],{"class":64},[58,1571,69],{"class":68},[58,1573,1574],{"class":60,"line":72},[58,1575,76],{"emptyLinePlaceholder":75},[58,1577,1578,1580,1582,1584,1586,1588],{"class":60,"line":79},[58,1579,82],{"class":68},[58,1581,86],{"class":85},[58,1583,89],{"class":68},[58,1585,93],{"class":92},[58,1587,97],{"class":96},[58,1589,227],{"class":92},[58,1591,1592,1594,1596,1598,1600,1602,1604,1606,1608,1610,1612,1614],{"class":60,"line":122},[58,1593,125],{"class":68},[58,1595,93],{"class":92},[58,1597,130],{"class":96},[58,1599,100],{"class":92},[58,1601,110],{"class":109},[58,1603,137],{"class":113},[58,1605,110],{"class":109},[58,1607,142],{"class":92},[58,1609,145],{"class":103},[58,1611,86],{"class":85},[58,1613,151],{"class":150},[58,1615,119],{"class":92},[58,1617,1618],{"class":60,"line":170},[58,1619,76],{"emptyLinePlaceholder":75},[58,1621,1622,1624,1626,1628,1630,1632,1635],{"class":60,"line":211},[58,1623,239],{"class":238},[58,1625,100],{"class":92},[58,1627,244],{"class":96},[58,1629,93],{"class":92},[58,1631,137],{"class":249},[58,1633,1634],{"class":92},")",[58,1636,1638],{"class":1637},"sutJx","  # args does not exist yet\n",[58,1640,1641,1643,1645,1647,1649,1651],{"class":60,"line":230},[58,1642,214],{"class":68},[58,1644,86],{"class":85},[58,1646,219],{"class":68},[58,1648,93],{"class":92},[58,1650,224],{"class":96},[58,1652,227],{"class":92},[14,1654,1655],{},"You must parse first:",[49,1657,1659],{"className":51,"code":1658,"language":53,"meta":54,"style":54},"import argparse\n\nparser = argparse.ArgumentParser()\nparser.add_argument(\"x\", type=int)\n\nargs = parser.parse_args()\nprint(args.x)\n",[17,1660,1661,1667,1671,1685,1711,1715,1729],{"__ignoreMap":54},[58,1662,1663,1665],{"class":60,"line":61},[58,1664,65],{"class":64},[58,1666,69],{"class":68},[58,1668,1669],{"class":60,"line":72},[58,1670,76],{"emptyLinePlaceholder":75},[58,1672,1673,1675,1677,1679,1681,1683],{"class":60,"line":79},[58,1674,82],{"class":68},[58,1676,86],{"class":85},[58,1678,89],{"class":68},[58,1680,93],{"class":92},[58,1682,97],{"class":96},[58,1684,227],{"class":92},[58,1686,1687,1689,1691,1693,1695,1697,1699,1701,1703,1705,1707,1709],{"class":60,"line":122},[58,1688,125],{"class":68},[58,1690,93],{"class":92},[58,1692,130],{"class":96},[58,1694,100],{"class":92},[58,1696,110],{"class":109},[58,1698,137],{"class":113},[58,1700,110],{"class":109},[58,1702,142],{"class":92},[58,1704,145],{"class":103},[58,1706,86],{"class":85},[58,1708,151],{"class":150},[58,1710,119],{"class":92},[58,1712,1713],{"class":60,"line":170},[58,1714,76],{"emptyLinePlaceholder":75},[58,1716,1717,1719,1721,1723,1725,1727],{"class":60,"line":211},[58,1718,214],{"class":68},[58,1720,86],{"class":85},[58,1722,219],{"class":68},[58,1724,93],{"class":92},[58,1726,224],{"class":96},[58,1728,227],{"class":92},[58,1730,1731,1733,1735,1737,1739,1741],{"class":60,"line":230},[58,1732,239],{"class":238},[58,1734,100],{"class":92},[58,1736,244],{"class":96},[58,1738,93],{"class":92},[58,1740,137],{"class":249},[58,1742,119],{"class":92},[41,1744,1746],{"id":1745},"how-to-read-the-help-output","How to read the help output",[14,1748,1749,1750,1752],{},"One of the best things about ",[17,1751,19],{}," is that it builds help text automatically.",[14,1754,895],{},[49,1756,1758],{"className":267,"code":1757,"language":269,"meta":54,"style":54},"python app.py --help\n",[17,1759,1760],{"__ignoreMap":54},[58,1761,1762,1764,1766],{"class":60,"line":61},[58,1763,53],{"class":276},[58,1765,279],{"class":113},[58,1767,1768],{"class":977}," --help\n",[14,1770,1771],{},"You will see output similar to this:",[49,1773,1776],{"className":1774,"code":1775,"language":295,"meta":54},[293],"usage: app.py [-h] x y\n\nAdd two numbers\n\npositional arguments:\n  x           First number\n  y           Second number\n\noptions:\n  -h, --help  show this help message and exit\n",[17,1777,1775],{"__ignoreMap":54},[14,1779,1780],{},"This help output includes:",[25,1782,1783,1790,1798],{},[28,1784,1785,1786,1789],{},"the ",[534,1787,1788],{},"usage"," line, which shows how to run the script",[28,1791,1785,1792,1794,1795],{},[534,1793,104],{},", which comes from ",[17,1796,1797],{},"ArgumentParser(...)",[28,1799,1785,1800,1794,1803],{},[534,1801,1802],{},"argument help text",[17,1804,1805],{},"help=...",[14,1807,1808,1809,1811,1812,93],{},"This is one reason ",[17,1810,19],{}," is easier to use than reading raw values from ",[386,1813,1814],{"href":393},[17,1815,1816],{},"sys.argv",[41,1818,1820],{"id":1819},"what-to-learn-next","What to learn next",[14,1822,1823],{},"After this example, a good next step is to learn the bigger picture of command line arguments.",[14,1825,1826],{},"You can continue with:",[25,1828,1829,1833,1837,1843],{},[28,1830,1831],{},[386,1832,389],{"href":388},[28,1834,1835],{},[386,1836,394],{"href":393},[28,1838,1839],{},[386,1840,1842],{"href":1841},"\u002Fstandard-library\u002Fpython-sys-module-overview\u002F","Python sys module overview",[28,1844,1845],{},[386,1846,1848],{"href":1847},"\u002Fexamples\u002Fpython-command-line-tool-example\u002F","Python command line tool example",[41,1850,1852],{"id":1851},"faq","FAQ",[404,1854,1856],{"id":1855},"what-is-argparse-in-python","What is argparse in Python?",[14,1858,1859,1861],{},[17,1860,19],{}," is a standard library module for reading command line arguments and options.",[404,1863,1865],{"id":1864},"what-is-the-difference-between-positional-and-optional-arguments","What is the difference between positional and optional arguments?",[14,1867,1868,1869,93],{},"Positional arguments are required and depend on order. Optional arguments use flags like ",[17,1870,1871],{},"--name",[404,1873,1875],{"id":1874},"why-use-argparse-instead-of-sysargv","Why use argparse instead of sys.argv?",[14,1877,1878,1880],{},[17,1879,19],{}," is easier for beginners because it adds help messages, type conversion, and clearer error messages.",[404,1882,1884],{"id":1883},"how-do-i-show-the-help-message","How do I show the help message?",[14,1886,1887,1888,1891],{},"Run the script with ",[17,1889,1890],{},"--help",", such as:",[49,1893,1894],{"className":267,"code":1757,"language":269,"meta":54,"style":54},[17,1895,1896],{"__ignoreMap":54},[58,1897,1898,1900,1902],{"class":60,"line":61},[58,1899,53],{"class":276},[58,1901,279],{"class":113},[58,1903,1768],{"class":977},[404,1905,1907],{"id":1906},"can-argparse-convert-input-types-automatically","Can argparse convert input types automatically?",[14,1909,1910,1911,1913],{},"Yes. For example, ",[17,1912,795],{}," converts an argument to an integer.",[41,1915,1917],{"id":1916},"see-also","See also",[25,1919,1920,1925,1929,1933,1938,1943,1949],{},[28,1921,1922],{},[386,1923,1924],{"href":388},"How to use command line arguments in Python",[28,1926,1927],{},[386,1928,1842],{"href":1841},[28,1930,1931],{},[386,1932,394],{"href":393},[28,1934,1935],{},[386,1936,1937],{"href":1451},"How to get user input in Python",[28,1939,1940],{},[386,1941,1942],{"href":1526},"ValueError: invalid literal for int() with base 10 fix",[28,1944,1945],{},[386,1946,1948],{"href":1947},"\u002Ferrors\u002Ftypeerror-str-object-cannot-be-interpreted-as-an-integer-fix\u002F","TypeError: str object cannot be interpreted as an integer fix",[28,1950,1951],{},[386,1952,1848],{"href":1847},[1954,1955,1956],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}",{"title":54,"searchDepth":72,"depth":72,"links":1958},[1959,1960,1961,1962,1969,1970,1974,1979,1984,1992,1993,1994,2001],{"id":43,"depth":72,"text":44},{"id":300,"depth":72,"text":301},{"id":333,"depth":72,"text":334},{"id":398,"depth":72,"text":399,"children":1963},[1964,1966,1967,1968],{"id":406,"depth":79,"text":1965},"1. Create an ArgumentParser object",{"id":447,"depth":79,"text":448},{"id":553,"depth":79,"text":554},{"id":579,"depth":79,"text":580},{"id":619,"depth":72,"text":620},{"id":841,"depth":72,"text":842,"children":1971},[1972,1973],{"id":850,"depth":79,"text":851},{"id":912,"depth":79,"text":913},{"id":995,"depth":72,"text":996,"children":1975},[1976,1977,1978],{"id":1247,"depth":79,"text":1248},{"id":1298,"depth":79,"text":1299},{"id":1322,"depth":79,"text":1323},{"id":1353,"depth":72,"text":1354,"children":1980},[1981,1982,1983],{"id":1366,"depth":79,"text":1367},{"id":1391,"depth":79,"text":1392},{"id":1423,"depth":79,"text":1424},{"id":1455,"depth":72,"text":1456,"children":1985},[1986,1987,1989,1990],{"id":1464,"depth":79,"text":1465},{"id":1495,"depth":79,"text":1988},"Passing text when the script expects int",{"id":1530,"depth":79,"text":1531},{"id":1550,"depth":79,"text":1991},"Trying to use args before calling parse_args()",{"id":1745,"depth":72,"text":1746},{"id":1819,"depth":72,"text":1820},{"id":1851,"depth":72,"text":1852,"children":1995},[1996,1997,1998,1999,2000],{"id":1855,"depth":79,"text":1856},{"id":1864,"depth":79,"text":1865},{"id":1874,"depth":79,"text":1875},{"id":1883,"depth":79,"text":1884},{"id":1906,"depth":79,"text":1907},{"id":1916,"depth":72,"text":1917},"Master python argument parser example argparse in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-argument-parser-example-argparse",{"title":5,"description":2002},"examples\u002Fpython-argument-parser-example-argparse","XOFn2h8Ncn1JP5lb1WbxpiWX_7k4DjdQz1kPspE7000",1777585474890]