-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathexpr_stats.py
executable file
·64 lines (50 loc) · 1.83 KB
/
expr_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
import argparse, sys
from argparse import RawTextHelpFormatter
__author__ = "Author ([email protected])"
__version__ = "$Revision: 0.0.1 $"
__date__ = "$Date: 2015-07-14 14:31 $"
# --------------------------------------
# define functions
def get_args():
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter, description="\
expr_stats.py\n\
author: " + __author__ + "\n\
version: " + __version__ + "\n\
description: Basic python script template")
parser.add_argument('-sr', '--argA', metavar='argA', type=str, required=True, help='description of argument')
parser.add_argument('-b', '--argB', metavar='argB', required=False, help='description of argument B')
parser.add_argument('-c', '--flagC', required=False, action='store_true', help='sets flagC to true')
parser.add_argument('input', nargs='?', type=argparse.FileType('r'), default=None, help='file to read. If \'-\' or absent then defaults to stdin.')
# parse the arguments
args = parser.parse_args()
# if no input, check if part of pipe and if so, read stdin.
if args.input == None:
if sys.stdin.isatty():
parser.print_help()
exit(1)
else:
args.input = sys.stdin
# send back the user input
return args
# primary function
def myFunction(argA, argB, flagC, file):
for line in file:
print line.rstrip()
return
# --------------------------------------
# main function
def main():
# parse the command line args
args = get_args()
# call primary function
myFunction(args.argA, args.argB, args.flagC, args.input)
# close the input file
args.input.close()
# initialize the script
if __name__ == '__main__':
try:
sys.exit(main())
except IOError, e:
if e.errno != 32: # ignore SIGPIPE
raise