Nicer command line parse python
<p>Using argparse, I have created a small script that contains a command line parser for my analysis program which is part of a self made python package. It works perfectly, but I don't really like how to control it.</p>

<p>This is how the code looks in the script itself</p>

<pre><code>def myAnalysis():

    parser = argparse.ArgumentParser(description='''
        lala''')
    parser.add_argument('-d', '--data',help='')
    parser.add_argument('-e', '--option_1', help='', default=False, required=False)
    parser.add_argument('-f', '--option_2', help='', default=False, required=False)


    # combine parsed arguments
    args = parser.parse_args()code here
</code></pre>

<p>Additional to this there is some more in the setup file of the analysis package</p>

<pre><code>entry_points={
          'console_scripts': [
              'py_analysis = edit.__main__:myAnalysis'
          ]
</code></pre>

<p>As I said, this works without any problems. To analyze some data I have to use</p>

<pre><code>py_analysis --data path_to_data_file
</code></pre>

<p>Sometimes, I need some of the options. For this it may look loke</p>

<pre><code>py_analysis --data path_to_data_file --option_1 True --option_2 True
</code></pre>

<p>In my personal taste, this is kind of ugly. I would prefer something like</p>

<pre><code>py_analysis path_to_data_file --option_1 --option_2
</code></pre>

<p>I am pretty sure this is possible. I just don't know how</p>

<p>Use <em>store_true</em> action</p>

<pre><code>parser.add_argument('-e', '--option_1', help='', default=False, action ='store_true')
</code></pre>

<p>Then just adding to command line <em>--option_1</em> will set its value to <em>True</em>.</p>

python argparse