[{"data":1,"prerenderedAt":656},["ShallowReactive",2],{"i-lucide:sun-moon":3,"i-lucide:menu":8,"post-\u002Fblog\u002F2022-10-01-using-python-argparse":10,"surround-\u002Fblog\u002F2022-10-01-using-python-argparse":639,"i-lucide:arrow-left":650,"i-lucide:sparkles":652,"i-lucide:user":654},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M12 2v2m2.837 12.385a6 6 0 1 1-7.223-7.222c.624-.147.97.66.715 1.248a4 4 0 0 0 5.26 5.259c.589-.255 1.396.09 1.248.715M16 12a4 4 0 0 0-4-4m7-3l-1.256 1.256M20 12h2\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M4 5h16M4 12h16M4 19h16\"\u002F>",{"id":11,"title":12,"authors":13,"body":16,"date":627,"description":22,"extension":628,"meta":629,"navigation":91,"path":632,"seo":633,"stem":634,"tags":635,"toc":91,"__hash__":638},"blog\u002Fblog\u002F2022-10-01-using-python-argparse.md","Using Python argparse",[14,15],"gpt","pfarmer",{"type":17,"value":18,"toc":621},"minimark",[19,23,28,31,34,53,56,60,63,235,238,241,245,248,372,375,379,382,605,608,611,614,617],[20,21,22],"p",{},"Python 3.11 introduced a new and improved argument parser in the argparse module. The new argument parser provides several handy features that make it easier to parse command line arguments in Python scripts. In this post, we'll take a look at how to use Python's new argument parser.",[24,25,27],"h2",{"id":26},"background","Background",[20,29,30],{},"The argparse module has been around for a while in Python, providing a standard way to handle command line arguments. The old parser was good, but had some annoyances and limitations.",[20,32,33],{},"The new parser in Python 3.11 aims to resolve many of those issues and provide a more powerful and flexible interface for parsing arguments. Some of the major improvements include:",[35,36,37,41,44,47,50],"ul",{},[38,39,40],"li",{},"Support for subcommands",[38,42,43],{},"Ability to define argument groups",[38,45,46],{},"More flexible naming and prefixes",[38,48,49],{},"Better help messages",[38,51,52],{},"Validation of argument types",[20,54,55],{},"Overall, the new parser makes it simpler and more intuitive to build robust command line interfaces in Python.",[24,57,59],{"id":58},"basic-usage","Basic Usage",[20,61,62],{},"Let's look at a simple example of how to use the new argument parser. We'll write a Python script that takes an input file, output file, and an optional verbosity flag:",[64,65,70],"pre",{"className":66,"code":67,"language":68,"meta":69,"style":69},"language-python shiki shiki-themes github-light github-dark-default","import argparse\n\nparser = argparse.ArgumentParser()\n\nparser.add_argument(\"input\", help=\"input file\")\nparser.add_argument(\"output\", help=\"output file\")\nparser.add_argument(\"-v\", \"--verbose\", action=\"store_true\", help=\"increase verbosity\")\n\nargs = parser.parse_args()\n\nprint(args.input)\nprint(args.output)\nprint(args.verbose)\n","python","",[71,72,73,86,93,105,110,135,154,188,193,204,209,219,227],"code",{"__ignoreMap":69},[74,75,78,82],"span",{"class":76,"line":77},"line",1,[74,79,81],{"class":80},"sDgYj","import",[74,83,85],{"class":84},"szmH1"," argparse\n",[74,87,89],{"class":76,"line":88},2,[74,90,92],{"emptyLinePlaceholder":91},true,"\n",[74,94,96,99,102],{"class":76,"line":95},3,[74,97,98],{"class":84},"parser ",[74,100,101],{"class":80},"=",[74,103,104],{"class":84}," argparse.ArgumentParser()\n",[74,106,108],{"class":76,"line":107},4,[74,109,92],{"emptyLinePlaceholder":91},[74,111,113,116,120,123,127,129,132],{"class":76,"line":112},5,[74,114,115],{"class":84},"parser.add_argument(",[74,117,119],{"class":118},"sNbzA","\"input\"",[74,121,122],{"class":84},", ",[74,124,126],{"class":125},"sjlk-","help",[74,128,101],{"class":80},[74,130,131],{"class":118},"\"input file\"",[74,133,134],{"class":84},")\n",[74,136,138,140,143,145,147,149,152],{"class":76,"line":137},6,[74,139,115],{"class":84},[74,141,142],{"class":118},"\"output\"",[74,144,122],{"class":84},[74,146,126],{"class":125},[74,148,101],{"class":80},[74,150,151],{"class":118},"\"output file\"",[74,153,134],{"class":84},[74,155,157,159,162,164,167,169,172,174,177,179,181,183,186],{"class":76,"line":156},7,[74,158,115],{"class":84},[74,160,161],{"class":118},"\"-v\"",[74,163,122],{"class":84},[74,165,166],{"class":118},"\"--verbose\"",[74,168,122],{"class":84},[74,170,171],{"class":125},"action",[74,173,101],{"class":80},[74,175,176],{"class":118},"\"store_true\"",[74,178,122],{"class":84},[74,180,126],{"class":125},[74,182,101],{"class":80},[74,184,185],{"class":118},"\"increase verbosity\"",[74,187,134],{"class":84},[74,189,191],{"class":76,"line":190},8,[74,192,92],{"emptyLinePlaceholder":91},[74,194,196,199,201],{"class":76,"line":195},9,[74,197,198],{"class":84},"args ",[74,200,101],{"class":80},[74,202,203],{"class":84}," parser.parse_args()\n",[74,205,207],{"class":76,"line":206},10,[74,208,92],{"emptyLinePlaceholder":91},[74,210,212,216],{"class":76,"line":211},11,[74,213,215],{"class":214},"sPgVT","print",[74,217,218],{"class":84},"(args.input)\n",[74,220,222,224],{"class":76,"line":221},12,[74,223,215],{"class":214},[74,225,226],{"class":84},"(args.output)\n",[74,228,230,232],{"class":76,"line":229},13,[74,231,215],{"class":214},[74,233,234],{"class":84},"(args.verbose)\n",[20,236,237],{},"We first create an ArgumentParser object and then add arguments using the add_argument() method. We can specify parameters like help text, default values, and more. The parse_args() method parses the command line and returns an object with the values.",[20,239,240],{},"This provides an easy way to access the argument values in a structured way.",[24,242,244],{"id":243},"argument-groups","Argument Groups",[20,246,247],{},"For additional structure, we can now add argument groups:",[64,249,251],{"className":66,"code":250,"language":68,"meta":69,"style":69},"parser = argparse.ArgumentParser()\n\ninput_group = parser.add_argument_group(\"Input Options\")\ninput_group.add_argument(\"input\", help=\"input file\")\n\noutput_group = parser.add_argument_group(\"Output Options\")  \noutput_group.add_argument(\"output\", help=\"output file\")\n\nother_group = parser.add_argument_group(\"Other Options\")\nother_group.add_argument(\"-v\", \"--verbose\", action=\"store_true\")\n",[71,252,253,261,265,280,297,301,316,333,337,351],{"__ignoreMap":69},[74,254,255,257,259],{"class":76,"line":77},[74,256,98],{"class":84},[74,258,101],{"class":80},[74,260,104],{"class":84},[74,262,263],{"class":76,"line":88},[74,264,92],{"emptyLinePlaceholder":91},[74,266,267,270,272,275,278],{"class":76,"line":95},[74,268,269],{"class":84},"input_group ",[74,271,101],{"class":80},[74,273,274],{"class":84}," parser.add_argument_group(",[74,276,277],{"class":118},"\"Input Options\"",[74,279,134],{"class":84},[74,281,282,285,287,289,291,293,295],{"class":76,"line":107},[74,283,284],{"class":84},"input_group.add_argument(",[74,286,119],{"class":118},[74,288,122],{"class":84},[74,290,126],{"class":125},[74,292,101],{"class":80},[74,294,131],{"class":118},[74,296,134],{"class":84},[74,298,299],{"class":76,"line":112},[74,300,92],{"emptyLinePlaceholder":91},[74,302,303,306,308,310,313],{"class":76,"line":137},[74,304,305],{"class":84},"output_group ",[74,307,101],{"class":80},[74,309,274],{"class":84},[74,311,312],{"class":118},"\"Output Options\"",[74,314,315],{"class":84},")  \n",[74,317,318,321,323,325,327,329,331],{"class":76,"line":156},[74,319,320],{"class":84},"output_group.add_argument(",[74,322,142],{"class":118},[74,324,122],{"class":84},[74,326,126],{"class":125},[74,328,101],{"class":80},[74,330,151],{"class":118},[74,332,134],{"class":84},[74,334,335],{"class":76,"line":190},[74,336,92],{"emptyLinePlaceholder":91},[74,338,339,342,344,346,349],{"class":76,"line":195},[74,340,341],{"class":84},"other_group ",[74,343,101],{"class":80},[74,345,274],{"class":84},[74,347,348],{"class":118},"\"Other Options\"",[74,350,134],{"class":84},[74,352,353,356,358,360,362,364,366,368,370],{"class":76,"line":206},[74,354,355],{"class":84},"other_group.add_argument(",[74,357,161],{"class":118},[74,359,122],{"class":84},[74,361,166],{"class":118},[74,363,122],{"class":84},[74,365,171],{"class":125},[74,367,101],{"class":80},[74,369,176],{"class":118},[74,371,134],{"class":84},[20,373,374],{},"This organizes related arguments into named groups in the help text.",[24,376,378],{"id":377},"subcommands","Subcommands",[20,380,381],{},"One major new feature is the ability to specify subcommands, each with their own arguments:",[64,383,385],{"className":66,"code":384,"language":68,"meta":69,"style":69},"parser = argparse.ArgumentParser()\n\nsubparsers = parser.add_subparsers(help=\"commands\")\n\n# A command \nparser_a = subparsers.add_parser(\"a\", help=\"A help\") \nparser_a.add_argument(\"bar\", type=int)\n\n# B command\nparser_b = subparsers.add_parser(\"b\", help=\"B help\")\nparser_b.add_argument(\"--baz\", choices=[\"a\", \"b\", \"c\"])\n\nargs = parser.parse_args() \n\nif args.command == \"a\":\n   print(args.bar)\nelif args.command == \"b\":\n   print(args.baz)\n",[71,386,387,395,399,418,422,428,453,473,477,482,505,537,541,550,555,573,582,597],{"__ignoreMap":69},[74,388,389,391,393],{"class":76,"line":77},[74,390,98],{"class":84},[74,392,101],{"class":80},[74,394,104],{"class":84},[74,396,397],{"class":76,"line":88},[74,398,92],{"emptyLinePlaceholder":91},[74,400,401,404,406,409,411,413,416],{"class":76,"line":95},[74,402,403],{"class":84},"subparsers ",[74,405,101],{"class":80},[74,407,408],{"class":84}," parser.add_subparsers(",[74,410,126],{"class":125},[74,412,101],{"class":80},[74,414,415],{"class":118},"\"commands\"",[74,417,134],{"class":84},[74,419,420],{"class":76,"line":107},[74,421,92],{"emptyLinePlaceholder":91},[74,423,424],{"class":76,"line":112},[74,425,427],{"class":426},"sPyeA","# A command \n",[74,429,430,433,435,438,441,443,445,447,450],{"class":76,"line":137},[74,431,432],{"class":84},"parser_a ",[74,434,101],{"class":80},[74,436,437],{"class":84}," subparsers.add_parser(",[74,439,440],{"class":118},"\"a\"",[74,442,122],{"class":84},[74,444,126],{"class":125},[74,446,101],{"class":80},[74,448,449],{"class":118},"\"A help\"",[74,451,452],{"class":84},") \n",[74,454,455,458,461,463,466,468,471],{"class":76,"line":156},[74,456,457],{"class":84},"parser_a.add_argument(",[74,459,460],{"class":118},"\"bar\"",[74,462,122],{"class":84},[74,464,465],{"class":125},"type",[74,467,101],{"class":80},[74,469,470],{"class":214},"int",[74,472,134],{"class":84},[74,474,475],{"class":76,"line":190},[74,476,92],{"emptyLinePlaceholder":91},[74,478,479],{"class":76,"line":195},[74,480,481],{"class":426},"# B command\n",[74,483,484,487,489,491,494,496,498,500,503],{"class":76,"line":206},[74,485,486],{"class":84},"parser_b ",[74,488,101],{"class":80},[74,490,437],{"class":84},[74,492,493],{"class":118},"\"b\"",[74,495,122],{"class":84},[74,497,126],{"class":125},[74,499,101],{"class":80},[74,501,502],{"class":118},"\"B help\"",[74,504,134],{"class":84},[74,506,507,510,513,515,518,520,523,525,527,529,531,534],{"class":76,"line":211},[74,508,509],{"class":84},"parser_b.add_argument(",[74,511,512],{"class":118},"\"--baz\"",[74,514,122],{"class":84},[74,516,517],{"class":125},"choices",[74,519,101],{"class":80},[74,521,522],{"class":84},"[",[74,524,440],{"class":118},[74,526,122],{"class":84},[74,528,493],{"class":118},[74,530,122],{"class":84},[74,532,533],{"class":118},"\"c\"",[74,535,536],{"class":84},"])\n",[74,538,539],{"class":76,"line":221},[74,540,92],{"emptyLinePlaceholder":91},[74,542,543,545,547],{"class":76,"line":229},[74,544,198],{"class":84},[74,546,101],{"class":80},[74,548,549],{"class":84}," parser.parse_args() \n",[74,551,553],{"class":76,"line":552},14,[74,554,92],{"emptyLinePlaceholder":91},[74,556,558,561,564,567,570],{"class":76,"line":557},15,[74,559,560],{"class":80},"if",[74,562,563],{"class":84}," args.command ",[74,565,566],{"class":80},"==",[74,568,569],{"class":118}," \"a\"",[74,571,572],{"class":84},":\n",[74,574,576,579],{"class":76,"line":575},16,[74,577,578],{"class":214},"   print",[74,580,581],{"class":84},"(args.bar)\n",[74,583,585,588,590,592,595],{"class":76,"line":584},17,[74,586,587],{"class":80},"elif",[74,589,563],{"class":84},[74,591,566],{"class":80},[74,593,594],{"class":118}," \"b\"",[74,596,572],{"class":84},[74,598,600,602],{"class":76,"line":599},18,[74,601,578],{"class":214},[74,603,604],{"class":84},"(args.baz)\n",[20,606,607],{},"We define subcommands \"a\" and \"b\", each with their own set of arguments. The user specifies the subcommand and arguments, which we can access on args.",[20,609,610],{},"This is perfect for building CLIs with multiple commands.",[20,612,613],{},"Python's new argparse module provides a robust and user-friendly interface for parsing command line arguments. Some of the major features include argument groups, subcommands, flexible naming, type checking, and auto-generated help. This improves on the older argparse and makes it easy to build powerful command line tools.",[20,615,616],{},"The new parser takes care of all the low-level details, allowing developers to focus on their program's logic and more easily create complex CLIs in Python.",[618,619,620],"style",{},"html pre.shiki code .sDgYj, html code.shiki .sDgYj{--shiki-default:#D73A49;--shiki-dark:#FF7B72}html pre.shiki code .szmH1, html code.shiki .szmH1{--shiki-default:#24292E;--shiki-dark:#E6EDF3}html pre.shiki code .sNbzA, html code.shiki .sNbzA{--shiki-default:#032F62;--shiki-dark:#A5D6FF}html pre.shiki code .sjlk-, html code.shiki .sjlk-{--shiki-default:#E36209;--shiki-dark:#FFA657}html pre.shiki code .sPgVT, html code.shiki .sPgVT{--shiki-default:#005CC5;--shiki-dark:#79C0FF}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 .sPyeA, html code.shiki .sPyeA{--shiki-default:#6A737D;--shiki-dark:#8B949E}",{"title":69,"searchDepth":88,"depth":88,"links":622},[623,624,625,626],{"id":26,"depth":88,"text":27},{"id":58,"depth":88,"text":59},{"id":243,"depth":88,"text":244},{"id":377,"depth":88,"text":378},"2022-10-01","md",{"type":630,"layout":631},"post","blog_page","\u002Fblog\u002F2022-10-01-using-python-argparse",{"title":12,"description":22},"blog\u002F2022-10-01-using-python-argparse",[68,636,637],"programming","devops","iGvOeuJoUuhpk_KNlP99ljK71MN1JZzKzYVkYJ5Vgys",[640,645],{"title":641,"path":642,"stem":643,"date":644,"children":-1},"Getting started with Git","\u002Fblog\u002F2022-09-14-getting-started-with-git","blog\u002F2022-09-14-getting-started-with-git","2022-09-14",{"title":646,"path":647,"stem":648,"date":649,"children":-1},"Using Linux nftables","\u002Fblog\u002F2022-10-15-using-nft","blog\u002F2022-10-15-using-nft","2022-10-15",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":651},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"m12 19l-7-7l7-7m7 7H5\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":653},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M11.017 2.814a1 1 0 0 1 1.966 0l1.051 5.558a2 2 0 0 0 1.594 1.594l5.558 1.051a1 1 0 0 1 0 1.966l-5.558 1.051a2 2 0 0 0-1.594 1.594l-1.051 5.558a1 1 0 0 1-1.966 0l-1.051-5.558a2 2 0 0 0-1.594-1.594l-5.558-1.051a1 1 0 0 1 0-1.966l5.558-1.051a2 2 0 0 0 1.594-1.594zM20 2v4m2-2h-4\"\u002F>\u003Ccircle cx=\"4\" cy=\"20\" r=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":655},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2\"\u002F>\u003Ccircle cx=\"12\" cy=\"7\" r=\"4\"\u002F>\u003C\u002Fg>",1783616167166]