Add option to specify arguments are required

Issue #20 closed
Former user created an issue

Maybe something like required=TRUE

Comments (9)

  1. Jiří Moravec

    I find myself always wanting this feature. With many positional arguments, it becomes a bit chaotic as their order is important and for scripts used in pipeline and in makefiles, it is much clearer what is being specified if --parameter arg1 is used instead of just script.r arg1 arg2. Python’s Argparse has it: https://docs.python.org/3/library/argparse.html#required and I often find myself implementing something like that as well for every “required” parameter in R as well.

  2. Michael Bertolacci

    Are you willing to budge on this issue? It’s not conventional in UNIX that non-positional arguments are optional. For example, the UNIX ld command requires the -o argument, see https://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html.

    The POSIX standard doesn’t specify this either, and on the flip side positional arguments are not necessarily required either (see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html).

    For myself, I like to name arguments when there are many of them for the same reason I like to name arguments to functions in R sometimes. It’s annoying that there’s no way to make these required.

  3. David Shih repo owner
    • changed status to open

    I'll try implementing a workaround: a function that checks whether a set of arguments have been specified.

    The reason why I don't want to implement an extra argument to the add_argument function is that it will add more complexity than I am willing to stomach at this point. I don't want to introduce extra features that cause bugs, certainly not features that I don’t use myself.

  4. Ryszard Czerminski

    I am all for adding required=TRUE as an option for non-positional argument - I find it very useful.

  5. David Shih repo owner

    I looked into this, and I still can't figure it out.

    It is not as simple as adding a required argument to add_argument.

    During parsing, one central assumption is that positional arguments are required.

    Additionally, optional positional arguments can introduce ambiguity.

    As for required named arguments, the client can do that check very easily by looking at the output of parse_args:

    p <- arg_parser();
    ...
    required <- c("a", "b" "c");
    args <- parse_args(p);
    missing <- ! required %in% names(args);
    if (any(missing)) {
      stop("required arguments missing: ", paste(required[missing], sep=", "))
    }
    

  6. Log in to comment