Skip to content

Commit 220539e

Browse files
committed
dirname: Add dirname.py
1 parent d268741 commit 220539e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/dirname.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python3
2+
3+
import os
4+
from optparse import OptionParser
5+
6+
7+
def dirname(opts, names: list[str]):
8+
for name in names:
9+
print(os.path.dirname(name) or ".", end="\0" if opts.zero else "\n")
10+
11+
12+
if __name__ == "__main__":
13+
parser = OptionParser(
14+
usage="Usage: %prog [OPTION]... NAME...",
15+
description=(
16+
"Print each path NAME with the last component removed,"
17+
" or '.' if NAME is the only component."
18+
),
19+
add_help_option=False,
20+
)
21+
parser.add_option("--help", action="help", help="show usage information and exit")
22+
23+
parser.add_option(
24+
"-z",
25+
"--zero",
26+
action="store_true",
27+
help="terminate outputs with NUL instead of newline",
28+
)
29+
30+
opts, args = parser.parse_args()
31+
32+
if not args:
33+
parser.error("missing operand")
34+
35+
dirname(opts, args)

0 commit comments

Comments
 (0)