-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcopy_cat.pl
66 lines (56 loc) · 1.39 KB
/
copy_cat.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
use strict;
use warnings;
use File::Spec;
use FindBin;
use Getopt::Long qw(GetOptions);
use lib File::Spec->catdir($FindBin::Bin, 'lib');
use lib File::Spec->catdir($FindBin::Bin, 'lib', 'cats-problem');
use CATS::ConsoleColor;
use CATS::FileUtil;
use CATS::Loggers;
sub usage {
my ($error) = @_;
print "$error\n" if $error;
my $text = <<"USAGE";
Copies input to either output or STDOUT.
Usage:
$FindBin::Script -i <input> -o <output> -m <mode> [-l <line>]
Options:
#-i# Input file
#-o# Output file
#-m# *STDOUT, *NONE or FILE
#-l# Copy only line with given number
USAGE
;
$text =~ s/#(\S+)#/CATS::ConsoleColor::colored($1, 'bold white')/eg;
print $text;
exit;
}
GetOptions(
'i=s' => \(my $input),
'o=s' => \(my $output),
'm=s' => \(my $mode = ''),
'l=i' => \(my $line_number = ''),
);
$input && ($output || $mode) or usage;
my $is_stdout = $mode =~ /^\*STDOUT|\*NONE$/;
if ($line_number) {
my $fout;
if ($is_stdout) {
$fout = *STDOUT;
}
else {
open $fout, '>', $output or die $!;
}
open my $fin, '<', $input or die $!;
for (my $line = 1; <$fin>; ++$line) {
print $fout $_ if $line == $line_number;
}
}
elsif ($is_stdout) {
open my $fin, '<', $input or die $!;
print while <$fin>;
}
else {
CATS::FileUtil->new({ logger => CATS::Logger::Die->new })->copy($input, $output);
}