Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Factoring out code
- X-seq: zsh-users 9383
- From: DervishD <zsh@xxxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxxxxx>
- Subject: Factoring out code
- Date: Mon, 5 Sep 2005 13:42:53 +0200
- Mail-followup-to: Zsh Users <zsh-users@xxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- Organization: DervishD
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...
Messages sorted by:
Reverse Date,
Date,
Thread,
Author