Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@audy
Created February 8, 2018 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audy/cc55820c6acdaad67d231c968aa26031 to your computer and use it in GitHub Desktop.
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)
#!/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