zsh-users
 help / color / mirror / code / Atom feed
* Mapping quoted parameter in function
@ 2016-11-10  6:20 Bernd Steinhauser
  2016-11-10 12:29 ` Clint Hepner
  0 siblings, 1 reply; 4+ messages in thread
From: Bernd Steinhauser @ 2016-11-10  6:20 UTC (permalink / raw)
  To: zsh-users

Hi,

I'm using a program that expects a parameter (actually multiple parameters) in 
the form
program "foo='bar'"

Because the cmdline for that program gets quite long, I wrote a function to call 
it and change parameters easily,it looks roughly like this:
progfunc() {
CORES=12
     program -n ${CORES} "foo='bar'" foo2="1 $3"
}

What I would want to do is to ensure that if I call
`progfunc x`

this would translate into "foo='x'", without touching the rest of the call.
Is that somehow possible?
iirc, variables won't work, because of the quoting style?

Best Regards,
Bernd


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Mapping quoted parameter in function
  2016-11-10  6:20 Mapping quoted parameter in function Bernd Steinhauser
@ 2016-11-10 12:29 ` Clint Hepner
  2016-11-10 16:48   ` Bernd Steinhauser
  0 siblings, 1 reply; 4+ messages in thread
From: Clint Hepner @ 2016-11-10 12:29 UTC (permalink / raw)
  To: Bernd Steinhauser; +Cc: zsh-users


> On 2016 Nov 10 , at 1:20 a, Bernd Steinhauser <linux@bernd-steinhauser.de> wrote:
> 
> Hi,
> 
> I'm using a program that expects a parameter (actually multiple parameters) in the form
> program "foo='bar'"
> 
> Because the cmdline for that program gets quite long, I wrote a function to call it and change parameters easily,it looks roughly like this:
> progfunc() {
> CORES=12
>    program -n ${CORES} "foo='bar'" foo2="1 $3"
> }
> 
> What I would want to do is to ensure that if I call
> `progfunc x`
> 
> this would translate into "foo='x'", without touching the rest of the call.
> Is that somehow possible?
> iirc, variables won't work, because of the quoting style?
> 
> Best Regards,
> Bernd

Just replace bar with a parameter default expansion.

    progfunc () {
      CORES=12
      program -n $CORES "foo='${1:-bar}' foo2="1 $3"
    }

    progfunc              # foo='bar'
    progfunc "hi there"   # foo='hi there'

If you don’t pass a first argument, bar is used. Otherwise, the value of the argument is.

The single quotes here don’t actually quote anything; they are literal characters included in the *double*-quoted string.

Clint

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Mapping quoted parameter in function
  2016-11-10 12:29 ` Clint Hepner
@ 2016-11-10 16:48   ` Bernd Steinhauser
  0 siblings, 0 replies; 4+ messages in thread
From: Bernd Steinhauser @ 2016-11-10 16:48 UTC (permalink / raw)
  Cc: zsh-users

On 10/11/16 13:29, Clint Hepner wrote:
>
>> On 2016 Nov 10 , at 1:20 a, Bernd Steinhauser <linux@bernd-steinhauser.de> wrote:
>>
>> Hi,
>>
>> I'm using a program that expects a parameter (actually multiple parameters) in the form
>> program "foo='bar'"
>>
>> Because the cmdline for that program gets quite long, I wrote a function to call it and change parameters easily,it looks roughly like this:
>> progfunc() {
>> CORES=12
>>    program -n ${CORES} "foo='bar'" foo2="1 $3"
>> }
>>
>> What I would want to do is to ensure that if I call
>> `progfunc x`
>>
>> this would translate into "foo='x'", without touching the rest of the call.
>> Is that somehow possible?
>> iirc, variables won't work, because of the quoting style?
>>
>> Best Regards,
>> Bernd
>
> Just replace bar with a parameter default expansion.
>
>     progfunc () {
>       CORES=12
>       program -n $CORES "foo='${1:-bar}' foo2="1 $3"
>     }
>
>     progfunc              # foo='bar'
>     progfunc "hi there"   # foo='hi there'
>
> If you don’t pass a first argument, bar is used. Otherwise, the value of the argument is.
>
> The single quotes here don’t actually quote anything; they are literal characters included in the *double*-quoted string.

Thanks, so my misunderstanding actually led me to not even try this.
Feels a bit embarrassing. ;)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Mapping quoted parameter in function
       [not found] <0a521f25-d548-d3b1-fb2e-7559f7995b7d__49592.623851686$1478759489$gmane$org@bernd-steinhauser.de>
@ 2016-11-10 16:10 ` Daniel Shahaf
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Shahaf @ 2016-11-10 16:10 UTC (permalink / raw)
  To: Bernd Steinhauser; +Cc: zsh-users

Bernd Steinhauser wrote on Thu, Nov 10, 2016 at 07:20:17 +0100:
> progfunc() {
> CORES=12
>     program -n ${CORES} "foo='bar'" foo2="1 $3"
> }
> 
> What I would want to do is to ensure that if I call
> `progfunc x`
> 
> this would translate into "foo='x'", without touching the rest of the call.
> Is that somehow possible?

If you want to get «x» as your $1 argument and have the callee see
«foo='x'» as its argument, you can do this:

    progfunc() { program "foo='$1'" }

Or this:

    progfunc() { program "foo=${(q)1}" }

If that doesn't answer your question, then please clarify it.

> iirc, variables won't work, because of the quoting style?

«"foo'$bar"» does interpolate $bar as a variable, despite the single
quote in there, because the ' is literal (part of the string data).
«"foo"'$bar'» doesn't, because the ' is syntactical.

Cheers,

Daniel


> Best Regards,
> Bernd
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-11-13  4:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-10  6:20 Mapping quoted parameter in function Bernd Steinhauser
2016-11-10 12:29 ` Clint Hepner
2016-11-10 16:48   ` Bernd Steinhauser
     [not found] <0a521f25-d548-d3b1-fb2e-7559f7995b7d__49592.623851686$1478759489$gmane$org@bernd-steinhauser.de>
2016-11-10 16:10 ` Daniel Shahaf

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