[R] R argparse: Line breaks in description

Dario Beraldi dario.beraldi at gmail.com
Tue Dec 9 10:57:19 CET 2014


On 27 November 2014 at 09:07, Dario Beraldi <dario.beraldi at gmail.com> wrote:

> Hello,
>
> I'm using the R package argparse to parse command line arguments.
>
> For readability, I'd like to add line breaks in the "description" of the
> script and in the help of the arguments. However, I can't do it... Let's
> see an example. Given this script:
>
>     #!/usr/bin/env Rscript
>
>     require(argparse)
>
>     docstring<- "Description\nDone"
>
>     parser<- ArgumentParser(description= docstring)
>     args<- parser$parse_args()
>
> When executed with *-h* it should print:
>
>     Description
>     Done
>
> However, I'm getting the error:
>
>     Error in rjson::fromJSON(output) : unexpected character 'F'
>     Calls: <Anonymous> -> <Anonymous> -> <Anonymous>
>     Execution halted
>
> Variation of docstring like *paste("Description", "Done", sep= '\n')* are
> equally unsuccessful.
>
> As well as passing RawTextHelpFormatter like:
> parser<- ArgumentParser(description= docstring, RawTextHelpFormatter= TRUE)
>
> Any idea how to put line breaks in argparse?
>
> Many thanks!
>
> NB: Cross posted on StackOverflow http://stackoverflow.com/posts/27150625/
>
> Dario
>
>     sessionInfo()
>     R version 3.0.1 (2013-05-16)
>     Platform: x86_64-apple-darwin10.8.0 (64-bit)
>
>     locale:
>     [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
>
>     attached base packages:
>     [1] stats     graphics  grDevices utils     datasets  methods
> base
>
>     other attached packages:
>     [1] argparse_1.0.1 proto_0.3-10
>
>     loaded via a namespace (and not attached):
>     [1] findpython_1.0.1 getopt_1.20.0    rjson_0.2.13
>
>
I found the solution to this issue. Long story short: Use formatter_class=
'argparse.RawTextHelpFormatter'

See also my answer on StackOverflow
http://stackoverflow.com/questions/27150625/r-argparse-line-breaks-in-description/27375898#27375898

Dario

	[[alternative HTML version deleted]]



More information about the R-help mailing list