Interactive System Arguments in Python
<p>I have written a code which is to be run from the command prompt, taking arguments from the command.</p>

<p>What i have tried.</p>

<pre><code>start_date = sys.argv[1]
end_date = sys.argv[2]

end_date2 = datetime.strptime(end_date, "%d %b %Y").date()
start_date2 = datetime.strptime(start_date, "%d %b %Y").date()
--- Do Something ---
</code></pre>

<p>and i am running the script thus :</p>

<pre><code>python myscript.py "19 Aug 2016" "19 Sep 2016"
</code></pre>

<p>this runs smoothly. however whatever i am doing in the script requires a condition :-- <code>start_date2 &gt; end_date2</code></p>

<p>so i am using a if statement to restrict this and <code>exit()</code> the code if the above condition is not met.</p>

<p>My Question :-</p>

<p>Is it possible to reset the argument if the above condition is not met??</p>

<p>something like :</p>

<pre><code>if start_date2 &lt; end_date2 :
    re enter the arguments from the command prompt 
    without stopping the code and take the new arguments and ignore the  wrong ones.
</code></pre>

<p>kind of like an interactive code.</p>

<p>Yes, it is possible. Simply after detecting dates are wrong use <code>print</code>/<code>raw_input</code> and ask user for new date, overwrite old one and check dates again.</p>

<p>For example (pseudocode a bit):</p>

<pre><code>start_date = sys.argv[1]
while start_date.isWrong():
    print "start_date is wrong, gimme new one and press enter: "
    start_date = parse_from_string(raw_input())
</code></pre>

<p>Anyway, you should try <a href="https://docs.python.org/2.7/library/argparse.html" rel="nofollow">argparse</a> to read command-line options.</p>

python python-2.7 sys