nargs = Inf is overridden by defaults

Issue #27 invalid
Former user created an issue

add_argument doesn't respect nargs whenever default is given.

This results in unexpected parsing failures, e.g.

library(argparser)
library(magrittr)

arg_parser("test") %>% add_argument("--arg", "test arg", 1L:3L, nargs = Inf) %>% parse_args(c("--arg", 1L:4L))

If we manually correct the error, it works just fine:

library(argparser)
library(magrittr)
library(purrr)

arg_parser("test") %>% add_argument("--arg", "test arg", 1L:3L, nargs = Inf) %>% modify_at("nargs", function(x){x[3] <- Inf; x}) %>% parse_args(c("--arg", 1L:4L))

The culprit is in add_argument:

nargs[!is.na(default)] <- unlist(lapply(default, length))

This line should be made more robust by only overwriting NAs. If a user specifies nargs and default, it should be his responsibility to have them match up (i.e. nargs == lengths(default) or nargs == Inf).

Comments (2)

  1. David Shih repo owner

    This works as intended. The length of the default takes precedence over specified nargs.

    The user should not be specifying incongruent arguments in the first place.

    If you don’t like this behaviour, write a proposal and an implementation, and I will review it. Otherwise, this behaviour will stay as it is.

  2. Log in to comment