Python 2.7 Argparse Optional and Required arguments
<p>So I've been frantically reading tutorials on argparse everywhere but can't seem to figure out why my program is getting an error. My code currently looks like this:</p>

<pre><code>parser = argparse.ArgumentParser()
parser.add_argument("-d", "-debug", required = False, help = "optional parameter")
parser.add_argument("input_file", help = "file to be parsed")
args = parser.parse_args()
</code></pre>

<p>When I run my program with the command "python myprogram.py -d inputfile" it complains that there are too few arguments. Furthermore, when I just run it with inputfile as the parameter, it works.</p>

<p>Does anyone know why this might be happening?</p>

<p>The <a href="https://docs.python.org/2.7/library/argparse.html#action" rel="nofollow">default action</a> for an argument is <code>'store'</code>.  <code>store</code> actions generally expect a <em>value</em> to be associated with the flag.</p>

<p>It looks like you want this to be a boolean switch type of flag in which case you want the <code>'store_true'</code> action</p>

<pre><code>parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", required = False, help = "optional parameter", action = "store_true")
parser.add_argument("input_file", help = "file to be parsed")
args = parser.parse_args()
</code></pre>

python python-2.7 command-line-arguments