Error: length(arg) == length(default) is not TRUE when adding multiple args at once

Issue #6 resolved
vtd mhd created an issue

In this MWE the lengths are definitely same

#!/usr/bin/env Rscript

library('argparser')
p <- arg_parser("desc")
argmt <- c('--mode', '--debug', '--cores')
add_argument(p, argmt, help=argmt, default=argmt )

R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree" Platform: x86_64-pc-linux-gnu (64-bit)

package v. 0.3 from CRAN

Comments (4)

  1. David Shih repo owner

    As per the help page:

    Details:
    
         This function supports multiple arguments in a vector. To ensure
         that the argument variable type is set correctly, either specify
         type directly or supply default argument values as a list.
    

    Therefore, you need to supply default as a list. Otherwise, all the values in default will be casted to the most permissive variable type.

    This works as intended:

    add_argument(p, argmt, help=argmt, default=as.list(argmt) )
    

    Note that in real world usage, you will not want to store argmt as a vector and cast it to a list, as the value types will already have been converted inappropriately. Instead, you would want to create the list directly so that the types are preserved.

    Vectors, unlike lists, can only contain elements of a single type.

    a <- c("yes", 1, 3, 3.4)
    b <- list("yes", 1, 3, 3.4)
    

    a is converted a character vector, whereas b preserves the types of the elements.

  2. Log in to comment