vendredi 25 août 2017

Getting Python to Confirm If/Elif/Else before proceeding

I've written a simple python script to take command line arguments and write them to a file for deployment to a router using Ansible. But before my script creates that output file, I want to force the user to confirm the request with a Y/N (Yes or No) entry.

How can I modify this script to request that after each if/elif statement?

#!/usr/bin/python

import argparse
import sys

parser = argparse.ArgumentParser()
parser.add_argument("-s", "--set", help="set", action="store_true")
parser.add_argument("-d", "--delete", help="delete", action="store_true")
parser.add_argument("-i", "--ipaddr", help="Target IP")
args = parser.parse_args()
if args.set:
        print "Deploying: set routing-options static route %s" % (args.ipaddr)
        filename = open("/var/tmp/output.txt",'w')
        sys.stdout = filename
        print "set routing-options static route %s" % (args.ipaddr)
elif args.delete:
        print "Deploying: delete routing-options static route %s" % (args.ipaddr)
        filename = open("/var/tmp/output.txt",'w')
        sys.stdout = filename
        print "delete routing-options static route %s" % (args.ipaddr)
else:
        exit(1)

Aucun commentaire:

Enregistrer un commentaire