From mschwartz at mn.rr.com  Mon Sep 26 21:09:23 2005
From: mschwartz at mn.rr.com (Marc Schwartz (via MN))
Date: Mon, 26 Sep 2005 14:09:23 -0500
Subject: [Rd] R's parsing of command line arguments using commandArgs()
In-Reply-To: <Pine.WNT.4.58.0509261703070.3800@auk>
References: <1127507823.4947.52.camel@localhost.localdomain>
	<1127749860.4288.18.camel@localhost.localdomain>
	<Pine.WNT.4.58.0509261703070.3800@auk>
Message-ID: <1127761763.4288.54.camel@localhost.localdomain>

On Mon, 2005-09-26 at 17:12 +0100, Prof Brian D Ripley wrote:
> On Mon, 26 Sep 2005, Marc Schwartz (via MN) wrote:
> 
> > On Fri, 2005-09-23 at 15:37 -0500, Marc Schwartz (via MN) wrote:

[snip]

> > > However, if there is any whitespace in the two arguments, such as:
> > >
> > > R --slave --vanilla --args "c(5, 5)" "c(.5, .5)" < RScript.R
> > >
> > > even though surrounded by double quotes (or single quotes or
> > > backquotes), the two arguments are parsed as four.
> > >
> > > Is this behavior expected? I was under the impression, from other C
> > > based programs and bash shell scripts for example, that the use of the
> > > double quotes would wrap such text and thus be parsed as a single
> > > argument.
> 
> Sort of.  Unfortunately both the R front-end script and the R executable
> get to play here, so once the front-end has parsed the args the quoting
> gets lost.  You might hope that double quoting would help, but it does not.

Prof. Ripley,

Thanks kindly for your reply. I suspected that there might be something
going on in the startup script and/or the executable, but did not
consider that the quoting gets stripped before being passed on to the
binary. That makes sense of course.

Given that 2.2.0 is imminent, I know timing on this is bad, but is there
any logic in considering something along the lines of the following for
a future enhancement to the startup script relative to the processing of
arguments after '--args'?

This generic script takes the arguments from the command line, parses
them and adds double quotes back to the arguments as an array, before
passing them to the binary executable command line:

#!/bin/sh

declare -i index
declare -a QuotedArgs

index=1

for Args in "$@"
do
  echo "Arg $index = $Args"
  # Arrays are 0 index based
  QuotedArgs[index-1]=\"$Args\"
  index=index+1
done

echo "R Exec Command: /usr/local/lib/bin/exec/R ${QuotedArgs[@]}"



Thus, from the command line, you get the following:

$ ./QuoteArgs one two three
Arg 1 = one
Arg 2 = two
Arg 3 = three
R Exec Command: /usr/local/lib/bin/exec/R "one" "two" "three"


$ ./QuoteArgs "one two" three
Arg 1 = one two
Arg 2 = three
R Exec Command: /usr/local/lib/bin/exec/R "one two" "three"


$ ./QuoteArgs "one two three"
Arg 1 = one two three
R Exec Command: /usr/local/lib/bin/exec/R "one two three"



Let me know your thoughts, especially any gotchas on this approach.

Best regards,

Marc



[Rd] R's parsing of command line arguments using commandArgs()
Marc Schwartz (via MN