zsh-users
 help / color / mirror / code / Atom feed
From: DervishD <zsh@dervishd.net>
To: Zsh Users <zsh-users@sunsite.dk>
Subject: Factoring out code
Date: Mon, 5 Sep 2005 13:42:53 +0200	[thread overview]
Message-ID: <20050905114253.GA7132@DervishD> (raw)

    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...


             reply	other threads:[~2005-09-05 11:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-05 11:42 DervishD [this message]
2005-09-05 20:08 ` Bart Schaefer
2005-09-06 10:37   ` DervishD
2005-09-06 14:58     ` Bart Schaefer
2005-09-06 15:27       ` DervishD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050905114253.GA7132@DervishD \
    --to=zsh@dervishd.net \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).