Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 04 Jun 2010 03:04:15 -0400
From:      Steve Bertrand <steve@ipv6canada.com>
To:        Aiza <aiza21@comclark.com>
Cc:        "questions@freebsd.org" <questions@freebsd.org>
Subject:   Re: .sh & getopts
Message-ID:  <4C08A56F.5050007@ipv6canada.com>
In-Reply-To: <4C0882AC.4000504@comclark.com>
References:  <4C0882AC.4000504@comclark.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2010.06.04 00:35, Aiza wrote:
> Have this code
> 
> shift; while getopts :ugr: arg; do case ${arg} in
>    u) action="freebsd-update";;
>    g) action="freebsd-upgrade";;
>    r) action="freebsd-rollback";;
>    ?) exerr ${cmd_usage};;
> esac; done; shift $(( ${OPTION} -1 ))
> 
> 
> Command being executed looks like this, cmd action -flags aaaa bbbb
> 
> Only a single -flag in allowed on the command.

Here's my obligatory "use Perl;"

# it's a dirty hack out of a util script I use that calls
# methods out of a module. 99% of the code has been stripped,
# so forgive me, especially for the dirty arg count check ;)

# save file to test.pl
# chmod 755 test.pl
# Examples:

#  Help:
#  ./test.pl --help
#  ./test.pl -h

# Man page:
#  ./test.pl --man
#  ./test.pl -M

---- copy/paste below this line, until _END_
#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long;
Getopt::Long::Configure qw( bundling );
use Pod::Usage;

if ( $#ARGV > 0 ) {

    my $arg_num = $#ARGV +1 ;
    print "\nYou supplied $arg_num args, when only one is allowed\n\n";

    die "See $0 -h\n\n";
}

my ( $help, $man ) = 0;

my $result = GetOptions(
	        	'update|u'    	=> \&update,
			'upgrade|g'   	=> \&upgrade,
			'rollback|r'  	=> \&rollback,
			'help|h'      	=> \$help,
			'man|M'		=> \$man,
		);

# begin pod2usage

pod2usage({ -verbose => 1 }) if $help;
pod2usage({ -verbose => 2 }) if $man;

sub update {

	print "We're updating!\n";

	# do something fancy here..
	exit;
}

sub upgrade
{

	print "We're upgrading!\n";
	# more fancy stuff...
	exit;
}

sub rollback {

	print "Ensure you have a backup, we're rolling back!\n";
	# uber fancy!!!
	exit;
}



=head1 NAME

perform_maintenance - Do maintenance on FreeBSD

=head1 SYNOPSIS

  # Do update

  ./test.pl --update
  ./test.pl -u

  # Do upgrade

  ./test.pl --upgrade
  ./test.pl -g

  # Do a rollback

  ./test.pl --rollback
  ./test.pl -r

  # display help

  ./test.pl --help
  ./test.pl -h

  # display the manual page

  ./test.pl --man
  ./test.pl -M



=head1 OPTIONS

=over 1



=item --update | -u

Do an update... this example simply outputs 'Update' to STDOUT.



=item --upgrade | -g

Do an upgrade... this example simply outputs 'Upgrade' to STDOUT.



=item --rollback | -r

Perform a rollback... again, of course, we only print out jibberish



=back

=head1 DESCRIPTION

This is a copy/paste of a real-life Perl application that has been
cleared out of all useful code, so it could be used as an example.

It is however an extremely handy framework for accepting both the long
and short forms of parameters, and the perldoc inclusion allows one to
dump 'error' (or more favourably put) help pages onto STDOUT for the user.

=cut

__END__



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4C08A56F.5050007>