According to previous question Else statement executing even the IF statement is TRUE the suggestion provided was to check the indention. Indention seems to be correct in my code. What seems to be the problem?
According to https://www.tutorialspoint.com/python/python_if_else.htm
An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
As per my understanding, else
should not be executed if the if
statement is correct/True.
However, in this code ... else
statement still get executed .. see python myCode.py -i 8.8.8.8
C:\Python>python myCode.py
No Argument Provided
C:\Python>python myCode.py -h
usage: myCode.py [-h] [-u URL] [-i IP]
optional arguments:
-h, --help show this help message and exit
-u URL, --url URL Uniform Resource Locator
-i IP, --ip IP IP Address
C:\Python>python myCode.py -u google.com
~ URL is google.com
C:\Python>python myCode.py -i 8.8.8.8
~ IP Address is 8.8.8.8
No Argument Provided
C:\Python>
This is the code.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url',
help='Uniform Resource Locator' )
parser.add_argument('-i', '--ip',
help='IP Address' )
args = parser.parse_args()
if args.ip:
print("~ IP Address is " + args.ip)
if args.url:
print("~ URL is " + args.url)
else:
print("No Argument Provided")
Please let me know what went wrong here. No Argument Provided
is not supposed to be there.
C:\Python>python myCode.py -i 8.8.8.8
~ IP Address is 8.8.8.8
No Argument Provided