* how do I get the last argument from a list of arguments?
@ 2014-07-07 21:06 ` TJ Luoma
2014-07-07 21:33 ` Kurtis Rader
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: TJ Luoma @ 2014-07-07 21:06 UTC (permalink / raw)
To: Zsh-Users List
I’m trying to learn better ways of dealing with arguments given to a
function, because I am sure that I am not doing it the most efficient
way.
For example, if I want to process a series of args, I usually use a
loop like this:
for FOO in "$@"
do
case "$FOO" in
-t|--to)
shift
TO="$1"
shift
;;
-v|--verbose)
VERBOSE='yes'
shift
;;
-*|--*)
echo " $NAME [warning]: Don't know what to do with arg: $1"
shift
;;
esac
done # for args
That has worked OK for what I've needed to do, but now I'm trying to
create two functions which I will use in place of 'cp' and 'mv' and I
need to be able to find the _last_ argument (the destination) before I
process all the rest of the args.
The only way that I can think of to get the last argument is to do
something like this
LAST=`echo "$@" | awk '{print $NF}'`
but that made me wonder if there wasn’t a better way.
Note that this does not need to be 'portable' at all -- I will happily
use any zsh-specific features which may exist, as long as it works in
5.0.2 (which is what comes with OS X).
Thanks!
TjL
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how do I get the last argument from a list of arguments?
2014-07-07 21:06 ` how do I get the last argument from a list of arguments? TJ Luoma
@ 2014-07-07 21:33 ` Kurtis Rader
2014-07-07 21:35 ` Peter Stephenson
` (2 subsequent siblings)
3 siblings, 0 replies; 9+ messages in thread
From: Kurtis Rader @ 2014-07-07 21:33 UTC (permalink / raw)
To: TJ Luoma; +Cc: Zsh-Users List
[-- Attachment #1: Type: text/plain, Size: 1796 bytes --]
See the description of the *${name:offset} *syntax in *man zshexpn*. Note
the space between the colon and minus-sign is required to disambiguate it
from the "var:-default" syntax.
function lastarg() {
print "${@[-1]}"
print "${@: -1}"
}
lastarg 1 2 3
On Mon, Jul 7, 2014 at 2:06 PM, TJ Luoma <luomat@gmail.com> wrote:
> I’m trying to learn better ways of dealing with arguments given to a
> function, because I am sure that I am not doing it the most efficient
> way.
>
> For example, if I want to process a series of args, I usually use a
> loop like this:
>
> for FOO in "$@"
> do
> case "$FOO" in
> -t|--to)
> shift
> TO="$1"
> shift
> ;;
>
> -v|--verbose)
> VERBOSE='yes'
> shift
> ;;
>
> -*|--*)
> echo " $NAME [warning]: Don't know what to do with arg: $1"
> shift
> ;;
>
> esac
>
> done # for args
>
> That has worked OK for what I've needed to do, but now I'm trying to
> create two functions which I will use in place of 'cp' and 'mv' and I
> need to be able to find the _last_ argument (the destination) before I
> process all the rest of the args.
>
> The only way that I can think of to get the last argument is to do
> something like this
>
> LAST=`echo "$@" | awk '{print $NF}'`
>
> but that made me wonder if there wasn’t a better way.
>
> Note that this does not need to be 'portable' at all -- I will happily
> use any zsh-specific features which may exist, as long as it works in
> 5.0.2 (which is what comes with OS X).
>
> Thanks!
>
> TjL
>
--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how do I get the last argument from a list of arguments?
2014-07-07 21:06 ` how do I get the last argument from a list of arguments? TJ Luoma
2014-07-07 21:33 ` Kurtis Rader
@ 2014-07-07 21:35 ` Peter Stephenson
2014-07-07 22:06 ` Bart Schaefer
2014-07-08 12:52 ` zzapper
3 siblings, 0 replies; 9+ messages in thread
From: Peter Stephenson @ 2014-07-07 21:35 UTC (permalink / raw)
To: Zsh-Users List
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 650 bytes --]
TJ Luoma wrote:
> That has worked OK for what I've needed to do, but now I'm trying to
> create two functions which I will use in place of 'cp' and 'mv' and I
> need to be able to find the _last_ argument (the destination) before I
> process all the rest of the args.
>
> The only way that I can think of to get the last argument is to do
> something like this
>
> LAST=`echo "$@" | awk '{print $NF}'`
>
> but that made me wonder if there wasn’t a better way.
The arguments are available as the array argv, so use $argv[-1].
--
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how do I get the last argument from a list of arguments?
2014-07-07 21:06 ` how do I get the last argument from a list of arguments? TJ Luoma
2014-07-07 21:33 ` Kurtis Rader
2014-07-07 21:35 ` Peter Stephenson
@ 2014-07-07 22:06 ` Bart Schaefer
2014-07-08 12:52 ` zzapper
3 siblings, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2014-07-07 22:06 UTC (permalink / raw)
To: TJ Luoma, Zsh-Users List
On Jul 7, 5:06pm, TJ Luoma wrote:
}
} I'm trying to learn better ways of dealing with arguments given to a
} function, because I am sure that I am not doing it the most efficient
} way.
If the arguments are defined in a fairly regular way, zparseopts is
probably the fastest method.
E.g. your "for FOO" loop becomes something like:
local -A opts
zparseopts -A opts -D -E -M t:=-to -to: v=-verbose -verbose
while [[ $1 = -* ]]
do
echo " $NAME [warning]: Don't know what to do with arg: $1"
shift
done
# You can skip this part and use $opts[] directly, but:
local TO=$opts[--to] VERBOSE=${opts[--verbose]+yes}
With the -E -D -K flags you can call zparseopts multiple times if you
want to (-E means skip over unspecified options, -D means remove the
specified ones, and -K means to keep the results of previous calls).
} That has worked OK for what I've needed to do, but now I'm trying to
} create two functions which I will use in place of 'cp' and 'mv' and I
} need to be able to find the _last_ argument (the destination) before I
} process all the rest of the args.
See Kurtis' reply regarding how to access arrays from the tail end.
However, GNU cp/mv both take a --target= option so the directory name
needn't be the last argument. So you could do something like
zparseopts -E -D -A opts --target:
if [[ -z $opts[--target] ]]
then
opts[--target]=$@[-1]
shift -p
fi
That "shift -p" means to "pop" the last argument, in the "cp" example
leaving $@ holding only the file names you are interested in copying.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how do I get the last argument from a list of arguments?
2014-07-07 21:06 ` how do I get the last argument from a list of arguments? TJ Luoma
` (2 preceding siblings ...)
2014-07-07 22:06 ` Bart Schaefer
@ 2014-07-08 12:52 ` zzapper
2014-07-08 14:11 ` zzapper
3 siblings, 1 reply; 9+ messages in thread
From: zzapper @ 2014-07-08 12:52 UTC (permalink / raw)
To: zsh-users
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 540 bytes --]
TJ Luoma <luomat@gmail.com> wrote in
news:CADjGqHuALx7Ue3Ei3s7XvT_pHWxECo2rt=BNegPO5Fwcuge2LQ@mail.gmail.com:
> I’m trying to learn better ways of dealing with arguments given to a
> function, because I am sure that I am not doing it the most efficient
> way.
Similar to Kurtis
p()
{
echo "last ${@[-1]}"
echo "2nd last ${@[-2]}"
}
p a b c d
--
zzapper
https://twitter.com/dailyzshtip
---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how do I get the last argument from a list of arguments?
2014-07-08 12:52 ` zzapper
@ 2014-07-08 14:11 ` zzapper
2014-07-08 15:06 ` Roman Neuhauser
0 siblings, 1 reply; 9+ messages in thread
From: zzapper @ 2014-07-08 14:11 UTC (permalink / raw)
To: zsh-users
zzapper <david@rayninfo.co.uk> wrote in
news:XnsA3648D0E647C1davidrayninfocouk@80.91.229.13:
> p()
> {
> echo "last ${@[-1]}"
> echo "2nd last ${@[-2]}"
> }
> p a b c d
just trying to see if i can display the at sign correctly by using an html
entity & #64 ;
--
zzapper
https://twitter.com/dailyzshtip
---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how do I get the last argument from a list of arguments?
2014-07-08 14:11 ` zzapper
@ 2014-07-08 15:06 ` Roman Neuhauser
2014-07-08 15:25 ` zzapper
0 siblings, 1 reply; 9+ messages in thread
From: Roman Neuhauser @ 2014-07-08 15:06 UTC (permalink / raw)
To: zzapper; +Cc: zsh-users
# david@rayninfo.co.uk / 2014-07-08 14:11:40 +0000:
> zzapper <david@rayninfo.co.uk> wrote in
> news:XnsA3648D0E647C1davidrayninfocouk@80.91.229.13:
>
> > p()
> > {
> > echo "last ${@[-1]}"
> > echo "2nd last ${@[-2]}"
> > }
> > p a b c d
>
>
> just trying to see if i can display the at sign correctly by using an html
> entity & #64 ;
your email had no MIME information let alone the text/html content type.
--
roman
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how do I get the last argument from a list of arguments?
2014-07-08 15:06 ` Roman Neuhauser
@ 2014-07-08 15:25 ` zzapper
2014-07-08 15:42 ` Roman Neuhauser
0 siblings, 1 reply; 9+ messages in thread
From: zzapper @ 2014-07-08 15:25 UTC (permalink / raw)
To: zsh-users
Roman Neuhauser <neuhauser@sigpipe.cz> wrote in
news:20140708150621.GJ11492@isis.sigpipe.cz:
> # david@rayninfo.co.uk / 2014-07-08 14:11:40 +0000:
>> just trying to see if i can display the at sign correctly by using an
>> html entity & #64 ;
>
> your email had no MIME information let alone the text/html content
> type.
>
Roman
Sorry for confusion was only on the
http://news.gmane.org/gmane.comp.shells.zsh.user
website where they automatically replace all @ with < at>
--
zzapper
https://twitter.com/dailyzshtip
---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how do I get the last argument from a list of arguments?
2014-07-08 15:25 ` zzapper
@ 2014-07-08 15:42 ` Roman Neuhauser
0 siblings, 0 replies; 9+ messages in thread
From: Roman Neuhauser @ 2014-07-08 15:42 UTC (permalink / raw)
To: zzapper; +Cc: zsh-users
# david@rayninfo.co.uk / 2014-07-08 15:25:31 +0000:
> Sorry for confusion was only on the
> http://news.gmane.org/gmane.comp.shells.zsh.user
>
> website where they automatically replace all @ with < at>
yeah, gmane sucks.
--
roman
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2014-07-08 15:43 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <luomat@gmail.com>
2014-07-07 21:06 ` how do I get the last argument from a list of arguments? TJ Luoma
2014-07-07 21:33 ` Kurtis Rader
2014-07-07 21:35 ` Peter Stephenson
2014-07-07 22:06 ` Bart Schaefer
2014-07-08 12:52 ` zzapper
2014-07-08 14:11 ` zzapper
2014-07-08 15:06 ` Roman Neuhauser
2014-07-08 15:25 ` zzapper
2014-07-08 15:42 ` Roman Neuhauser
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).