Created
February 8, 2018 02:56
-
-
Save audy/cc55820c6acdaad67d231c968aa26031 to your computer and use it in GitHub Desktop.
Attempting to write an argparse clone in pure R (using R6 classes tho)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env Rscript | |
library(R6) | |
library(testthat) | |
Argument <- R6Class('Argument', | |
public = list( | |
flag = NA, | |
help = NA, | |
short_flag = NA, | |
initialize = function(flag=NA, short_flag=NA, help=NA) { | |
self$flag <- flag | |
self$short_flag <- short_flag | |
self$help <- help | |
} | |
) | |
) | |
ArgumentParser <- R6Class('ArgumentParser', | |
public = list( | |
arguments = NULL, | |
description = NA, | |
initialize = function(description=NA) { | |
self$description <- description | |
}, | |
add_argument = function(...) { | |
arg <- Argument$new(...) | |
# append argument to arguments | |
self$arguments <- c(self$arguments, arg) | |
}, | |
print_help = function() { | |
if (!is.na(self$description)) { | |
message(self$description) | |
message() | |
} | |
for (argument in self$arguments) { | |
message( | |
paste( | |
argument$flag, argument$short_flag, ' ', argument$help | |
) | |
) | |
} | |
} | |
) | |
) | |
p <- ArgumentParser$new(description='this is a test of the rargparse system') | |
p$add_argument('--foo', '-f', help='asdf') | |
p$add_argument('--bar', '-b', help='asdf') | |
p$print_help() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment