File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/python3
2+
3+ import os
4+ import sys
5+ from optparse import OptionParser
6+
7+
8+ def tty (opts ):
9+ try :
10+ ttyname = os .ttyname (sys .stdin .fileno ())
11+ except OSError :
12+ if not opts .silent :
13+ print ("not a tty" ) # to stdout, not stderr
14+ sys .exit (1 )
15+ else :
16+ if not opts .silent :
17+ print (ttyname )
18+
19+
20+ if __name__ == "__main__" :
21+ parser = OptionParser (
22+ usage = "Usage: %prog [OPTION]" ,
23+ description = "Print the path to the terminal connected to standard input." ,
24+ add_help_option = False ,
25+ )
26+ parser .add_option ("--help" , action = "help" , help = "show usage information and exit" )
27+
28+ parser .add_option (
29+ "-s" ,
30+ "--silent" ,
31+ "--quiet" ,
32+ action = "store_true" ,
33+ help = "print nothing; only return an exit status" ,
34+ )
35+
36+ opts , args = parser .parse_args ()
37+
38+ if args :
39+ parser .error (f"extra operand '{ args [0 ]} '" )
40+
41+ tty (opts )
You can’t perform that action at this time.
0 commit comments