# HG changeset patch # User Jeff Hammel # Date 1372115184 25200 # Node ID 7eaaf99e31821650061a72608768d57493ba2460 # Parent 0694882a1ad87a9f04fdffecc656edfd036106e4 dammit diff -r 0694882a1ad8 -r 7eaaf99e3182 python/is_interactive.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/is_interactive.py Mon Jun 24 16:06:24 2013 -0700 @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +""" +illustrates usage of isatty + +Ref: http://www.tutorialspoint.com/python/file_isatty.htm +""" + +import optparse +import os +import sys + +# DOES NOT WORK! :( +# > echo 'foo' | is_interactive.py +# Usage: is_interactive.py [options] + +# is_interactive.py: error: missing input + + +def main(args=sys.argv[1:]): + parser = optparse.OptionParser() + options, args = parser.parse_args(args) + + function = str.upper + + if args: + # take input from command line args + input = ' '.join(args) + elif not sys.stderr.isatty(): + # take input from stdin + input = sys.stdin.read() + else: + # no input! cry! + parser.error("missing input") + + print function(input) + +if __name__ == '__main__': + main()