Last active
April 22, 2016 06:36
-
-
Save buuhsmead/4043e4caf8b7de176b504dea742cb939 to your computer and use it in GitHub Desktop.
ptyhon command line parms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, getopt | |
print 'Number of arguments:', len(sys.argv), 'arguments.' | |
print 'Argument List:', str(sys.argv) | |
def usage(msg=None): | |
if msg: | |
print msg | |
print '%s -i <inputfile> -o <outputfile>' % sys.argv[0] | |
sys.exit(1) | |
def main(argv): | |
# parms needed to be filled through cli options | |
inputfile = None | |
outputfile = None | |
# parse cli options | |
try: | |
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) | |
except getopt.GetoptError as e: | |
usage(str(e)) | |
# split the cli options into vars | |
# and / or take action | |
for opt, arg in opts: | |
if opt == '-h': | |
usage('help about parameters:') | |
elif opt in ("-i", "--ifile"): | |
inputfile = arg | |
elif opt in ("-o", "--ofile"): | |
outputfile = arg | |
print 'Input file is ', inputfile | |
print 'Output file is ', outputfile | |
# check required parms | |
if inputfile == None or outputfile == None: | |
usage('Needed input parms not given:') | |
# if we are here the parms are filled | |
# do program logic from here on | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment