Skip to content

Instantly share code, notes, and snippets.

@buuhsmead
Last active April 22, 2016 06:36
Show Gist options
  • Save buuhsmead/4043e4caf8b7de176b504dea742cb939 to your computer and use it in GitHub Desktop.
Save buuhsmead/4043e4caf8b7de176b504dea742cb939 to your computer and use it in GitHub Desktop.
ptyhon command line parms
#!/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