Commit 5ecec274 authored by Peter Pentchev's avatar Peter Pentchev

Add the --help and --version long options.

parent c56e4bf6
......@@ -4,6 +4,7 @@ Change log for ethstats, the network traffic statistics utility
- fix a Perl 6 bug: allow the ".", "/", and "-" characters in
an interface name
- allow the program to be run directly during the tests
- add the "--help" and "--version" long options
1.1.1 2015/12/28
- fix a grave bug I introduced in 1.1.0: get the correct value
......
......@@ -49,8 +49,13 @@ test: all
${PERL} ${TEST_SCRIPT} -V -h
@echo
@echo "=== Long options for version and usage"
${TEST_SCRIPT} --version
${TEST_SCRIPT} --help
@echo "=== Error out on invalid options"
! ${PERL} ${TEST_SCRIPT} -X
! ${TEST_SCRIPT} --whee
@echo
@if [ -r "/proc/net/dev" ]; then \
......
......@@ -19,7 +19,7 @@ sub usage(Bool:D $err = True)
{
my $s = q:to/EOUSAGE/;
Usage: ethstats [-t] [-C | -M] [-c count] [-i iface] [-n period]
ethstats -V | -h
ethstats -V | --version | -h | --help
-C color the "total" line in the output
-c exit after the specified number of samples
......@@ -212,6 +212,17 @@ sub convert(Str $iface, UInt:D $period, InterfaceStats:D $total)
$opts.bool('h', Nil, -> $v { %flags<h> = $v });
$opts.bool('V', Nil, -> $v { %flags<V> = $v });
$opts.str('-', Nil, -> $v {
given $v {
when 'help' { %flags<h> = True }
when 'version' { %flags<V> = True }
default {
note "Invalid long option '$v'";
usage;
}
}
});
my Bool $addtime = False;
$opts.bool('t', Nil, -> $v { $addtime = $v; });
......
......@@ -33,7 +33,7 @@ sub usage($)
my ($err) = @_;
my $s = <<EOUSAGE
Usage: ethstats [-t] [-C | -M] [-c count] [-i iface] [-n period]
ethstats -V | -h
ethstats -V | --version | -h | --help
-C color the "total" line in the output
-c exit after the specified number of samples
......@@ -154,10 +154,23 @@ MAIN:
my $period = 10;
my %opts;
getopts 'Cc:hi:Mn:tV', \%opts or usage(1);
version if $opts{V};
usage 0 if $opts{h};
exit 0 if $opts{V} || $opts{h};
getopts 'Cc:hi:Mn:tV-:', \%opts or usage(1);
my $Vflag = $opts{V} || ($opts{'-'} // '') eq 'version';
my $hflag = $opts{h};
my $dash = $opts{'-'};
if (defined $dash) {
if ($dash eq 'version') {
$Vflag = 1;
} elsif ($dash eq 'help') {
$hflag = 1;
} else {
warn "Invalid long option '$dash'\n";
usage 1;
}
}
version if $Vflag;
usage 0 if $hflag;
exit 0 if $Vflag || $hflag;
my $addtime = $opts{t};
if (defined $opts{n}) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment