{"id":157,"date":"2011-05-18T12:24:22","date_gmt":"2011-05-18T16:24:22","guid":{"rendered":"http:\/\/www.borg.org\/?p=157"},"modified":"2011-05-18T12:24:22","modified_gmt":"2011-05-18T16:24:22","slug":"python-command-line-arguments","status":"publish","type":"post","link":"https:\/\/www.borg.org\/?p=157","title":{"rendered":"Python Command Line Arguments"},"content":{"rendered":"<p>For quite some time I have thought I shouldn&#8217;t parse my own command line arguments in Python, but each time I looked for the &#8220;right&#8221; way to do it, I turned back.<\/p>\n<p>Today, I decided to persist.<\/p>\n<p>Now I know why I turned back, the libraries were terrible.<\/p>\n<p>I want to have:<\/p>\n<pre>mycleverprog.py mycommand --nifty-option\n<\/pre>\n<p>Where the command is to do one thing or do the opposite.\u00a0 It is a required thing to say which.<\/p>\n<p>The standard library argparse is a little obscure at first, but with a little reading it starts to make sense.\u00a0 Cool.<\/p>\n<p>Oh, dang!\u00a0 It is new in Python 2.7, and I am running 2.6.\u00a0 Okay, so let&#8217;s look at the deprecated optparse, it looks really similar, I&#8217;ll convert my embryonic code&#8230;<\/p>\n<p>Horrors!\u00a0 I don&#8217;t know the history of the project, but it looks like some &#8220;little minds&#8221; have been at work on this library.\u00a0 Because it is called &#8220;optparse&#8221; it doesn&#8217;t support &#8220;required options&#8221; because the term is self-contradictory.\u00a0 Come on!\u00a0 A <em>very<\/em> common use case is to type a command with a required parameter following on the command line.\u00a0 Think Unix commands like &#8220;rm&#8221; or &#8220;mkdir&#8221; or &#8220;touch&#8221;, or &#8230;, you get the idea.\u00a0 These commands also have &#8220;options&#8221; that are optional.\u00a0 So someone built a library that handles the &#8220;options&#8221; but makes a point of not handling a required parameter, just because of an unfortunate name for the library!?!\u00a0 They even waste effort pontificating on why options should be optional?<\/p>\n<p><strong>Bosch!<\/strong><\/p>\n<p>They do talk about &#8220;positional arguments&#8221; being required, but it isn&#8217;t clear whether they even support that.<\/p>\n<p>My solution?\u00a0 Download<\/p>\n<pre>http:\/\/argparse.googlecode.com\/files\/argparse-1.2.1.tar.gz<\/pre>\n<p>put argparse.py in<\/p>\n<pre>\/usr\/local\/lib\/python2.6\/dist-packages<\/pre>\n<p>and use argparse.\u00a0 My little utility will just be for personal use&#8211;at least for now. By the time I give it to anyone else, newer versions of Python will be standard.\u00a0 Or I can include a copy of argparse.py.<\/p>\n<p>-kb, the Kent who is slowly getting better and better at Python.<\/p>\n<p>\u00a92011 Kent Borg<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For quite some time I have thought I shouldn&#8217;t parse my own command line arguments in Python, but each time I looked for the &#8220;right&#8221; way to do it, I turned back. Today, I decided to persist. Now I know why I turned back, the libraries were terrible. I want to have: mycleverprog.py mycommand &#8211;nifty-option [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[22,221,251],"class_list":["post-157","post","type-post","status-publish","format-standard","hentry","category-tech","tag-argparse","tag-optparse","tag-python"],"_links":{"self":[{"href":"https:\/\/www.borg.org\/index.php?rest_route=\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.borg.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.borg.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.borg.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.borg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157"}],"version-history":[{"count":0,"href":"https:\/\/www.borg.org\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.borg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.borg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.borg.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}