How to check if the parameter exist in python
<p>I would like to create a simple python script that will take a parameter from the console, and it will display this parameter. If there will be no parameter then I would like to display error message, but custom message not something like IndexError: list index out of range</p>

<p>Something like this:</p>

<pre><code>if isset(sys.argv[1]):
    print sys.argv[1];
else:
    print "No parameter has been included"
</code></pre>

<p>Just for fun, you can also use <code>getopt</code> which provides you a way of predefining the options that are acceptable using the unix <code>getopt</code> conventions.</p>

<pre><code>import sys
import getopt
try:
    opts, args = getopt.getopt(sys.argv[1:], "hvxrc:s:", ["help", "config=", "section="])
except getopt.GetoptError as err:
    print ("Option error:", str(err))
    opts=[]
for op , val in opts:
    print ("option",op,"Argument",val)
if not opts:
    print ("No parameter supplied")
</code></pre>

<p>In the above if an incorrect parameter is supplied all of the options are scrapped.<br>
Examples of use would be:   </p>

<pre><code>python myprog.py -h
python myprog.py --help
python myprog.py -c123
python myprog.py --config=123
</code></pre>

<p><a href="https://pymotw.com/2/getopt/" rel="nofollow">https://pymotw.com/2/getopt/</a><br>
<a href="http://linux.about.com/library/cmd/blcmdl1_getopt.htm" rel="nofollow">http://linux.about.com/library/cmd/blcmdl1_getopt.htm</a></p>

python linux