From owner-freebsd-questions@FreeBSD.ORG Fri Jun 4 07:29:27 2010 Return-Path: Delivered-To: questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0B07F106564A for ; Fri, 4 Jun 2010 07:29:27 +0000 (UTC) (envelope-from aiza21@comclark.com) Received: from avmxsmtp1.comclark.com (avmxsmtp1.comclark.com [202.69.191.115]) by mx1.freebsd.org (Postfix) with ESMTP id 8F9758FC2D for ; Fri, 4 Jun 2010 07:29:26 +0000 (UTC) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnEZAKhHCEzKRa1aPGdsb2JhbAAHh2aWVwEBAQE1vluFFgSDRg X-IronPort-AV: E=Sophos;i="4.53,360,1272816000"; d="scan'208";a="1939552" Received: from unknown (HELO [10.0.10.3]) ([202.69.173.90]) by avmxsmtp3.comclark.com with ESMTP; 04 Jun 2010 15:29:24 +0800 Message-ID: <4C08AB51.1030609@comclark.com> Date: Fri, 04 Jun 2010 15:29:21 +0800 From: Aiza User-Agent: Thunderbird 2.0.0.17 (Windows/20080914) MIME-Version: 1.0 To: Steve Bertrand References: <4C0882AC.4000504@comclark.com> <4C08A56F.5050007@ipv6canada.com> In-Reply-To: <4C08A56F.5050007@ipv6canada.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: "questions@freebsd.org" Subject: Re: .sh & getopts X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Jun 2010 07:29:27 -0000 Steve Bertrand wrote: > 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. > Steve Bertrand as the subject says .sh not perl.