-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.pl
executable file
·69 lines (56 loc) · 1.32 KB
/
cmdline.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
#!/usr/bin/perl
#
# Module: cmdline.pl
# Purpose: This module demonstrates getopt in Perl
# Author: Wade Hampton
# Date: 10/7/2015
# Notes:
# 1) simple getopt command line processing
#
# define defaults
my $verbose = 0;
my $ifile = "no_ifile.txt";
my $ofile = "no_ofile.txt";
# include the getopt module
use Getopt::Std;
# print all options
sub PrintOptions($$$) {
my ($v, $if, $of) = @_;
printf "Verbose=%d\n", $v;
printf "Ifile=%s\n", $if;
printf "Ofile=%s\n", $of;
}
print "cmdline.pl: test of command line\n";
# this is better than using ARGV
print "Use of @ARGV input array, very-basic input processing\n";
my $cnt = @ARGV;
if ($cnt == 0) {
print "no args\n";
} else {
for (my $ii=0; $ii<$cnt; $ii++) {
printf " %04d %s\n", $ii, $ARGV[$ii];
}
}
# parse using getopts
print "Use of getopts(), like C getopt()\n";
getopts("v:i:o:h");
# define a function for each option you wish to get
if ($opt_h) {
print "Options:\n";
print " -v n : enable verbose level n\n";
print " -i if : input file if\n";
print " -o of : output file of\n";
print " -h : print this help\n";
exit(0);
}
if ($opt_v) {
$verbose = $opt_v;
}
if ($opt_i) {
$ifile = $opt_i;
}
if ($opt_o) {
$ofile = $opt_o;
}
PrintOptions($verbose, $ifile, $ofile);
exit(0);