-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can we get mandatory argument support ? #17
Comments
This has been discussed before ( #3 ) and one can note this was never a feature in the Python optparse library this package is based off nor will it be a priority in this package (see https://docs.python.org/2/library/optparse.html#what-are-options-for for the reasoning on why). Other R packages such as "argparse" ( https://github.com/trevorld/argparse ) does support explicitly specifying mandatory positional arguments and/or mandatory optional arguments. If you wanted mandatory arguments with "optparse" you could always manually check if a mandatory value was specified. For example for a mandatory optional argument:
The second example in the package vignette shows checking if a mandatory positional argument was reasonable. Edit 2018-10-19: Comment previously claimed incorrectly that the argparse package doesn't support required optional arguments but actually it does. |
A simple R-style solution would be to lazy evaluate |
@eantonya , the issue with your proposed |
Don't check for |
There is a school of thought that specifying optional arguments in a function (such as I'm not fully persuaded of the merits of having the
Note if one sets an integer value for the With the lazy evaluation approach there is also the risk that a bunch of un-needed computations (possibly with other undesirable side-effects) occur before the I think it is much cleaner and safer to do Step 2 separately either by using instead mandatory positional arguments or instead checking if the optional argument is either present in
Or by doing something like checking that the parsed option is not
If those are too verbose for you one could always write a helper function::
Or the functions themselves later in the Rscript can assert if they were fed reasonable arguments. |
The user can also forgo using this package altogether and do everything themselves - that's obviously not the point. Point is to improve this package to make it easier to use and more versatile. Crappy solutions to this outside of the package exist, but that's what they are - crappy. If you simply must have a default value of Probably any solution you pick is faster to type out than all of this arguing, so maybe you just think that this should never be added, which is fine, but it makes this package less useful than it could be. |
Here's a real-world example btw of why I need this, and why positional arguments are not a good solution. I have an R script that given a date range and a country, prints out the official holidays of that country. All 3 arguments are mandatory, and have no sensible defaults. Maybe you could argue that start/end dates can be positional, but that would still leave country up in the air + surely after using R one can appreciate how much nicer it is not to worry about position of arguments and instead just specify them by name wherever you like. |
There is also the
In your particular use case I'd argue that start, end, AND country need not be mandatory and in fact can all be given a sensible optional default. For a typical person one desired behaviour could be to see what would be all their official holidays for upcoming year after inferring the user's country:
As a user I appreciate it when developer's take the time to pick out or infer reasonable defaults for me and usually try to do the same with my Rscripts. I fail to see in your particular use case why any of your options need to be mandatory. The user can always try the command a second time with explicit options passed in to tweak the output for their use case (or maybe create an alias with their preferred settings).
Someone could want to set that explicitly if they would then pass option into a function which interprets
which is then called in an Rscript by
|
This is not a good solution, because I believe that https://stackoverflow.com/questions/3433603/parsing-command-line-arguments-in-r-scripts
as the developer of Despite the rhetoric on whether flags should or should not be possible to be required, ultimately in real-life situations it is quite often simpler to use cli flag arguments and to make them required. For example, your script has a number of required input parameter; if you can use long-form flags, you can make it much more clear to the reader and user what each input item is. Consider that you are working on a project (such as a long complex bioinformatics pipeline) and you come across a line like this, verbatim, embedded in the source code you are working on;
the lack of flags here make the required positional args completely unintelligible to everyone that has to read this. and its not as simple as "just run instead consider the same thing, but written as this;
Now everyone's code is more readable and easier to understand without the requirement that every new user has to manually inspect I hope you will reconsider your position on this, because the lack of this feature causes a serious detriment to R users and makes R that much more unpleasant to deal with for everyone. |
Okay, I'd be willing to consider a pull request that implements a Do note though that developers have always been free to write scripts with library("optparse")
parser <- OptionParser()
parser <- add_option(parser, "--option1")
parser <- add_option(parser, "--option2")
args <- parse_args(parser)
required_options <- c("option1", "option2")
for (opt in required_options) {
if (is.null(args[[opt]])) {
cat("required option", opt, "not found\n")
quit('no', status = 1, runLast = FALSE)
}
}
print(args) |
If my understanding is correct there is no way to indicate that an argument is required and not specify a default value.
I raised the question : http://stackoverflow.com/questions/35252547/can-i-specify-mandatory-arguments-with-optparse
Am I wrong ?
The text was updated successfully, but these errors were encountered: