1import sys
2print("This is the name of the script:", sys.argv[0])
3print("Number of arguments:", len(sys.argv))
4print("The arguments are:" , str(sys.argv))
5
6#Example output
7#This is the name of the script: sysargv.py
8#Number of arguments in: 3
9#The arguments are: ['sysargv.py', 'arg1', 'arg2']
1#!/usr/bin/python
2
3import sys
4
5print('Number of arguments:', len(sys.argv), 'arguments.')
6print('Argument List:', str(sys.argv))
1#!/usr/bin/python
2
3import sys
4
5print 'Number of arguments:', len(sys.argv), 'arguments.'
6print 'Argument List:', str(sys.argv)
1#!/usr/bin/python
2
3import sys, getopt
4
5def main(argv):
6 inputfile = ''
7 outputfile = ''
8 try:
9 opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
10 except getopt.GetoptError:
11 print 'test.py -i <inputfile> -o <outputfile>'
12 sys.exit(2)
13 for opt, arg in opts:
14 if opt == '-h':
15 print 'test.py -i <inputfile> -o <outputfile>'
16 sys.exit()
17 elif opt in ("-i", "--ifile"):
18 inputfile = arg
19 elif opt in ("-o", "--ofile"):
20 outputfile = arg
21 print 'Input file is "', inputfile
22 print 'Output file is "', outputfile
23
24if __name__ == "__main__":
25 main(sys.argv[1:])
1import argparse
2
3parser = argparse.ArgumentParser(description='Process some integers.')
4parser.add_argument('integers', metavar='N', type=int, nargs='+',
5 help='an integer for the accumulator')
6parser.add_argument('--sum', dest='accumulate', action='store_const',
7 const=sum, default=max,
8 help='sum the integers (default: find the max)')
9
10args = parser.parse_args()
11print(args.accumulate(args.integers))
12