[med-svn] [r-cran-optparse] 01/06: New upstream version 1.4.4

Andreas Tille tille at debian.org
Fri Sep 29 20:21:34 UTC 2017


This is an automated email from the git hooks/post-receive script.

tille pushed a commit to branch master
in repository r-cran-optparse.

commit 5fbbd0ef0a61db02a408e4ab9aa625c4e1af8875
Author: Andreas Tille <tille at debian.org>
Date:   Fri Sep 29 22:12:42 2017 +0200

    New upstream version 1.4.4
---
 DESCRIPTION                     |  11 +--
 MD5                             |  30 +++----
 NAMESPACE                       |   3 +-
 NEWS                            |  15 ++++
 R/optparse.R                    |  46 +++++++---
 build/vignette.rds              | Bin 221 -> 220 bytes
 inst/COPYRIGHTS                 |   4 +-
 inst/doc/optparse.html          | 188 ++++++++++++++++++++++++++++++++++++++++
 inst/doc/optparse.pdf           | Bin 7542 -> 0 bytes
 man/OptionParser-class.Rd       |  10 +--
 man/OptionParser.Rd             |   9 +-
 man/OptionParserOption-class.Rd |  11 ++-
 man/add_make_option.Rd          |  24 ++---
 man/optparse-package.Rd         |  23 ++---
 man/parse_args.Rd               |  55 +++++++-----
 man/print_help.Rd               |   9 +-
 tests/testthat/test-optparse.R  |  63 ++++++++++----
 17 files changed, 386 insertions(+), 115 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index b982038..5c25e6a 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -2,7 +2,7 @@ Encoding: UTF-8
 Package: optparse
 Type: Package
 Title: Command Line Option Parser
-Version: 1.3.2
+Version: 1.4.4
 Author: Trevor L Davis. Some documentation and examples ported from Allen Day's
     getopt package. Some documentation from the optparse Python module by the
     Python Software Foundation. Contributions from Steve Lianoglou, Jim
@@ -17,10 +17,11 @@ URL: https://github.com/trevorld/optparse
 BugReports: https://github.com/trevorld/optparse/issues
 LazyLoad: yes
 Depends: R (>= 2.9.0)
-Imports: methods, getopt (>= 1.19)
-Suggests: knitr, stringr, testthat
+Imports: methods, getopt (>= 1.19.0)
+Suggests: knitr (>= 1.15.19), stringr, testthat
 VignetteBuilder: knitr
+RoxygenNote: 6.0.1
 NeedsCompilation: no
-Packaged: 2015-10-02 21:55:38 UTC; trevorld
+Packaged: 2017-07-18 03:26:04 UTC; trevorld
 Repository: CRAN
-Date/Publication: 2015-10-03 07:58:03
+Date/Publication: 2017-07-18 22:24:26 UTC
diff --git a/MD5 b/MD5
index eaf7a1e..3b4e91b 100644
--- a/MD5
+++ b/MD5
@@ -1,23 +1,23 @@
 eb723b61539feef013de476e68b5c50a *COPYING
-b94608980e82cf5257cf307c9cfd4f05 *DESCRIPTION
-ea81ec72175d41efe9813c90f1bf8f26 *NAMESPACE
-0c5716f7e0380e35dc7b4b7225700ccb *NEWS
+ce81a796dfbf2278cf99842398085caf *DESCRIPTION
+749f23b64eeb7c0b390b46bf4b9fd05c *NAMESPACE
+1a5486daff51215c8c7fe8586e395d6d *NEWS
 52e3196bac3d6fa2c056308a33b6ed82 *R/optparse-package.R
-79b912edab13e48299e3d5ec51338af4 *R/optparse.R
-860ce8ee2100d9eb6ec28a80384bcdd9 *build/vignette.rds
+148887ec729b4414562dc7fa052f185b *R/optparse.R
+435b78f0d560935a5a096d8db11bad73 *build/vignette.rds
 1208f601f23f90cd50aeaf3834b0e6d2 *exec/display_file.R
 df7a5045be8d8227affc83c88cb21e79 *exec/example.R
-8bfdb662e7d8ebc1e24eeacde4e88f40 *inst/COPYRIGHTS
+e3b4ab464d3b774a7ba93b60c34cc716 *inst/COPYRIGHTS
 e8d2f193753efe3a0260ddb16cf68493 *inst/doc/optparse.R
 8a64473dff672d3f50622f46f76d56eb *inst/doc/optparse.Rrst
-66763e511962ad478b501283ab74989f *inst/doc/optparse.pdf
-ae4d5843a72a0c448479b6b2a1ceb03b *man/OptionParser-class.Rd
-f92ae22c2ad2d9d25858f8ba18720d8f *man/OptionParser.Rd
-8081c11b6b92b7a096ea41bb4f4679e5 *man/OptionParserOption-class.Rd
-607c9542df5c4f0c462ef36985e045a6 *man/add_make_option.Rd
-73c3da906e6844162fd642d809b57f77 *man/optparse-package.Rd
-e92fa42c81551e7696ab1a56c258805a *man/parse_args.Rd
-9edefcd9f24b63862a777fe273b075fc *man/print_help.Rd
+9d965bbc828a03c8697e3be9c9133a20 *inst/doc/optparse.html
+12daa156845d29d0eaee6879cd1f25e2 *man/OptionParser-class.Rd
+07a5161e2ca95285aff95a61565d19c4 *man/OptionParser.Rd
+0dcee7403c9dc84876743d69a5bb099c *man/OptionParserOption-class.Rd
+4807fdaa669b288f36bceec38615c9fd *man/add_make_option.Rd
+1e125ca48f272987c81387481c2e845b *man/optparse-package.Rd
+8885bdb1abe98033e3f1245eb1006989 *man/parse_args.Rd
+d31f07626001175444d44f070a5d2c12 *man/print_help.Rd
 398b710c16889ab67e30dbb3bad87137 *tests/run-all.R
-c20b18393b4c9c7e2147a145752ab784 *tests/testthat/test-optparse.R
+08a5305946fa1a1c403c779a7a64e9be *tests/testthat/test-optparse.R
 8a64473dff672d3f50622f46f76d56eb *vignettes/optparse.Rrst
diff --git a/NAMESPACE b/NAMESPACE
index 2401a30..d7d760e 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -1,10 +1,11 @@
-# Generated by roxygen2 (4.1.1): do not edit by hand
+# Generated by roxygen2: do not edit by hand
 
 export(OptionParser)
 export(OptionParserOption)
 export(add_option)
 export(make_option)
 export(parse_args)
+export(parse_args2)
 export(print_help)
 exportClasses(OptionParser)
 exportClasses(OptionParserOption)
diff --git a/NEWS b/NEWS
index b4ae4d4..b638ddf 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,18 @@
+optparse 1.4.4
+==============
+* Minor documentation fixes.  Thanks jjramsey and Daeyoung Kim for bug reports.
+
+optparse 1.4.1
+==============
+* Fix bug when ``add_help_option`` in ``OptionParser`` set to ``FALSE``.  Thanks to Jeff Bruce for bug report.
+
+optparse 1.4.0
+==============
+* parse_args now supports convert_hyphens_to_underscores argument which converts any hyphens to underscores 
+  when returning the list of options
+* Now includes the convenience function parse_args2 which wraps parse_args with positional_arguments set to TRUE
+  and convert_hyphens_to_underscores set to TRUE.
+
 optparse 1.3.1
 ==============
 * optparse should no longer give any warnings when options(warnPartialMatchArgs=TRUE).  Thanks Rich FitzJohn for patch.
diff --git a/R/optparse.R b/R/optparse.R
index 40f57e6..ab3555f 100644
--- a/R/optparse.R
+++ b/R/optparse.R
@@ -29,7 +29,7 @@
 
 #' Option Parser
 #' 
-#'  @slot usage The program usage message that will printed out if
+#' @slot usage The program usage message that will printed out if
 #'     \code{parse_args} finds a help option, \code{\%prog} is substituted with the
 #'     value of the \code{prog} argument.
 #' @slot options A list of of \code{OptionParserOption} instances that will
@@ -72,8 +72,7 @@ setClass("OptionParser", representation(usage = "character", options = "list",
 #'     by \code{parse_args} should \code{optparse} store option values.  Default is
 #'     derived from the long flag in \code{opt_str}.
 #' @slot default The default value \code{optparse} should use if it does not
-#'     find the option on the command line.  Default is derived from the long flag
-#'     in \code{opt_str}.
+#'     find the option on the command line.  
 #' @slot help A character string describing the option to be used by
 #'     \code{print_help} in generating a usage message.  \code{\%default} will be
 #'     substituted by the value of \code{default}.
@@ -183,8 +182,7 @@ OptionParser <- function(usage = "usage: %prog [options]", option_list=list(),
 #'     by \code{parse_args} should \code{optparse} store option values.  Default is
 #'     derived from the long flag in \code{opt_str}.
 #' @param default The default value \code{optparse} should use if it does not
-#'     find the option on the command line.  Default is derived from the long flag
-#'     in \code{opt_str}.
+#'     find the option on the command line.  
 #' @param help A character string describing the option to be used by
 #'     \code{print_help} in generating a usage message.  \code{\%default} will be
 #'     substituted by the value of \code{default}.
@@ -338,7 +336,9 @@ print_help <- function(object) {
 #' Parse command line options.
 #' 
 #' \code{parse_args} parses command line options using an \code{OptionParser}
-#' instance for guidance.
+#' instance for guidance. \code{parse_args2} is a wrapper to \code{parse_args}
+#' setting the options \code{positional_arguments} and \code{convert_hyphens_to_underscores}
+#' to \code{TRUE}.
 #' 
 #' @param object An \code{OptionParser} instance.
 #' @param args A character vector containing command line options to be parsed.
@@ -355,6 +355,9 @@ print_help <- function(object) {
 #'     \code{c(0, Inf)}.  The default \code{FALSE} is
 #'     supported for backward compatibility only, as it alters
 #'     the format of the return value.
+#' @param convert_hyphens_to_underscores If the names in the returned list of options
+#'      contains hyphens then convert them to underscores.  The default \code{FALSE} is 
+#'      supported for backward compatibility reasons as it alters the format of the return value
 #' @return Returns a list with field \code{options} containing our option values
 #'     as well as another field \code{args} which contains a vector of
 #'     positional arguments.  For backward compatibility, if and only if
@@ -395,20 +398,24 @@ print_help <- function(object) {
 #'
 #'# example from vignette using positional arguments
 #'option_list2 <- list( 
-#'    make_option(c("-n", "--add_numbers"), action="store_true", default=FALSE,
+#'    make_option(c("-n", "--add-numbers"), action="store_true", default=FALSE,
 #'        help="Print line number at the beginning of each line [default]")
 #'    )
 #'parser <- OptionParser(usage = "%prog [options] file", option_list=option_list2)
 #'
-#'parse_args(parser, args = c("--add_numbers", "example.txt"), positional_arguments = TRUE)
+#'parse_args(parser, args = c("--add-numbers", "example.txt"), positional_arguments = TRUE)
 #'
-#'parse_args(parser, args = c("-add_numbers", "example.txt"), positional_arguments = TRUE)
+#'parse_args(parser, args = c("--add-numbers", "example.txt"), positional_arguments = TRUE,
+#'          convert_hyphens_to_underscores = TRUE)
+#'
+#'parse_args2(parser, args = c("--add-numbers", "example.txt"))
 #'
 #' @import getopt
 #' @importFrom utils tail
 #' @export 
 parse_args <- function(object, args = commandArgs(trailingOnly = TRUE), 
-                    print_help_and_exit = TRUE, positional_arguments = FALSE) {
+                    print_help_and_exit = TRUE, positional_arguments = FALSE,
+                    convert_hyphens_to_underscores = FALSE) {
 
     n_options <- length( object at options )
 
@@ -479,9 +486,14 @@ parse_args <- function(object, args = commandArgs(trailingOnly = TRUE),
             }
         }
     }
-    if(options_list[["help"]] & print_help_and_exit) {
-        print_help(object)
-        if(interactive()) stop("help requested") else quit(status=1) 
+    if (convert_hyphens_to_underscores) {
+        names(options_list) <- gsub("-", "_", names(options_list))
+    }
+    if (any(grepl("^help$", names(options_list)))) {
+        if(options_list[["help"]] & print_help_and_exit) {
+            print_help(object)
+            if(interactive()) stop("help requested") else quit(status=1) 
+        }
     }
     if (length(arguments_positional) < min(positional_arguments)) {
       stop(sprintf("required at least %g positional arguments, got %g",
@@ -497,6 +509,13 @@ parse_args <- function(object, args = commandArgs(trailingOnly = TRUE),
         return(options_list)
     }
 }
+#' @rdname parse_args
+#' @export
+parse_args2 <- function(object, args = commandArgs(trailingOnly = TRUE),
+                        print_help_and_exit = TRUE) {
+    parse_args(object, args = args, print_help_and_exit = print_help_and_exit,
+               positional_arguments = TRUE, convert_hyphens_to_underscores = TRUE)
+}
 
 # Tells me whether a string is a valid option
 .is_option_string <- function(argument, object) {
@@ -591,3 +610,4 @@ parse_args <- function(object, args = commandArgs(trailingOnly = TRUE),
     }
     return( c( long_flag, short_flag, argument, object at type, object at help) )
 }
+
diff --git a/build/vignette.rds b/build/vignette.rds
index 116e23e..8129921 100644
Binary files a/build/vignette.rds and b/build/vignette.rds differ
diff --git a/inst/COPYRIGHTS b/inst/COPYRIGHTS
index e79d0f4..050420d 100644
--- a/inst/COPYRIGHTS
+++ b/inst/COPYRIGHTS
@@ -1,7 +1,7 @@
 Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
 
 Files: *
-Copyright: 2010-2015 Trevor L Davis 
+Copyright: 2010-2017 Trevor L Davis 
 License: GPL-2+
 
 File: exec/example.R
@@ -32,7 +32,7 @@ License: GPL-2+
 
 File: R/optparse.R 
 Copyright: 1990-2009 Python Software Foundation
-           2011-2014 Trevor L Davis 
+           2011-2017 Trevor L Davis 
            2010 Steve Lianoglou
            2011 Jim Nikelski
            2013 Kirill Müller
diff --git a/inst/doc/optparse.html b/inst/doc/optparse.html
new file mode 100644
index 0000000..46e673c
--- /dev/null
+++ b/inst/doc/optparse.html
@@ -0,0 +1,188 @@
+<h1 id="optparse-command-line-option-parsing">optparse Command Line Option Parsing</h1>
+<p>optparse is a command line option parser inspired by Python's "optparse" library. Use this with Rscript to write "#!"-shebang scripts that accept short and long flags/options, generate a usage statement, and set default values for options that are not specified on the command line.</p>
+<p>In our working directory we have two example R scripts, named "example.R" and "display_file.R" illustrating the use of the optparse package.</p>
+<p><strong>bash$ ls</strong></p>
+<pre><code>display_file.R
+example.R</code></pre>
+<p>In order for a *nix system to recognize a "#!"-shebang line you need to mark the file executable with the <code>chmod</code> command, it also helps to add the directory containing your Rscripts to your path:</p>
+<p><strong>bash$ chmod ug+x display_file.R example.R</strong></p>
+<p><strong>bash$ export PATH=$PATH:pwd</strong></p>
+<p>Here is what <code>example.R</code> contains:</p>
+<p><strong>bash$ display_file.R example.R</strong></p>
+<pre><code>#!/usr/bin/env Rscript
+# Copyright 2010-2013 Trevor L Davis <trevor.l.davis at stanford.edu>
+# Copyright 2008 Allen Day
+#  
+#  This file is free software: you may copy, redistribute and/or modify it  
+#  under the terms of the GNU General Public License as published by the  
+#  Free Software Foundation, either version 2 of the License, or (at your  
+#  option) any later version.  
+#  
+#  This file is distributed in the hope that it will be useful, but  
+#  WITHOUT ANY WARRANTY; without even the implied warranty of  
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+#  General Public License for more details.  
+#  
+#  You should have received a copy of the GNU General Public License  
+#  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+suppressPackageStartupMessages(library("optparse"))
+suppressPackageStartupMessages(library("stats"))
+
+# specify our desired options in a list
+# by default OptionParser will add an help option equivalent to 
+# make_option(c("-h", "--help"), action="store_true", default=FALSE, 
+#               help="Show this help message and exit")
+option_list <- list( 
+    make_option(c("-v", "--verbose"), action="store_true", default=TRUE,
+        help="Print extra output [default]"),
+    make_option(c("-q", "--quietly"), action="store_false", 
+        dest="verbose", help="Print little output"),
+    make_option(c("-c", "--count"), type="integer", default=5, 
+        help="Number of random normals to generate [default %default]",
+        metavar="number"),
+    make_option("--generator", default="rnorm", 
+        help = "Function to generate random deviates [default \"%default\"]"),
+    make_option("--mean", default=0, 
+        help="Mean if generator == \"rnorm\" [default %default]"),
+    make_option("--sd", default=1, metavar="standard deviation",
+        help="Standard deviation if generator == \"rnorm\" [default %default]")
+    )
+
+# get command line options, if help option encountered print help and exit,
+# otherwise if options not found on command line then set defaults, 
+opt <- parse_args(OptionParser(option_list=option_list))
+
+# print some progress messages to stderr if "quietly" wasn't requested
+if ( opt$verbose ) { 
+    write("writing some verbose output to standard error...\n", stderr()) 
+}
+
+# do some operations based on user input
+if( opt$generator == "rnorm") {
+    cat(paste(rnorm(opt$count, mean=opt$mean, sd=opt$sd), collapse="\n"))
+} else {
+    cat(paste(do.call(opt$generator, list(opt$count)), collapse="\n"))
+}
+cat("\n")</code></pre>
+<p>By default <em>optparse</em> will generate a help message if it encounters <code>--help</code> or <code>-h</code> on the command line. Note how <code>%default</code> in the example program was replaced by the actual default values in the help statement that <em>optparse</em> generated.</p>
+<p><strong>bash$ example.R --help</strong></p>
+<pre><code>Usage: example.R [options]
+
+
+Options:
+    -v, --verbose
+        Print extra output [default]
+
+    -q, --quietly
+        Print little output
+
+    -c NUMBER, --count=NUMBER
+        Number of random normals to generate [default 5]
+
+    --generator=GENERATOR
+        Function to generate random deviates [default "rnorm"]
+
+    --mean=MEAN
+        Mean if generator == "rnorm" [default 0]
+
+    --sd=STANDARD DEVIATION
+        Standard deviation if generator == "rnorm" [default 1]
+
+    -h, --help
+        Show this help message and exit</code></pre>
+<p>If you specify default values when creating your <code>OptionParser</code> then <em>optparse</em> will use them as expected.</p>
+<p><strong>bash$ example.R</strong></p>
+<pre><code>writing some verbose output to standard error...
+
+1.49626496576745
+-0.430133968033571
+0.412183647327778
+-0.0446135231106203
+0.0332484143153748</code></pre>
+<p>Or you can specify your own values.</p>
+<p><strong>bash$ example.R --mean=10 --sd=10 --count=3</strong></p>
+<pre><code>writing some verbose output to standard error...
+
+11.6458476431069
+5.72555072552593
+11.2994544799871</code></pre>
+<p>If you remember from the example program that <code>--quiet</code> had <code>action="store_false"</code> and <code>dest="verbose"</code>. This means that <code>--quiet</code> is a switch that turns the <code>verbose</code> option from its default value of <code>TRUE</code> to <code>FALSE</code>. Note how the <code>verbose</code> and <code>quiet</code> options store their value in the exact same variable.</p>
+<p><strong>bash$ example.R --quiet -c 4 --generator="runif"</strong></p>
+<pre><code>0.0652185005601496
+0.263430899009109
+0.397433762671426
+0.582770691253245</code></pre>
+<p>If you specify an illegal flag then <em>optparse</em> will throw an error.</p>
+<p><strong>bash$ example.R --silent -m 5</strong></p>
+<pre><code>Error in getopt(spec = spec, opt = args) : long flag "silent" is invalid
+Calls: parse_args -> getopt
+Execution halted</code></pre>
+<p>If you specify the same option multiple times then <em>optparse</em> will use the value of the last option specified.</p>
+<p><strong>bash$ example.R -c 100 -c 2 -c 1000 -c 7</strong></p>
+<pre><code>writing some verbose output to standard error...
+
+-0.0502636939925738
+-1.20953394405362
+1.46327954003337
+1.43860488309639
+1.26968788684086
+1.51600057753648
+-0.763034718503095</code></pre>
+<p><em>optparse</em> can also recognize positional arguments if <code>parse_args</code> is given the option <code>positional_arguments = c(min_pa, max_pa)</code> where <code>min_pa</code> is the minimum and <code>max_pa</code> is the maximum number of supported positional arguments. (A single numeric corresponds to <code>min_pa == max_pa</code>, <code>TRUE</code> is equivalent to <code>c(0, Inf)</code>, and <code>FALSE</code>, the default, is equivalent to <code>0</code>.) Below we give  [...]
+<p><strong>bash$ display_file.R --help</strong></p>
+<pre><code>Usage: display_file.R [options] file
+
+
+Options:
+    -n, --add_numbers
+        Print line number at the beginning of each line [default]
+
+    -h, --help
+        Show this help message and exit</code></pre>
+<p><strong>bash$ display_file.R --add_numbers display_file.R</strong></p>
+<pre><code>1 #!/usr/bin/env Rscript
+2 # Copyright 2010-2013 Trevor L Davis <trevor.l.davis at stanford.edu>
+3 # Copyright 2013 Kirill Müller 
+4 #  
+5 #  This file is free software: you may copy, redistribute and/or modify it  
+6 #  under the terms of the GNU General Public License as published by the  
+7 #  Free Software Foundation, either version 2 of the License, or (at your  
+8 #  option) any later version.  
+9 #  
+10 #  This file is distributed in the hope that it will be useful, but  
+11 #  WITHOUT ANY WARRANTY; without even the implied warranty of  
+12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
+13 #  General Public License for more details.  
+14 #  
+15 #  You should have received a copy of the GNU General Public License  
+16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+17 suppressPackageStartupMessages(library("optparse"))
+18 
+19 option_list <- list( 
+20     make_option(c("-n", "--add_numbers"), action="store_true", default=FALSE,
+21         help="Print line number at the beginning of each line [default]")
+22     )
+23 parser <- OptionParser(usage = "%prog [options] file", option_list=option_list)
+24 
+25 arguments <- parse_args(parser, positional_arguments = 1)
+26 opt <- arguments$options
+27 file <- arguments$args
+28 
+29 if( file.access(file) == -1) {
+30     stop(sprintf("Specified file ( %s ) does not exist", file))
+31 } else {
+32     file_text <- readLines(file)
+33 }
+34 
+35 if(opt$add_numbers) {
+36     cat(paste(1:length(file_text), file_text), sep = "\n")
+37 } else {
+38     cat(file_text, sep = "\n")
+39 }</code></pre>
+<p><strong>bash$ display_file.R non_existent_file.txt</strong></p>
+<pre><code>Error: Specified file ( non_existent_file.txt ) does not exist
+Execution halted</code></pre>
+<p><strong>bash$ display_file.R</strong></p>
+<pre><code>Error in parse_args(parser, positional_arguments = 1) : 
+  required at least 1 positional arguments, got 0
+Execution halted</code></pre>
diff --git a/inst/doc/optparse.pdf b/inst/doc/optparse.pdf
deleted file mode 100644
index 01961f4..0000000
Binary files a/inst/doc/optparse.pdf and /dev/null differ
diff --git a/man/OptionParser-class.Rd b/man/OptionParser-class.Rd
index 625701d..fc1db44 100644
--- a/man/OptionParser-class.Rd
+++ b/man/OptionParser-class.Rd
@@ -1,4 +1,4 @@
-% Generated by roxygen2 (4.1.1): do not edit by hand
+% Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/optparse.R
 \docType{class}
 \name{OptionParser-class}
@@ -26,10 +26,10 @@ usage statement and options statement}
 \item{\code{epilogue}}{Additional text for \code{print_help} to print out after
 the options statement}
 }}
-\author{
-Trevor Davis.
-}
+
 \seealso{
 \code{\link{OptionParserOption}}
 }
-
+\author{
+Trevor Davis.
+}
diff --git a/man/OptionParser.Rd b/man/OptionParser.Rd
index 68a940b..30434aa 100644
--- a/man/OptionParser.Rd
+++ b/man/OptionParser.Rd
@@ -1,4 +1,4 @@
-% Generated by roxygen2 (4.1.1): do not edit by hand
+% Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/optparse.R
 \name{OptionParser}
 \alias{OptionParser}
@@ -40,9 +40,6 @@ This function is used to create an instance of a parser object
 which when combined with the \code{parse_args}, \code{make_option}, and \code{add_option}
 methods is very useful for parsing options from the command line.
 }
-\author{
-Trevor Davis.
-}
 \references{
 Python's \code{optparse} library, which inspired this package,
    is described here: \url{http://docs.python.org/library/optparse.html}
@@ -51,4 +48,6 @@ Python's \code{optparse} library, which inspired this package,
 \code{\link{parse_args}} \code{\link{make_option}}
     \code{\link{add_option}}
 }
-
+\author{
+Trevor Davis.
+}
diff --git a/man/OptionParserOption-class.Rd b/man/OptionParserOption-class.Rd
index 4563aa7..9ef99a8 100644
--- a/man/OptionParserOption-class.Rd
+++ b/man/OptionParserOption-class.Rd
@@ -1,9 +1,9 @@
-% Generated by roxygen2 (4.1.1): do not edit by hand
+% Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/optparse.R
 \docType{class}
 \name{OptionParserOption-class}
-\alias{OptionParserOption}
 \alias{OptionParserOption-class}
+\alias{OptionParserOption}
 \title{Class to hold information about command-line options}
 \description{
 Class to hold information about command-line options
@@ -14,7 +14,7 @@ Class to hold information about command-line options
 \item{\code{short_flag}}{String of the desired short flag
 comprised of the \dQuote{-} followed by a letter.}
 
-\item{\code{long_flag}}{String of the desired long flag comprised of \dQuote{--}
+\item{\code{long_flag}}{String of the desired long flag comprised of \dQuote{--} 
 followed by a letter and then a sequence of alphanumeric characters.}
 
 \item{\code{action}}{A character string that describes the action \code{optparse}
@@ -38,8 +38,7 @@ by \code{parse_args} should \code{optparse} store option values.  Default is
 derived from the long flag in \code{opt_str}.}
 
 \item{\code{default}}{The default value \code{optparse} should use if it does not
-find the option on the command line.  Default is derived from the long flag
-in \code{opt_str}.}
+find the option on the command line.}
 
 \item{\code{help}}{A character string describing the option to be used by
 \code{print_help} in generating a usage message.  \code{\%default} will be
@@ -48,7 +47,7 @@ substituted by the value of \code{default}.}
 \item{\code{metavar}}{A character string that stands in for the option argument when
 printing help text.  Default is the value of \code{dest}.}
 }}
+
 \seealso{
 \code{\link{make_option}}
 }
-
diff --git a/man/add_make_option.Rd b/man/add_make_option.Rd
index 7fde6fc..9bc30ca 100644
--- a/man/add_make_option.Rd
+++ b/man/add_make_option.Rd
@@ -1,8 +1,8 @@
-% Generated by roxygen2 (4.1.1): do not edit by hand
+% Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/optparse.R
 \name{make_option}
-\alias{add_option}
 \alias{make_option}
+\alias{add_option}
 \title{Functions to enable our OptionParser to recognize specific command line
 options.}
 \usage{
@@ -39,8 +39,7 @@ by \code{parse_args} should \code{optparse} store option values.  Default is
 derived from the long flag in \code{opt_str}.}
 
 \item{default}{The default value \code{optparse} should use if it does not
-find the option on the command line.  Default is derived from the long flag
-in \code{opt_str}.}
+find the option on the command line.}
 
 \item{help}{A character string describing the option to be used by
 \code{print_help} in generating a usage message.  \code{\%default} will be
@@ -63,7 +62,8 @@ whereas \code{make_option} is used to create a list of
 new \code{OptionParser} instance.
 }
 \examples{
-make_option("--longflag")
+
+   make_option("--longflag")
    make_option(c("-l", "--longflag"))
    make_option("--integer", type="integer", default=5)
    make_option("--integer", default=as.integer(5))  # same as previous
@@ -73,18 +73,16 @@ make_option("--longflag")
        help="Print extra output [default]")
    make_option(c("-q", "--quietly"), action="store_false",
      dest="verbose", help="Print little output")
-   make_option(c("-c", "--count"), type="integer", default=5,
+   make_option(c("-c", "--count"), type="integer", default=5, 
        help="Number of random normals to generate [default \%default]",
        metavar="number")
-   make_option("--generator", default="rnorm",
+   make_option("--generator", default="rnorm", 
        help = "Function to generate random deviates [default \\"\%default\\"]")
-   make_option("--mean", default=0,
+   make_option("--mean", default=0, 
        help="Mean if generator == \\"rnorm\\" [default \%default]")
    make_option("--sd", default=1, metavar="standard deviation",
        help="Standard deviation if generator == \\"rnorm\\" [default \%default]")
-}
-\author{
-Trevor Davis.
+
 }
 \references{
 Python's \code{optparse} library, which inspires this package,
@@ -93,4 +91,6 @@ Python's \code{optparse} library, which inspires this package,
 \seealso{
 \code{\link{parse_args}} \code{\link{OptionParser}}
 }
-
+\author{
+Trevor Davis.
+}
diff --git a/man/optparse-package.Rd b/man/optparse-package.Rd
index 472d2b3..d3da4ae 100644
--- a/man/optparse-package.Rd
+++ b/man/optparse-package.Rd
@@ -1,9 +1,9 @@
-% Generated by roxygen2 (4.1.1): do not edit by hand
+% Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/optparse-package.R
 \docType{package}
 \name{optparse-package}
-\alias{optparse}
 \alias{optparse-package}
+\alias{optparse}
 \title{Command line option parser}
 \description{
 Goal is to create an R package of a command line parser inspired by Python's
@@ -30,20 +30,14 @@ together, sharing a single leading \dQuote{"-"}, but only the final short
 flag is able to have a corresponding argument. %%%
 }
 \examples{
-example_file <- system.file("exec", "example.R", package = "optparse")
+
+   example_file <- system.file("exec", "example.R", package = "optparse")
    example_file_2 <- system.file("exec", "display_file.R", package = "optparse")
    \dontrun{
        readLines(example_file)
        readLines(example_file_2)
    }
-}
-\author{
-Trevor Davis.
-
-Some documentation and unit tests ported from Allen Day's getopt package.
 
-The documentation for Python's optparse library, which this package is based
-on, is Copyright 1990-2009, Python Software Foundation.
 }
 \references{
 Python's \code{optparse} library, which this package is based on,
@@ -52,5 +46,12 @@ is described here: \url{http://docs.python.org/library/optparse.html}
 \seealso{
 \code{\link[getopt]{getopt}}
 }
-\keyword{package}
+\author{
+Trevor Davis.
+
+Some documentation and unit tests ported from Allen Day's getopt package.
 
+The documentation for Python's optparse library, which this package is based
+on, is Copyright 1990-2009, Python Software Foundation.
+}
+\keyword{package}
diff --git a/man/parse_args.Rd b/man/parse_args.Rd
index a9bd676..24c09b2 100644
--- a/man/parse_args.Rd
+++ b/man/parse_args.Rd
@@ -1,19 +1,24 @@
-% Generated by roxygen2 (4.1.1): do not edit by hand
+% Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/optparse.R
 \encoding{latin1}
 \name{parse_args}
 \alias{parse_args}
+\alias{parse_args2}
 \title{Parse command line options.}
 \usage{
 parse_args(object, args = commandArgs(trailingOnly = TRUE),
-  print_help_and_exit = TRUE, positional_arguments = FALSE)
+  print_help_and_exit = TRUE, positional_arguments = FALSE,
+  convert_hyphens_to_underscores = FALSE)
+
+parse_args2(object, args = commandArgs(trailingOnly = TRUE),
+  print_help_and_exit = TRUE)
 }
 \arguments{
 \item{object}{An \code{OptionParser} instance.}
 
 \item{args}{A character vector containing command line options to be parsed.
 Default is everything after the Rscript program in the command line. If
-\code{positional_arguments} is not \code{FALSE} then \code{parse_args} will
+\code{positional_arguments} is not \code{FALSE} then \code{parse_args} will 
 look for positional arguments at the end of this vector.}
 
 \item{print_help_and_exit}{Whether \code{parse_args} should call
@@ -27,6 +32,10 @@ length two denoting the minimum and maximum number of arguments
 \code{c(0, Inf)}.  The default \code{FALSE} is
 supported for backward compatibility only, as it alters
 the format of the return value.}
+
+\item{convert_hyphens_to_underscores}{If the names in the returned list of options
+contains hyphens then convert them to underscores.  The default \code{FALSE} is 
+supported for backward compatibility reasons as it alters the format of the return value}
 }
 \value{
 Returns a list with field \code{options} containing our option values
@@ -37,31 +46,34 @@ Returns a list with field \code{options} containing our option values
 }
 \description{
 \code{parse_args} parses command line options using an \code{OptionParser}
-instance for guidance.
+instance for guidance. \code{parse_args2} is a wrapper to \code{parse_args}
+setting the options \code{positional_arguments} and \code{convert_hyphens_to_underscores}
+to \code{TRUE}.
 }
 \section{Acknowledgement}{
-
+ 
     A big thanks to Steve Lianoglou for a bug report and patch;
-    Juan Carlos \enc{Borrás}{Borras} for a bug report;
-    Jim Nikelski for a bug report and patch;
+    Juan Carlos \enc{Borrás}{Borras} for a bug report; 
+    Jim Nikelski for a bug report and patch; 
     Ino de Brujin and Benjamin Tyner for a bug report;
     Jonas Zimmermann for bug report; Miroslav Posta for bug reports;
     Stefan Seemayer for bug report and patch;
     Kirill \enc{Müller}{Muller} for patches; Steve Humburg for patch.
 }
+
 \examples{
 # example from vignette
-option_list <- list(
+option_list <- list( 
    make_option(c("-v", "--verbose"), action="store_true", default=TRUE,
        help="Print extra output [default]"),
-   make_option(c("-q", "--quietly"), action="store_false",
+   make_option(c("-q", "--quietly"), action="store_false", 
        dest="verbose", help="Print little output"),
-   make_option(c("-c", "--count"), type="integer", default=5,
+   make_option(c("-c", "--count"), type="integer", default=5, 
        help="Number of random normals to generate [default \%default]",
        metavar="number"),
-   make_option("--generator", default="rnorm",
+   make_option("--generator", default="rnorm", 
        help = "Function to generate random deviates [default \\"\%default\\"]"),
-   make_option("--mean", default=0,
+   make_option("--mean", default=0, 
        help="Mean if generator == \\"rnorm\\" [default \%default]"),
    make_option("--sd", default=1, metavar="standard deviation",
        help="Standard deviation if generator == \\"rnorm\\" [default \%default]")
@@ -69,18 +81,19 @@ option_list <- list(
 parse_args(OptionParser(option_list = option_list), args = c("--sd=3", "--quietly"))
 
 # example from vignette using positional arguments
-option_list2 <- list(
-   make_option(c("-n", "--add_numbers"), action="store_true", default=FALSE,
+option_list2 <- list( 
+   make_option(c("-n", "--add-numbers"), action="store_true", default=FALSE,
        help="Print line number at the beginning of each line [default]")
    )
 parser <- OptionParser(usage = "\%prog [options] file", option_list=option_list2)
 
-parse_args(parser, args = c("--add_numbers", "example.txt"), positional_arguments = TRUE)
+parse_args(parser, args = c("--add-numbers", "example.txt"), positional_arguments = TRUE)
+
+parse_args(parser, args = c("--add-numbers", "example.txt"), positional_arguments = TRUE,
+         convert_hyphens_to_underscores = TRUE)
+
+parse_args2(parser, args = c("--add-numbers", "example.txt"))
 
-parse_args(parser, args = c("-add_numbers", "example.txt"), positional_arguments = TRUE)
-}
-\author{
-Trevor Davis.
 }
 \references{
 Python's \code{optparse} library, which inspired this package,
@@ -89,4 +102,6 @@ Python's \code{optparse} library, which inspired this package,
 \seealso{
 \code{\link{OptionParser}} \code{\link{print_help}}
 }
-
+\author{
+Trevor Davis.
+}
diff --git a/man/print_help.Rd b/man/print_help.Rd
index 514408e..239a395 100644
--- a/man/print_help.Rd
+++ b/man/print_help.Rd
@@ -1,4 +1,4 @@
-% Generated by roxygen2 (4.1.1): do not edit by hand
+% Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/optparse.R
 \name{print_help}
 \alias{print_help}
@@ -17,9 +17,6 @@ message.  It returns \code{invisible(NULL)}.
 \code{print_help} print an usage message from an OptionParser object, usually
 called by \code{parse_args} when it encounters a help option.
 }
-\author{
-Trevor Davis.
-}
 \references{
 Python's \code{optparse} library, which inspired this package,
     is described here: \url{http://docs.python.org/library/optparse.html}
@@ -27,4 +24,6 @@ Python's \code{optparse} library, which inspired this package,
 \seealso{
 \code{{parse_args}} \code{{OptionParser}}
 }
-
+\author{
+Trevor Davis.
+}
diff --git a/tests/testthat/test-optparse.R b/tests/testthat/test-optparse.R
index 87c81de..81dcfa6 100644
--- a/tests/testthat/test-optparse.R
+++ b/tests/testthat/test-optparse.R
@@ -38,14 +38,27 @@ test_that("make_option works as expected", {
                 make_option("--integer", default=as.integer(5)))
     expect_equal(make_option("--logical", type="logical", default="TRUE"),
                 make_option("--logical", default=TRUE))
+    expect_equal(make_option("--filename")@type, "character")
     expect_that(make_option("badflag"), throws_error())
 })
 
+get_long_flags <- function(parser) {
+    sort(sapply(parser at options, function(x) { x at long_flag }))
+}
+context("Test add_option")
+test_that("add_option works as expected", {
+    parser1 <- OptionParser(option_list = list(make_option("--generator"), make_option("--count")))
+    parser2 <- OptionParser()
+    parser2 <- add_option(parser2, "--generator")
+    parser2 <- add_option(parser2, "--count")
+    expect_equal(get_long_flags(parser1), get_long_flags(parser2))
+})
+
 context("Testing parse_args")
 test_that("parse_args works as expected", {
     # option_list took outside test_that
     option_list2 <- list( 
-        make_option(c("-n", "--add_numbers"), action="store_true", default=FALSE,
+        make_option(c("-n", "--add-numbers"), action="store_true", default=FALSE,
             help="Print line number at the beginning of each line [default]")
         )
     parser <- OptionParser(usage = "\\%prog [options] file", option_list=option_list2)
@@ -62,32 +75,39 @@ test_that("parse_args works as expected", {
                             args = c("-c", "10"))),
                 sort_list(list(sd = 1, help = FALSE, verbose = TRUE, 
                             count = 10, mean = 0, generator = "rnorm")))
-    expect_equal(sort_list(parse_args(parser, args = c("--add_numbers", "example.txt"), 
+    expect_equal(sort_list(parse_args(parser, args = c("--add-numbers", "example.txt"), 
                             positional_arguments = TRUE)),
-                sort_list(list(options = list(add_numbers = TRUE, help = FALSE), 
+                sort_list(list(options = list(`add-numbers` = TRUE, help = FALSE), 
                              args = c("example.txt"))))
-    expect_equal(sort_list(parse_args(parser, args = c("--add_numbers"), 
+    expect_equal(sort_list(parse_args(parser, args = c("--add-numbers"), 
                             positional_arguments = TRUE)),
+                sort_list(list(options = list(`add-numbers` = TRUE, help = FALSE), 
+                             args = character(0))))
+    expect_equal(sort_list(parse_args(parser, args = c("--add-numbers"), 
+                            positional_arguments = TRUE, convert_hyphens_to_underscores = TRUE)),
+                sort_list(list(options = list(add_numbers = TRUE, help = FALSE), 
+                             args = character(0))))
+    expect_equal(sort_list(parse_args2(parser, args = c("--add-numbers"))),
                 sort_list(list(options = list(add_numbers = TRUE, help = FALSE), 
                              args = character(0))))
-    expect_equal(sort_list(parse_args(parser, args = c("-add_numbers", "example.txt"), 
+    expect_equal(sort_list(parse_args(parser, args = c("-add-numbers", "example.txt"), 
                                 positional_arguments = TRUE)),
-                sort_list(list(options = list(add_numbers = FALSE, help = FALSE), 
-                             args = c("-add_numbers", "example.txt"))))
-    expect_that(parse_args(parser, args = c("-add_numbers", "example.txt")), throws_error())
-    expect_equal(sort_list(parse_args(parser, args = c("-add_numbers", "example.txt"),
+                sort_list(list(options = list(`add-numbers` = FALSE, help = FALSE), 
+                             args = c("-add-numbers", "example.txt"))))
+    expect_that(parse_args(parser, args = c("-add-numbers", "example.txt")), throws_error())
+    expect_equal(sort_list(parse_args(parser, args = c("-add-numbers", "example.txt"),
                                       positional_arguments = c(1,3))),
-                 sort_list(list(options = list(add_numbers = FALSE, help = FALSE),
-                                args = c("-add_numbers", "example.txt"))))
-    expect_equal(sort_list(parse_args(parser, args = c("--add_numbers", "example.txt"),
+                 sort_list(list(options = list(`add-numbers` = FALSE, help = FALSE),
+                                args = c("-add-numbers", "example.txt"))))
+    expect_equal(sort_list(parse_args(parser, args = c("--add-numbers", "example.txt"),
                                       positional_arguments = c(1,3))),
-                 sort_list(list(options = list(add_numbers = TRUE, help = FALSE),
+                 sort_list(list(options = list(`add-numbers` = TRUE, help = FALSE),
                                 args = c("example.txt"))))
     expect_equal(sort_list(parse_args(parser, args = c("example.txt"),
                                       positional_arguments = 1)),
-                 sort_list(list(options = list(add_numbers = FALSE, help = FALSE),
+                 sort_list(list(options = list(`add-numbers` = FALSE, help = FALSE),
                                 args = c("example.txt"))))
-    expect_that(parse_args(parser, args = c("-add_numbers", "example.txt"),
+    expect_that(parse_args(parser, args = c("-add-numbers", "example.txt"),
                            positional_arguments=c(0, 1)), throws_error())
     expect_that(parse_args(parser, args = c("example.txt"),
                            positional_arguments=c(2, Inf)), throws_error())
@@ -242,3 +262,16 @@ test_that("Can set zero length default options", {
 #     expect_that(print_help(OptionParser()), not(gives_warning()))
 # })
 # 
+
+# Use h flag for non-help (Reported by Jeff Bruce)
+context("Use h option for non-help")
+test_that("Use h option for non-help", {
+    option_list_neg <- list( make_option(c("-h", "--mean"), default=0.0) )
+    parser <- OptionParser(usage = "\\%prog [options] file", option_list=option_list_neg)
+    expect_that(parse_args(parser, args = c("-h", "-5.0")), throws_error()) 
+
+    option_list_neg <- list( make_option(c("-h", "--mean"), default=0.0) )
+    parser <- OptionParser(usage = "\\%prog [options] file", option_list=option_list_neg, add_help_option=FALSE)
+    args <- parse_args(parser, args = c("-h", "-5.0"))
+    expect_equal(args, list(mean = -5.0)) 
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/r-cran-optparse.git



More information about the debian-med-commit mailing list