annotate python/is_interactive.py @ 438:364ddd44fd82

all the parts are there; now just to assemble it
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 08 Aug 2013 17:00:47 -0700
parents 7eaaf99e3182
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
346
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 illustrates usage of isatty
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 Ref: http://www.tutorialspoint.com/python/file_isatty.htm
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 """
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import optparse
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import os
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 import sys
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 # DOES NOT WORK! :(
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 # > echo 'foo' | is_interactive.py
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 # Usage: is_interactive.py [options]
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 # is_interactive.py: error: missing input
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 def main(args=sys.argv[1:]):
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 parser = optparse.OptionParser()
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 options, args = parser.parse_args(args)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 function = str.upper
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 if args:
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 # take input from command line args
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 input = ' '.join(args)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 elif not sys.stderr.isatty():
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 # take input from stdin
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 input = sys.stdin.read()
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 else:
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 # no input! cry!
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 parser.error("missing input")
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 print function(input)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 if __name__ == '__main__':
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 main()