trevorld / r-argparse

@@ -1,4 +1,4 @@
Loading
1 -
# Copyright (c) 2012-2020 Trevor L. Davis <trevor.l.davis@gmail.com>
1 +
# Copyright (c) 2012-2021 Trevor L. Davis <trevor.l.davis@gmail.com>
2 2
#
3 3
#  This file is free software: you may copy, redistribute and/or modify it
4 4
#  under the terms of the GNU General Public License as published by the
@@ -47,21 +47,23 @@
Loading
47 47
#' @export
48 48
#' @examples
49 49
#'
50 -
#' parser <- ArgumentParser(description='Process some integers')
51 -
#' parser$add_argument('integers', metavar='N', type = "integer", nargs='+',
52 -
#'                    help='an integer for the accumulator')
53 -
#' parser$add_argument('--sum', dest='accumulate', action='store_const',
54 -
#'                    const='sum', default='max',
55 -
#'                    help='sum the integers (default: find the max)')
56 -
#' parser$print_help()
57 -
#' # default args for ArgumentParser()$parse_args are commandArgs(TRUE)
58 -
#' # which is what you'd want for an Rscript but not for interactive use
59 -
#' args <- parser$parse_args(c("--sum", "1", "2", "3"))
60 -
#' accumulate_fn <- get(args$accumulate)
61 -
#' print(accumulate_fn(args$integers))
50 +
#' if (argparse:::detects_python()) {
51 +
#'   parser <- ArgumentParser(description='Process some integers')
52 +
#'   parser$add_argument('integers', metavar='N', type = "integer", nargs='+',
53 +
#'                      help='an integer for the accumulator')
54 +
#'   parser$add_argument('--sum', dest='accumulate', action='store_const',
55 +
#'                      const='sum', default='max',
56 +
#'                      help='sum the integers (default: find the max)')
57 +
#'   parser$print_help()
58 +
#'   # default args for ArgumentParser()$parse_args are commandArgs(TRUE)
59 +
#'   # which is what you'd want for an Rscript but not for interactive use
60 +
#'   args <- parser$parse_args(c("--sum", "1", "2", "3"))
61 +
#'   accumulate_fn <- get(args$accumulate)
62 +
#'   print(accumulate_fn(args$integers))
63 +
#' }
62 64
ArgumentParser <- function(..., python_cmd = NULL) { # nolint
63 -
    python_cmd <- .find_python_cmd(python_cmd)
64 -
    .assert_python_cmd(python_cmd)
65 +
    python_cmd <- find_python_cmd(python_cmd)
66 +
    assert_python_cmd(python_cmd)
65 67
    initial_python_code <- c("import argparse",
66 68
        "try:",
67 69
        "    import json",
@@ -294,7 +296,7 @@
Loading
294 296
295 297
# Internal function to check python cmd is okay
296 298
# @param python_cmd Python cmd to use
297 -
.assert_python_cmd <- function(python_cmd) {
299 +
assert_python_cmd <- function(python_cmd) {
298 300
    if (!findpython::is_python_sufficient(python_cmd, required_modules = c("argparse", "json | simplejson"))) {
299 301
        stop(paste(sprintf("python executable %s either is not installed,", python_cmd),
300 302
                "is not on the path, or does not have argparse, json modules",
@@ -302,9 +304,15 @@
Loading
302 304
    }
303 305
}
304 306
307 +
detects_python <- function() {
308 +
    python_cmd <- find_python_cmd()
309 +
    findpython::is_python_sufficient(python_cmd,
310 +
                                     required_modules = c("argparse", "json | simplejson"))
311 +
}
312 +
305 313
# Internal function to find python cmd
306 314
# @param python_cmd  Python cmd to use
307 -
.find_python_cmd <- function(python_cmd) {
315 +
find_python_cmd <- function(python_cmd = NULL) {
308 316
    if (is.null(python_cmd)) {
309 317
        python_cmd <- getOption("python_cmd")
310 318
    }
Files Coverage
R/argparse.R 91.19%
Project Totals (1 files) 91.19%
chahs3rvcm6lqvap

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading