zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: TJ Luoma <luomat@gmail.com>, Zsh-Users List <zsh-users@zsh.org>
Subject: Re: how do I get the last argument from a list of arguments?
Date: Mon, 07 Jul 2014 15:06:25 -0700	[thread overview]
Message-ID: <140707150625.ZM20692@torch.brasslantern.com> (raw)
In-Reply-To: <CADjGqHuALx7Ue3Ei3s7XvT_pHWxECo2rt=BNegPO5Fwcuge2LQ@mail.gmail.com>

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.


  parent reply	other threads:[~2014-07-07 22:07 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <luomat@gmail.com>
2014-07-07 21:06 ` TJ Luoma
2014-07-07 21:33   ` Kurtis Rader
2014-07-07 21:35   ` Peter Stephenson
2014-07-07 22:06   ` Bart Schaefer [this message]
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

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=140707150625.ZM20692@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=luomat@gmail.com \
    --cc=zsh-users@zsh.org \
    /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).