stdinargs.py (556B)
1 import sys 2 import select 3 import argparse 4 5 def stdincheck(): 6 h = select.select([sys.stdin], [], [], 0) # 0 = poll, noblock 7 if len(h[0]) > 0: 8 v = h[0][0].read() 9 return v.rstrip() 10 return None 11 12 argparser = argparse.ArgumentParser() 13 argparser.add_argument('val', nargs='?', default=stdincheck(), type=str, help='input val') 14 args = argparser.parse_args(sys.argv[1:]) 15 16 if args.val == None: 17 argparser.error('value but be specified on stdin or as first positional argument') 18 19 print(args.val)