Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
^README.rst$
^Rakefile$
^\.air.toml$
^\.claude$
^\.editorconfig$
^\.git$
^\.github$
Expand Down
30 changes: 13 additions & 17 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Workflow derived from https://github.com/r-lib/actions/tree/master/examples
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

permissions: read-all

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}
Expand All @@ -18,8 +19,8 @@ jobs:
fail-fast: false
matrix:
config:
- {os: macOS-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
# - {os: macOS-latest, r: 'release'}
# - {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
Expand All @@ -31,6 +32,9 @@ jobs:
steps:
- uses: actions/checkout@v4

# - if: runner.os == 'macOS'
# run: brew install --cask xquartz

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
Expand All @@ -41,18 +45,10 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: rcmdcheck
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2

- name: Show testthat output
if: always()
run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
# with:
# upload-snapshots: true
# build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
2 changes: 1 addition & 1 deletion .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:

- name: Test coverage
run: covr::codecov()
shell: Rscript {0}
shell: Rscript {0}
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: argparse
Type: Package
Title: Command Line Optional and Positional Argument Parser
Version: 2.3.1
Version: 2.3.2-1
Authors@R: c(person("Trevor L.", "Davis", role=c("aut", "cre"),
email="trevor.l.davis@gmail.com",
comment = c(ORCID = "0000-0001-6341-4639")),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
argparse 2.3.2 (development)
============================

* `ArgumentParser()` now also checks the `LITTLER_SCRIPT_PATH` environment variable for the script name so it works when called from `littler`.

argparse 2.3.1
==============

Expand Down
16 changes: 14 additions & 2 deletions R/argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,25 @@ needs_prog <- function(argument_names) {

# Manually copied over from getopt to eliminate it as a dependency
get_Rscript_filename <- function() {
prog <- sub("--file=", "", grep("--file=", commandArgs(), value = TRUE)[1])
if (.Platform$OS.type == "windows") {
args <- command_args()
args_idx <- match("--args", args)
if (!is.na(args_idx)) {
args <- args[seq_len(args_idx - 1L)]
}
prog <- sub("--file=", "", grep("^--file=", args, value = TRUE)[1L])
if (is.na(prog)) {
prog <- littler_script_path()
}
if (!is.na(prog) && .Platform$OS.type == "windows") {
prog <- gsub("\\\\", "\\\\\\\\", prog)
}
prog
}

command_args <- function() commandArgs()

littler_script_path <- function() Sys.getenv("LITTLER_SCRIPT_PATH", unset = NA_character_)

# Internal function to check python cmd is okay
# @param python_cmd Python cmd to use
assert_python_cmd <- function(python_cmd) {
Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions tests/testthat/test-argparse.R
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,39 @@ test_that("Paths that `quit()`", {
expect_equal("usage: scripts/test_help.R [-h]", help[1])
expect_equal(" -h, --help show this help message and exit", help[4])
})


test_that("get_Rscript_filename() ignores --file= after --args", {
local_mocked_bindings(
command_args = function() c("Rscript", "--file=script.R", "--args", "--file=fake.R")
)
expect_equal(get_Rscript_filename(), "script.R")
})

test_that("get_Rscript_filename() works with no --args", {
local_mocked_bindings(
command_args = function() c("Rscript", "--file=script.R")
)
expect_equal(get_Rscript_filename(), "script.R")
})

test_that("get_Rscript_filename() returns NA when no --file=", {
local_mocked_bindings(
command_args = function() character(0)
)
expect_equal(get_Rscript_filename(), NA_character_)
})

test_that("get_Rscript_filename() returns NA when no --file= before --args", {
local_mocked_bindings(
command_args = function() c("R", "--args", "--file=boo.r")
)
expect_equal(get_Rscript_filename(), NA_character_)
})

test_that("get_Rscript_filename() returns LITTLER_SCRIPT_PATH when set", {
local_mocked_bindings(
littler_script_path = function() "script.r"
)
expect_equal(get_Rscript_filename(), "script.r")
})
Loading