Browse code

use Getopt::Std for commandline parsing

René Wagner authored on 01.01.2020 18:02:14
Showing 3 changed files

... ...
@@ -1,5 +1,5 @@
1 1
 #!/usr/bin/perl
2
-# Copyright(c) René Wagner 2019
2
+# Copyright(c) René Wagner 2019-2020
3 3
 # published under BSD 3-Clause License
4 4
 # https://www.codewolke.net/photo-helpers/
5 5
 
... ...
@@ -12,42 +12,45 @@ use File::Copy;
12 12
 use File::Find;
13 13
 use File::Find::Rule;
14 14
 use File::Spec::Functions;
15
-use Switch;
15
+use Getopt::Std;
16 16
 
17
-my $VERSION = '0.4';
17
+$Getopt::Std::STANDARD_HELP_VERSION = 'true';
18
+
19
+my $VERSION = '0.5';
18 20
 my $PROGRAM = 'JpegDivider';
19 21
 
20
-my $SUBFOLDER = 'JPEG';
21
-# parse commandline args
22
+# read commandline switches
23
+our $opt_m = 0;
24
+our $opt_f = 'JPEG';
25
+
26
+if ( !getopts('mf:') )
27
+{
28
+	die("Invalid parameters provided! See 'jpegdivider.pl --help' for more details.");
29
+}
30
+
31
+# read remaining commandline args
22 32
 # last dir will win
23 33
 my $work_dir = getcwd;
24
-my $move_flag = 0;
25
-
26 34
 foreach my $arg ( @ARGV ) 
27 35
 {
28
-	switch ( $arg )
29
-	{
30
-		case '-h'  	{ show_help(); exit; }
31
-		case '-m'   { $move_flag = 1; }
32
-	 	else		{ if ( -d $arg ) { $work_dir = $arg } }
33
-	}
36
+	if ( -d $arg ) { $work_dir = $arg }
34 37
 }
35 38
 
36
-main($work_dir, $move_flag);
39
+move_files($work_dir, $opt_m);
37 40
 
38 41
 exit 0;
39 42
 
40
-sub main
43
+sub move_files
41 44
 {
42 45
 	my ( $dir, $move_flag ) = @_;
43 46
 	my $move_counter = 0;
44 47
 
45 48
 	say "working directory: $dir";
46 49
 	say 'action           : '. ($move_flag ? 'move' : 'list only');
47
-	say "subfolder        : $SUBFOLDER";
50
+	say "subfolder        : $opt_f";
48 51
 	say '';
49 52
 	
50
-	my $destination_dir = catdir($dir, $SUBFOLDER);
53
+	my $destination_dir = catdir($dir, $opt_f);
51 54
 	if ( $move_flag && ! -d $destination_dir )
52 55
 	{
53 56
 		mkdir $destination_dir or die "Could not create destination dir $_ : $!";
... ...
@@ -76,12 +79,16 @@ sub main
76 79
 	say "\r\nfound $move_counter jpeg files to subfolder.";
77 80
 }
78 81
 
79
-sub show_help
82
+sub main::VERSION_MESSAGE()
80 83
 {
81 84
 	say $PROGRAM . ' - version ' . $VERSION;
82 85
 	say 'published under BSD 3 - Clause License';
83
-	say 'Copyright(c) 2019 René Wagner';
86
+	say 'Copyright(c) 2019-2020 René Wagner';
84 87
 	say 'https://www.codewolke.net/photo-helpers/';
88
+}
89
+
90
+sub main::HELP_MESSAGE
91
+{
85 92
 	say '';
86 93
 	say 'Moves JPEG files to a designated subfolder, by default "JPEG"';
87 94
 	say '';
... ...
@@ -90,5 +97,5 @@ sub show_help
90 97
 	say 'jpegdivider.pl -m - create a subfolder "JPEG" in the current working dir and move jpeg files in there';
91 98
 	say 'jpegdivider.pl <folder> - list all jpeg files in the given directory';
92 99
 	say 'jpegdivider.pl -m <folder> - create a subfolder "JPEG" in the given directory and move jpeg files in there';
93
-	say 'jpegdivider.pl -h - show this help';
100
+	say 'jpegdivider.pl --help - show this help';
94 101
 }
... ...
@@ -1,5 +1,5 @@
1 1
 #!/usr/bin/perl
2
-# Copyright(c) René Wagner 2019
2
+# Copyright(c) René Wagner 2019-2020
3 3
 # published under BSD 3-Clause License
4 4
 # https://www.codewolke.net/photo-helpers/
5 5
 
... ...
@@ -10,34 +10,39 @@ use Cwd;
10 10
 use File::Basename;
11 11
 use File::Find::Rule;
12 12
 use File::Find;
13
-use Switch;
13
+use Getopt::Std;
14 14
 
15
-my $VERSION = '0.2';
15
+$Getopt::Std::STANDARD_HELP_VERSION = 'true';
16
+
17
+my $VERSION = '0.3';
16 18
 my $PROGRAM = 'Multiversion Sidecar Cleaner';
17 19
 
18
-my $delete_flag = 0;
19
-my $delete_dir = getcwd;
20
+# read commandline switches
21
+our $opt_d = 0;
20 22
 
23
+if ( !getopts('d') )
24
+{
25
+	die("Invalid parameters provided! See 'silkypix_multiversion_sidecar_cleaner.pl --help' for more details.");
26
+}
27
+
28
+# read remaining commandline args
29
+# last dir will win
30
+my $work_dir = getcwd;
21 31
 foreach my $arg ( @ARGV ) 
22 32
 {
23
-	switch ( $arg )
24
-	{
25
-		case '-h'   { show_help(); exit; }
26
-	 	case '-d'	{ $delete_flag = 1; }
27
-	 	else		{ if ( -d $arg ) { $delete_dir = $arg } }
28
-	}
33
+	if ( -d $arg ) { $work_dir = $arg }
29 34
 }
30 35
 
31
-main($delete_dir, $delete_flag);
36
+delete_files($work_dir, $opt_d);
32 37
 
33 38
 exit 0;
34 39
 
35
-sub main
40
+sub delete_files
36 41
 {
37
-	my ( $dir, $delete ) = @_;
38
-	my ( $delcounter ) = 0;
42
+	my ( $dir, $delete_flag ) = @_;
43
+	my ( $file_counter ) = 0;
39 44
 
40
-	say "working directory: $delete_dir";
45
+	say "working directory: $dir";
41 46
 	say 'action           : '. ($delete_flag ? 'delete' : 'list only') . "\r\n";
42 47
 	say 'files with newer versions:';
43 48
 
... ...
@@ -50,8 +55,8 @@ sub main
50 55
 		if ( exist_newer_file($file) )
51 56
 		{
52 57
 			print $file .'...';
53
-			$delcounter++;
54
-			if ( $delete )
58
+			$file_counter++;
59
+			if ( $delete_flag )
55 60
 			{
56 61
 				if ( unlink $file )
57 62
 				{
... ...
@@ -66,7 +71,7 @@ sub main
66 71
 		}
67 72
 	}
68 73
 	
69
-	say "\r\nfound $delcounter duplicate file versions.";
74
+	say "\r\nfound $file_counter duplicate file versions.";
70 75
 }
71 76
 
72 77
 sub exist_newer_file
... ...
@@ -103,12 +108,16 @@ sub exist_newer_file
103 108
 	return 0;
104 109
 }
105 110
 
106
-sub show_help
111
+sub main::VERSION_MESSAGE()
107 112
 {
108 113
 	say $PROGRAM . ' - version ' . $VERSION;
109 114
 	say 'published under BSD 3 - Clause License';
110
-	say 'Copyright(c) 2019 René Wagner';
115
+	say 'Copyright(c) 2019-2020 René Wagner';
111 116
 	say 'https://www.codewolke.net/photo-helpers/';
117
+}
118
+
119
+sub main::HELP_MESSAGE
120
+{
112 121
 	say '';
113 122
 	say 'Deletes Silkypix Sidecar files when newer versions of the files are available.';
114 123
 	say 'This usually happens when a file is opened in a newer version of Silkypix, as the sidecar files are version specific.';
... ...
@@ -118,5 +127,5 @@ sub show_help
118 127
 	say 'duplicate_sidecar_cleaner.pl -d - delete duplicate sidecar file versions in given directory and subfolders';	
119 128
 	say 'duplicate_sidecar_cleaner.pl <folder> - list duplicate sidecar file versions in given directory and subfolders';
120 129
 	say 'duplicate_sidecar_cleaner.pl -d <folder> - delete duplicate sidecar file versions in given directory and subfolders';
121
-	say 'duplicate_sidecar_cleaner.pl -h - show this help';
130
+	say 'duplicate_sidecar_cleaner.pl --help - show this help';
122 131
 }
... ...
@@ -1,5 +1,5 @@
1 1
 #!/usr/bin/perl
2
-# Copyright(c) René Wagner 2019
2
+# Copyright(c) René Wagner 2019-2020
3 3
 # published under BSD 3-Clause License
4 4
 # https://www.codewolke.net/photo-helpers/
5 5
 
... ...
@@ -11,49 +11,52 @@ use File::Basename;
11 11
 use File::Find::Rule;
12 12
 use File::Find;
13 13
 use File::Spec::Functions;
14
-use Switch;
14
+use Getopt::Std;
15
+
16
+$Getopt::Std::STANDARD_HELP_VERSION = 'true';
15 17
 
16 18
 my $VERSION = '0.3';
17 19
 my $PROGRAM = 'Orphaned Sidecar Cleaner';
18 20
 
19
-# parse commandline args
20
-# last dir will win
21
-my $delete_flag = 0;
22
-my $delete_dir = getcwd;
21
+# read commandline switches
22
+our $opt_d = 0;
23
+
24
+if ( !getopts('d') )
25
+{
26
+	die("Invalid parameters provided! See 'silkypix_multiversion_sidecar_cleaner.pl --help' for more details.");
27
+}
23 28
 
29
+# read remaining commandline args
30
+# last dir will win
31
+my $work_dir = getcwd;
24 32
 foreach my $arg ( @ARGV ) 
25 33
 {
26
-	switch ( $arg )
27
-	{
28
-		case '-h'   { show_help(); exit; }
29
-	 	case '-d'	{ $delete_flag = 1; }
30
-	 	else		{ if ( -d $arg ) { $delete_dir = $arg } }
31
-	}
34
+	if ( -d $arg ) { $work_dir = $arg }
32 35
 }
33 36
 
34
-main($delete_dir, $delete_flag);
37
+delete_files($work_dir, $opt_d);
35 38
 
36 39
 exit 0;
37 40
 
38
-sub main
41
+sub delete_files
39 42
 {
40
-	my ( $dir, $delete ) = @_;
41
-	my ( $delcounter ) = 0;
43
+	my ( $delete_dir, $delete_flag ) = @_;
44
+	my ( $file_counter ) = 0;
42 45
 
43 46
 	say "working directory: $delete_dir";
44 47
 	say 'action           : '. ($delete_flag ? 'delete' : 'list only') ."\r\n";
45 48
 	say 'files with missing raw:';
46 49
 
47 50
 	# alle Dateien in allen Unterordnern *.spd *.spf suchen
48
-	my @files = File::Find::Rule->file->name( qr/\.sp(d|f)$/i )->in( $dir );
51
+	my @files = File::Find::Rule->file->name( qr/\.sp(d|f)$/i )->in( $delete_dir );
49 52
 	
50 53
 	foreach my $file ( @files )
51 54
 	{
52 55
 		if ( ! exists_raw_filename($file) )
53 56
 		{
54 57
 			print $file .'...';
55
-			$delcounter++;
56
-			if ( $delete )
58
+			$file_counter++;
59
+			if ( $delete_flag )
57 60
 			{
58 61
 				if ( unlink $file )
59 62
 				{
... ...
@@ -68,7 +71,7 @@ sub main
68 71
 		}
69 72
 	}
70 73
 	
71
-	say "\r\nfound $delcounter orphaned sidecar files found.";
74
+	say "\r\nfound $file_counter orphaned sidecar files found.";
72 75
 }
73 76
 
74 77
 sub exists_raw_filename
... ...
@@ -85,12 +88,16 @@ sub exists_raw_filename
85 88
 	return 0;
86 89
 }
87 90
 
88
-sub show_help
91
+sub main::VERSION_MESSAGE()
89 92
 {
90 93
 	say $PROGRAM . ' - version ' . $VERSION;
91 94
 	say 'published under BSD 3 - Clause License';
92
-	say 'Copyright(c) 2019 René Wagner';
95
+	say 'Copyright(c) 2019-2020 René Wagner';
93 96
 	say 'https://www.codewolke.net/photo-helpers/';
97
+}
98
+
99
+sub main::HELP_MESSAGE
100
+{
94 101
 	say '';
95 102
 	say 'Deletes Silkypix Sidecar files when corresponding RAW files are missing.';
96 103
 	say '';
... ...
@@ -99,5 +106,5 @@ sub show_help
99 106
 	say 'orphaned_sidecar_cleaner.pl -d - delete orphaned sidecar file in current working directory and subfolders';	
100 107
 	say 'orphaned_sidecar_cleaner.pl <folder> - list orphaned sidecar file in given directory and subfolders';
101 108
 	say 'orphaned_sidecar_cleaner.pl -d <folder> - delete orphaned sidecar file in given directory and subfolders';
102
-	say 'orphaned_sidecar_cleaner.pl -h - show this help';
109
+	say 'orphaned_sidecar_cleaner.pl --help - show this help';
103 110
 }