From owner-freebsd-questions@FreeBSD.ORG Fri Jun 4 07:04:16 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 73928106564A for ; Fri, 4 Jun 2010 07:04:16 +0000 (UTC) (envelope-from steve@ipv6canada.com) Received: from smtp.ibctech.ca (v6.ibctech.ca [IPv6:2607:f118::b6]) by mx1.freebsd.org (Postfix) with SMTP id D60E28FC16 for ; Fri, 4 Jun 2010 07:04:15 +0000 (UTC) Received: (qmail 90351 invoked by uid 89); 4 Jun 2010 07:08:31 -0000 Received: from unknown (HELO ?IPv6:2607:f118::5?) (steve@ibctech.ca@2607:f118::5) by 2607:f118::b6 with ESMTPA; 4 Jun 2010 07:08:31 -0000 Message-ID: <4C08A56F.5050007@ipv6canada.com> Date: Fri, 04 Jun 2010 03:04:15 -0400 From: Steve Bertrand User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20100111 Lightning/1.0b1 Thunderbird/3.0.1 MIME-Version: 1.0 To: Aiza References: <4C0882AC.4000504@comclark.com> In-Reply-To: <4C0882AC.4000504@comclark.com> X-Enigmail-Version: 1.0.1 Content-Type: text/plain; charset=ISO-8859-1 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:04:16 -0000 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__