{"id":7867,"date":"2021-05-20T10:28:38","date_gmt":"2021-05-20T15:28:38","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=7867"},"modified":"2021-05-21T10:28:53","modified_gmt":"2021-05-21T15:28:53","slug":"boolean-opts-in-python","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=7867","title":{"rendered":"Boolean Opts in Python"},"content":{"rendered":"\n<p>I have a few command line arguments on a Python script that are most readily used if they are boolean. I sometimes need a &#8220;verbose&#8221; option for script debugging &#8212; print a lot of extra stuff to show what&#8217;s going on, and I usually want a &#8220;dry run&#8221; option where the script reads data, performs calculations, and prints results to the screen without making any changes or sending data anywhere (database, email, etc). To use command line arguments as boolean values, I use a function that converts a variety of possible inputs to True\/False. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef string2boolean(strInput):\n    &quot;&quot;&quot;\n    :param strInput: String string to be converted to boolean\n    :return: Boolean representation of input\n    &quot;&quot;&quot;\n    if isinstance(strInput, bool):\n        return strInput\n    if strInput.lower() in (&#039;yes&#039;, &#039;true&#039;, &#039;t&#039;, &#039;y&#039;, &#039;1&#039;):\n        return True\n    elif strInput.lower() in (&#039;no&#039;, &#039;false&#039;, &#039;f&#039;, &#039;n&#039;, &#039;0&#039;):\n        return False\n    else:\n        raise argparse.ArgumentTypeError(&#039;Boolean value expected.&#039;)\n\n<\/pre><\/div>\n\n\n<p>Use &#8220;type&#8221; when adding the argument to run the input through your function. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n    parser.add_argument(&#039;-r&#039;, &#039;--dryrun&#039;, action=&#039;store&#039;, type=string2boolean, dest=&#039;boolDryRun&#039;, default=False, help=&quot;Preview data processing without sending data to DB or Kafka. Valid values: &#039;true&#039; or &#039;false&#039;.&quot;)\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>I have a few command line arguments on a Python script that are most readily used if they are boolean. I sometimes need a &#8220;verbose&#8221; option for script debugging &#8212; print a lot of extra stuff to show what&#8217;s going on, and I usually want a &#8220;dry run&#8221; option where the script reads data, performs &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[771,664],"class_list":["post-7867","post","type-post","status-publish","format-standard","hentry","category-coding","tag-debugging","tag-python"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7867"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7867\/revisions"}],"predecessor-version":[{"id":7868,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/7867\/revisions\/7868"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}