-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasename.py
More file actions
82 lines (70 loc) · 2.14 KB
/
basename.py
File metadata and controls
82 lines (70 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: hamdy.aea@protonmail.com
Date of creation: 2-11-2024
Last update: 2-11-2024
Version: 1.0
Description: A clone of the baseman command from GNU coreutils in Python3
Example of use: python3 basename.py /usr/bin/sort
'''
import os
import sys
VERSION = "basename (Python coreutils) 1.0"
def print_help():
help_text = """
Usage: basename [OPTION]... NAME...
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.
-a, --multiple support multiple arguments and treat each as a NAME
-s, --suffix=SUFFIX remove a trailing SUFFIX; implies -a
-z, --zero end each output line with NUL, not newline
--help display this help and exit
--version output version information and exit
"""
print(help_text.strip())
def basename(name, suffix=None):
base = os.path.basename(name)
if suffix and base.endswith(suffix):
base = base[:-len(suffix)]
return base
def main():
args = sys.argv[1:]
multiple = False
suffix = None
zero_terminated = False
if "--help" in args:
print_help()
return
if "--version" in args:
print(VERSION)
return
names = []
i = 0
while i < len(args):
arg = args[i]
if arg in ("-a", "--multiple"):
multiple = True
elif arg.startswith("-s=") or arg.startswith("--suffix="):
suffix = arg.split("=", 1)[1]
multiple = True
elif arg == "-s" or arg == "--suffix":
i += 1
suffix = args[i]
multiple = True
elif arg in ("-z", "--zero"):
zero_terminated = True
else:
names.append(arg)
i += 1
if not names:
print("basename: missing operand")
sys.exit(1)
if not multiple and len(names) > 1:
print("basename: extra operand", names[1:])
sys.exit(1)
output_terminator = "\0" if zero_terminated else "\n"
for name in names:
print(basename(name, suffix=suffix), end=output_terminator)
if __name__ == "__main__":
main()