comparison python/is_interactive.py @ 346:7eaaf99e3182

dammit
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 24 Jun 2013 16:06:24 -0700
parents
children
comparison
equal deleted inserted replaced
345:0694882a1ad8 346:7eaaf99e3182
1 #!/usr/bin/env python
2
3 """
4 illustrates usage of isatty
5
6 Ref: http://www.tutorialspoint.com/python/file_isatty.htm
7 """
8
9 import optparse
10 import os
11 import sys
12
13 # DOES NOT WORK! :(
14 # > echo 'foo' | is_interactive.py
15 # Usage: is_interactive.py [options]
16
17 # is_interactive.py: error: missing input
18
19
20 def main(args=sys.argv[1:]):
21 parser = optparse.OptionParser()
22 options, args = parser.parse_args(args)
23
24 function = str.upper
25
26 if args:
27 # take input from command line args
28 input = ' '.join(args)
29 elif not sys.stderr.isatty():
30 # take input from stdin
31 input = sys.stdin.read()
32 else:
33 # no input! cry!
34 parser.error("missing input")
35
36 print function(input)
37
38 if __name__ == '__main__':
39 main()