forked from soarpenguin/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetopt.pl
84 lines (70 loc) · 1.55 KB
/
getopt.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
#!/usr/bin/perl -w
###################################################################
#Usage:
# perl getopt.pl --verbose --verbose -v --more \
# --lib='/lib' -l '/lib64' --f a=1 --flag b=3 --debug 3 -t ygzhu
#
#Result:
# ### $verbose: 3
# ### $more: 1
# ### $debug: 3
# ### $test: 1
# ### @libs: [
# ### '/lib',
# ### '/lib64'
# ### ]
# ### %flags: {
# ### a => '1',
# ### b => '3'
# ### }
###################################################################
use strict;
use File::Basename;
use Getopt::Long;
# test some modules installed or not.
BEGIN {
if (eval "require Smart::Comments") {
use Smart::Comments;
print "Use Smart::Comments\n";
} else {
warn "No Smart::Comments";
}
}
my $script = File::Basename::basename($0);
my @libs = ();
my %flags = ();
my ( $verbose, $all, $more, $debug, $test);
GetOptions(
'verbose+' => \$verbose, # the '+' means the $verbose will +1
# when the -v or --verbose appear once
'more!' => \$more,
'debug:i' => \$debug,
'lib=s' => \@libs,
'flag=s' => \%flags,
'test|t' => \$test,
#'all|everything|universe!' => $all,
);
use Getopt::Std qw( getopts );
my %opts;
getopts("a:dhl:p:t:uk", \%opts)
or die &usage();
if ($opts{h}) {
print &usage();
exit;
}
sub usage {
return <<'_EOC_';
Usage:
$script [optoins]
Options:
-h Print this usage.
Examples:
$script -h
_EOC_
}
### $verbose
### $more
### $debug
### $test
### @libs;
### %flags