p <- arg_parser("A text file modifying program")
# Add a positional argument
p <- add_argument(p, "input", help="input file")
# Add an optional argument
p <- add_argument(p, "--output", help="output file", default="output.txt")
# Add a flag
p <- add_argument(p, "--append", help="append to file", flag=TRUE)
# Add multiple arguments together
p <- add_argument(p,
c("ref", "--date", "--sort"),
help = c("reference file", "date stamp to use", "sort lines"),
flag = c(FALSE, FALSE, TRUE))
# Print the help message
print(p)
# Example of custom type, using the example from pythons argparse
setClass("perfectSquare")
setMethod("coerce", c(from = "ANY", to = "perfectSquare"),
function(from, to) {
from <- as.numeric(from)
if (!all.equal(from, as.integer(from))) {
stop("Type error: ", from, " is not an integer!")
}
sqt <- sqrt(from)
if (sqt != as.integer(sqt)) {
stop("Type error: ", from, " is not a perfect square!")
}
from
}
)
p2 <- arg_parser("Perfect square checker")
p2 <- add_argument(p2, arg = c("--perfect-square"),
help = "A perfect square integer",
type = "perfectSquare")
parse_args(p2, c("--perfect-square", 144))
Run the code above in your browser using DataLab