by
0
8
2,176
0
Top 1% !
Popular
Famous
Tagged
Easy-to-find
Specified
Refined
OpenSource
Popularity: 10321st place

Published on:

BrowserIE
IE Version9.0
Languagepython
LicenseMIT_X11
OSWindows
Windows VersionWindowsVista

How to check the presence of argv parameter in python

This script checks the parameters passed via command line. If the parameter is present a check on its value and cast are performed. The script handles exception as well.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/718/How-to-check-the-presence-of-argv-parame?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
#!/usr/bin/python import sys # ------------------------------- Main program ------------------------------- # The default value for command line argv parameter default_size = 70 actual_value = default_size if (len(sys.argv) > 1): try: if(int(sys.argv[1]) > 0): actual_value = int(sys.argv[1]) print "Actual size: " + str(actual_value) else: print("Please specify only positive integer as parameter! " + str(sys.argv[1])) print("Actual size set with default value: " + str(actual_value)) except Exception: print("Ouch!... Please review the parameter! " + str(sys.argv[1])) sys.exit(-1)
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus