-
Notifications
You must be signed in to change notification settings - Fork 45
/
basename.pl
executable file
·91 lines (72 loc) · 1.55 KB
/
basename.pl
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
82
83
84
85
86
87
88
89
90
#!/usr/bin/perl -w
#
# basename.pl -- strip directory and suffix from filenames. {{{1
#
# Author: soarpenguin <[email protected]>
# First release Nov.14 2012
# 1}}}
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
#use Smart::Comments;
use Term::ANSIColor;
#print color("red"), "Stop!\n", color("reset");
#print color("green"), "Go!\n", color("reset");
my $script = basename $0;
my $myversion = '0.1.0';
my $usage = "
Usage: $script NAME [SUFFIX]
or: $script OPTION
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.
-h, --help Display this help and exit
-V, --version Display version information.
Examples:
$script /usr/bin/sort Output \"sort\".
$script include/stdio.h .h Output \"stdio\".
";
my $ret = GetOptions(
'help|h' => \&usage,
'version|V' => \&version
);
if(! $ret) {
&usage();
}
if(@ARGV > 2 or @ARGV <= 0) {
&usage();
}
&main();
sub main {
my ($file, $suffix) = @ARGV;
my @basename;
### $file
### $suffix
if(! $file) {
&usage();
}
if($file =~ /\//) {
@basename = split('\/', $file);
### @basename
my $scalar = @basename;
### $scalar
$file = $basename[$scalar - 1];
}
if($suffix) {
if($file =~ /(.*)$suffix$/) {
$file = $1;
}
}
print "$file\n";
### $file
}
sub usage {
print color("blue");
print $usage;
print color("reset");
exit;
}
sub version {
print "$script version $myversion\n";
exit;
}