How to print just the content of the help string of a specific argument of ArgParse
<p>Is there a way to access the help strings for specific arguments of the argument parser library object? </p>

<p>I want to print the help string content if the option was present on the command line. <em>Not</em> the complete help text that Argument Parser can display via ArgumentParser.print_help .</p>

<p>So something along those lines:</p>

<pre><code>parser = argparse.ArgumentParser()

parser.add_argument("-d", "--do_x", help='the program will do X')

if do_x:
    print(parser.&lt;WHAT DO I HAVE TO PUT HERE?&gt;('do_x')
</code></pre>

<p>And this is the required behavior</p>

<p>$program -d</p>

<blockquote>
  <p>the program will do X</p>
</blockquote>

<p>There is <code>parser._option_string_actions</code> which is mapping between option strings (<code>-d</code> or <code>--do_x</code>) and <a href="https://docs.python.org/3/library/argparse.html#action-classes" rel="nofollow"><code>Action</code> objects</a>. <code>Action.help</code> attribute holds the help string.</p>

<pre><code>import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--do_x", action='store_true',
                    help='the program will do X')
args = parser.parse_args()
if args.do_x:
    print(parser._option_string_actions['--do_x'].help)
    # OR  print(parser._option_string_actions['-d'].help)
</code></pre>

python argparse