Skip to content

Commit 08d8b7c

Browse files
committed
tty: Add tty.py
1 parent 0f55395 commit 08d8b7c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/tty.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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)

0 commit comments

Comments
 (0)