{"id":644,"date":"2014-01-21T14:33:30","date_gmt":"2014-01-21T21:33:30","guid":{"rendered":"http:\/\/somethingk.com\/main\/?p=644"},"modified":"2014-01-21T14:42:05","modified_gmt":"2014-01-21T21:42:05","slug":"python-optionparser","status":"publish","type":"post","link":"https:\/\/somethingk.com\/main\/python-optionparser\/","title":{"rendered":"Python OptionParser"},"content":{"rendered":"<p>I love when code makes life easier. However, if you don&#8217;t know what the code does&#8230; life isn&#8217;t so easy now. That&#8217;s why we have <a title=\"Man (Manual) Pages\" href=\"http:\/\/en.wikipedia.org\/wiki\/Man_page\" target=\"_blank\">man<\/a> pages!<\/p>\n<figure style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/i.qkme.me\/3swzzm.jpg\"><img fetchpriority=\"high\" decoding=\"async\" class=\"  \" alt=\"\" src=\"http:\/\/i.qkme.me\/3swzzm.jpg\" width=\"300\" height=\"300\" \/><\/a><figcaption class=\"wp-caption-text\">In honor of those &#8220;man&#8221; pages&#8230;<\/figcaption><\/figure>\n<p>Except building a man page for a script and handling the &#8220;-h&#8221; argument from a command prompt can be tedious.<\/p>\n<p>Python has a really cool library called optparse, that includes the ability to make a &#8220;help&#8221; output for a script along with argument handling. You know\u00a0<span style=\"line-height: 1.5em;\">how nice that is to have a library take care of the whole argument handling? Very nice! I would hate to use regular expressions to try and parse a list of\u00a0<\/span><span style=\"line-height: 1.5em;\">command line inputs.<\/span><\/p>\n<p>If my blog posts about the language haven&#8217;t made this obvious already, I really love Python. It does have faults because it is not a type-safe language and in some\u00a0<span style=\"line-height: 1.5em;\">circumstances that can be an issue. I&#8217;ll go into detail at a later time. Ideally, it is best to have both type-safe and dynamic type languages in your repertoire of tools.<\/span><\/p>\n<p>Anyways, back to Optparse. It&#8217;s basically self-explanatory.<\/p>\n<p>Import OptionParser.<\/p>\n<pre>from optparse import OptionParser<\/pre>\n<p>Next, setup an OptionParser object.<\/p>\n<pre>parser = OptionParser()<\/pre>\n<p>Give it some flags or parameter options. You do not have to create a -h or help option, this will be created for you automatically. Below is a template for creating an option.<\/p>\n<p><code>parser.add_option(\"-&lt;OPTION LETTER&gt;\", \"--&lt;OPTION NAME&gt;\", dest=\"&lt;VARIABLE NAME TO STORE OPTION CONTENT&gt;\", default=&lt;DEFAULT VALUE&gt;, help=\"&lt;HELP TEXT&gt;\")<\/code><\/p>\n<p>Each option creates a possible input parameter that user&#8217;s can specify. If you do not want the user to have to input a value, maybe a flag is more your style.<\/p>\n<p>A flag is a true or false notification that does not require additional user input. If a flag is present do something, if not do something else. To create a flag, set\u00a0<span style=\"line-height: 1.5em;\">the default option to true or false and add &#8220;action=&#8221;store_true&#8221; or add &#8220;action=&#8221;store_false&#8221;\u00a0in order to simulate a flag option. When you want the flag to turn a statement true set\u00a0&#8220;action=&#8221;store_true&#8221; and &#8220;default=False&#8221; and the exact opposite for a flag to turn something false.\u00a0<\/span><\/p>\n<p>To help clarify things, I wrote up a fun example below.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>The below was coded for Python 2.7 and saved to a file called fun.py.<\/p>\n<p><code>from optparse import OptionParser<br \/>\nimport os, time<\/p>\n<p>def somethingCool(dazzling):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print \"Something Cool\"<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if dazzling:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print \"\\n~*~*~*~*~*~*~\"+dazzling+\"~*~*~*~*~*~*~\"<\/p>\n<p>def somethingStupid(dazzling):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print \"Something Stupid...\"<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if dazzling:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print \"\\n--------------\"+dazzling+\"--------------\"<\/p>\n<p>def main():<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;parser = OptionParser() #Help menu options<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;parser.add_option(\"-c\", \"--cool\", action=\"store_true\", dest=\"cool\", default=False, help=\"Do something cool!\")<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;parser.add_option(\"-s\", \"--stupid\", action=\"store_true\", dest=\"stupid\", default=False, help=\"Do something stupid...\")<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;parser.add_option(\"-d\", \"--dazzling\", dest=\"dazzling\", default=None, help=\"Add this dazzling text to the cool or stupid thing.\")<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;(options, args) = parser.parse_args()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if options.cool:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;somethingCool(options.dazzling)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;elif options.stupid:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;somethingStupid(options.dazzling)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;else:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print \"Lame, you selected nothing...\"<\/p>\n<p>if __name__ == \"__main__\":<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;main()<\/code><\/p>\n<p><strong>Explanation<\/strong><\/p>\n<p>OptionParser will automatically create a help option (-h) for you. Here is what the help screen looks like for my code example:<\/p>\n<p><a href=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.52.37-PM.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-646\" alt=\"Screen Shot 2014-01-21 at 3.52.37 PM\" src=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.52.37-PM-300x192.png\" width=\"300\" height=\"192\" srcset=\"https:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.52.37-PM-300x192.png 300w, https:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.52.37-PM.png 567w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Basically, if I call the script with the -c flag, something cool will happen.<\/p>\n<pre>python fun.py -c<\/pre>\n<p>If I call the script with the -s flag, something stupid will happen.<\/p>\n<pre>python fun.py -s<\/pre>\n<p><a href=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.52.55-PM.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-647\" alt=\"Screen Shot 2014-01-21 at 3.52.55 PM\" src=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.52.55-PM-300x191.png\" width=\"300\" height=\"191\" srcset=\"https:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.52.55-PM-300x191.png 300w, https:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.52.55-PM.png 568w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>If I call the script with the -d &lt;TEXT&gt; and a flag something will happen with text.<\/p>\n<pre>python fun.py -c -d \"My Blog at Somethingk.com\"\r\n\r\n<a href=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.54.36-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-648\" alt=\"Screen Shot 2014-01-21 at 3.54.36 PM\" src=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.54.36-PM-300x191.png\" width=\"300\" height=\"191\" srcset=\"https:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.54.36-PM-300x191.png 300w, https:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-3.54.36-PM.png 568w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/pre>\n<p>If I don&#8217;t provide a -s or a -c flag, the code insults me&#8230; A flag isn&#8217;t required but nothing else will happen if one isn&#8217;t given.<\/p>\n<p><a href=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-4.07.48-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-649\" alt=\"Screen Shot 2014-01-21 at 4.07.48 PM\" src=\"http:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-4.07.48-PM-300x193.png\" width=\"300\" height=\"193\" srcset=\"https:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-4.07.48-PM-300x193.png 300w, https:\/\/somethingk.com\/main\/wp-content\/uploads\/2014\/01\/Screen-Shot-2014-01-21-at-4.07.48-PM.png 562w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Check out <a href=\"http:\/\/docs.python.org\/2\/library\/optparse.html\" title=\"Optparse\" target=\"_blank\">http:\/\/docs.python.org\/2\/library\/optparse.html<\/a> for more details. Have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I love when code makes life easier. However, if you don&#8217;t know what the code does&#8230; life isn&#8217;t so easy now. That&#8217;s why we have man pages! Except building a man page for a script and handling the &#8220;-h&#8221; argument from a command prompt can be tedious. Python has a really cool library called optparse, that includes the ability to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[157,155,156,153],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-python","tag-argument-handling","tag-optionparser","tag-optparse","tag-python-2-7"],"_links":{"self":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":14,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":662,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/644\/revisions\/662"}],"wp:attachment":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}