Skip to content

Commit 2e3e66a

Browse files
committed
pwd: Add pwd.py
1 parent 0c98839 commit 2e3e66a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/pwd.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/python3
2+
3+
import os
4+
from optparse import OptionParser
5+
6+
7+
def pwd(opts):
8+
resolved_pwd = os.getcwd()
9+
10+
print(
11+
resolved_pwd
12+
if opts.resolve
13+
or not (
14+
# Only use PWD's contents if it accurately
15+
# points to the current working directory
16+
# and the -L flag is also given.
17+
(pwd_from_env := os.environ.get("PWD"))
18+
and os.path.samefile(pwd_from_env, resolved_pwd)
19+
)
20+
else pwd_from_env
21+
)
22+
23+
24+
if __name__ == "__main__":
25+
parser = OptionParser(
26+
usage="Usage: %prog [OPTION]",
27+
description="Print the path to the current working directory.",
28+
add_help_option=False,
29+
)
30+
parser.add_option("--help", action="help", help="show usage information and exit")
31+
32+
parser.add_option(
33+
"-L",
34+
"--logical",
35+
dest="resolve",
36+
action="store_false",
37+
help="don't resolve symlinks",
38+
)
39+
parser.add_option(
40+
"-P",
41+
"--physical",
42+
dest="resolve",
43+
action="store_true",
44+
default=True,
45+
help="resolve all encountered symlinks",
46+
)
47+
48+
opts, args = parser.parse_args()
49+
50+
if args:
51+
parser.error("too many arguments")
52+
53+
pwd(opts)

0 commit comments

Comments
 (0)