- changed status to closed
Add option to specify arguments are required
Maybe something like required=TRUE
Comments (9)
-
repo owner -
repo owner All positional arguments are required. All non-positional arguments are optional.
-
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 justscript.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.
-
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.
-
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. -
I am all for adding required=TRUE as an option for non-positional argument - I find it very useful.
-
repo owner - changed status to resolved
I looked into this, and I still can't figure it out.
It is not as simple as adding a
required
argument toadd_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=", ")) }
-
repo owner - changed status to open
-
repo owner - changed status to closed
- Log in to comment