From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29686 invoked from network); 5 Sep 2005 11:39:01 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 5 Sep 2005 11:39:01 -0000 Received: (qmail 86622 invoked from network); 5 Sep 2005 11:38:53 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 5 Sep 2005 11:38:53 -0000 Received: (qmail 28688 invoked by alias); 5 Sep 2005 11:38:45 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 9383 Received: (qmail 28678 invoked from network); 5 Sep 2005 11:38:45 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 5 Sep 2005 11:38:45 -0000 Received: (qmail 85512 invoked from network); 5 Sep 2005 11:38:44 -0000 Received: from ns9.hostinglmi.net (213.194.149.146) by a.mx.sunsite.dk with SMTP; 5 Sep 2005 11:38:40 -0000 Received: from 212.red-80-35-44.pooles.rima-tde.net ([80.35.44.212]:32773 helo=localhost) by ns9.hostinglmi.net with esmtpa (Exim 4.52) id 1ECFJQ-0008Iv-Qn for zsh-users@sunsite.dk; Mon, 05 Sep 2005 13:38:57 +0200 Date: Mon, 5 Sep 2005 13:42:53 +0200 From: DervishD To: Zsh Users Subject: Factoring out code Message-ID: <20050905114253.GA7132@DervishD> Mail-Followup-To: Zsh Users Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.4.2.1i Organization: DervishD X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - ns9.hostinglmi.net X-AntiAbuse: Original Domain - sunsite.dk X-AntiAbuse: Originator/Caller UID/GID - [0 0] / [47 12] X-AntiAbuse: Sender Address Domain - dervishd.net X-Source: X-Source-Args: X-Source-Dir: X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.4 Hi all :) Most (probably all...) of my scrips start like this: ---- cut here ---- 8< ---- emulate -L zsh # Not all of them have this: [[ $# -eq 0 ]] && set -- --help # But almost all have this: [[ $# -gt 0 && $argv[(i)--help] -le $# ]] && { # Show help output here return 0 } # ...and this: [[ $# -gt 0 && $argv[(i)--doc] -le $# ]] && { # Show internal documentation here return 0 } ---- cut here ---- >8 ---- OK, so I've factored out code: ---- cut here ---- 8< ---- # This is "common.sh", and MUST BE SOURCED! emulate -L zsh # Dummy stubs, just in case the caller didn't gave them. function doc() { print "This script doesn't have internal documentation yet!" return 0 } function help() { print "This script doesn't have help output yet!" return 0 } # We make argv global so we really are messing with the # argv of OUR CALLER typeset -gx argv # Handle a couple of options [[ $# -gt 0 && $argv[(i)--help] -le $# ]] && help [[ $# -gt 0 && $argv[(i)--doc] -le $# ]] && doc # Delete handled options, that is, MODIFY caller's argv! # ... ---- cut here ---- >8 ---- My scripts now look like this: ---- cut here ---- 8< ---- . common.sh # We want to make '--help' the default option for this script: [[ $# -eq 0 ]] && set -- --help function help() { # Here goes the real help output } function doc() { # Here goes the real doc output } # Do the rest of the job # We should source "common.sh" here, really, # in order to make it work... ---- cut here ---- >8 ---- Of course this doesn't work, because the sourceing of 'common.sh' should be done AFTER the function declarations and AFTER the default argv setting. But then, there will be some code that I won't be able to factor out :(, because some things (not factored out yet) MUST be done BEFORE any other code is run, that is, before of declaring funcions and the like. I cannot put in a function the code needed to be run AFTER the function declarations, because then the argv thing won't work, and I cannot pass argv as a parameter because it has to be modified :( How can I make "common.sh" to run code BEFORE and AFTER some point in the script which sources it, if that code must modify global variables in the caller and work like a cut'n'paste (I mean, for the 'sourcer' script the code should run like if was cut from "common.sh" and pasted into the file, just like a macro)? The only solution I've found so far is to source "common.sh" after declaring the functions and running other code, but then I loose the ability of factoring out the 'emulate -L zsh' code, some options settings and additional code that must be factored, and this will lead to the same maintenance problems I have right now :( Thanks a lot in advance, and if you need more information I can provide more examples. I'm afraid I haven't explained the issue quite well :((( Raúl Núñez de Arenas Coronado -- Linux Registered User 88736 | http://www.dervishd.net http://www.pleyades.net & http://www.gotesdelluna.net It's my PC and I'll cry if I want to...