#python command.py arg1 arg2 arg3 -W ignore #!/usr/bin/python import sys print "Number of arguments: ", len(sys.argv);//4 for i, e in enumerate(sys.argv): print i, e;
#!/usr/bin/python import argparse # Create a parser parser = argparse.ArgumentParser(description='Process some integers.') # Add Positional Arguments parser.add_argument('echo', help='echo help') parser.add_argument('square', help='square help', type=int) # Add Optional Arguments parser.add_argument('-f', '--foo', help='foo help') args = parser.parse_args() #python command.py --foo temp 1 2 #echo = "1" #square = 2 #foo = temp print args # print all arguments print args.foo # print a single argument
#python command.py -h #python command.py -i inputFile -o outputFile arg1 arg2 #python command.py --help #python command.py --input inputFile --output outputFile arg1 arg2 #python command.py #!/usr/bin/python import sys, getopt try: opts, args = getopt.getopt(sys.argv[1:], 'hi:o:', ['help', 'input=', 'output=']); except getopt.GetoptError, err: print err.msg; print err.opt; print str(err); print 'python command.py -i <inputFile> -o <outputFile> args'; sys.exit(2); print 'Opts:'; for opt, arg in opts: print opt, arg; print 'Args: '; for arg in args: print arg;
#!/user/bin/python def KToF(t): '''Convert Kelvin temperature to Fahrenheit temperature''' assert(t >= 0), "Colder than absolute zero!" return (t-273)*1.8+32; if __name__ == '__main__': print KToF(273); print KToF(-1);