Skip to content

feat: add btw CLI via Rapp for command-line access to R tools#176

Merged
gadenbuie merged 19 commits intomainfrom
feat/btw-cli
Mar 14, 2026
Merged

feat: add btw CLI via Rapp for command-line access to R tools#176
gadenbuie merged 19 commits intomainfrom
feat/btw-cli

Conversation

@gadenbuie
Copy link
Copy Markdown
Collaborator

@gadenbuie gadenbuie commented Mar 13, 2026

Summary

Adds a btw CLI that wraps the package's tool groups — docs, pkg, info, and cran — so that both humans and LLMs can invoke them from the command line via Rapp.

Key pieces:

  • exec/btw.R — The Rapp CLI script with two-level command dispatch (btw <group> <command> [args] [flags]). Supports TTY-aware output (colored for terminals, plain markdown when piped).
  • R/cli.Rinstall_btw_cli() installer function that verifies all CLI dependencies are present before installing, plus an interactive prompt to install the r-btw-cli skill.
  • inst/cli-skill/r-btw-cli/SKILL.md — Claude Code skill file documenting CLI usage for AI agents.
  • Tests and snapshots — Comprehensive test suite in tests/testthat/test-cli.R covering argument parsing, help text, topic/package lookup semantics, JSON output, and error cases.

CLI commands implemented:

  • btw docs help, btw docs vignette, btw docs news
  • btw pkg document|check|test|load|coverage
  • btw info platform|packages
  • btw cran search|info

Notable behaviors

Topic-first help lookupbtw docs help <name> tries to resolve as a help topic first, falling back to a package listing. Use btw docs help {<pkg>} to explicitly request a package listing, or btw docs help <pkg>::<topic> for scoped lookup.

--json flag — Structured JSON output for commands with tabular/list data, suitable for piping to jq:

btw info platform --json | jq -r '."r_version:"'
btw info packages dplyr ggplot2 --check --json | jq '.[].version'
btw cran search "geospatial" --json | jq '.[].package'
btw cran info ggplot2 --json | jq '.Imports'
btw pkg coverage --json | jq '.[] | select(.coverage < 80)'

Verification

# Install the CLI (requires Rapp v0.3.0+)
btw::install_btw_cli()

# Try it out
btw docs help dplyr::mutate
btw docs help {dplyr}
btw cran search "machine learning" --json | jq '.[0].package'
btw info platform --json
btw pkg test --filter cli

Implement a command-line interface for btw using Rapp (R CLI framework).
The CLI wraps the four tool groups as two-level subcommands:

- btw docs {help,vignette,news} — R package documentation
- btw pkg {document,check,test,load,coverage} — package development
- btw info {platform,packages} — R environment info
- btw cran {search,info} — CRAN package registry

Includes:
- exec/btw: Main Rapp script with nested switch() dispatch
- install_btw_cli(): Convenience installer that checks all dependencies
- Rapp (>= 0.3.0) added to Suggests

Implements _dev/agents/cli/design.md
- Quote short flag annotations to prevent YAML boolean coercion
  (bare `n` was parsed as `false`)
- Fall back to listing available vignettes when intro vignette not
  found, instead of erroring
- Catch warnings from vignette() (signals warning, not error, when
  vignette not found)
Allow `btw docs help dplyr::mutate` as an alternative to
`btw docs help mutate --package dplyr`, mirroring standard R
notation. The :: form takes priority; if --package is also
provided, a warning is emitted and the flag is ignored.
Change switch defaults from NULL (required positional) to "" (optional)
so that running `btw`, `btw docs`, etc. without a subcommand prints
scoped help text instead of erroring. Uses Rapp:::print_app_help() via
commandArgs(TRUE)[1] to render the same output as --help.
@gadenbuie gadenbuie marked this pull request as ready for review March 13, 2026 22:26
- pkgdown requires all exported topics in the reference index
- Tests use processx::run() which must be declared in Suggests
- Add has_value() helper to check for both NA and empty string
  sentinels, replacing bare is.na() checks throughout
- Add defensive S7 type check in btw_output() before property
  extraction, with a clear error for unexpected types
- Switch optional flag defaults from NA_character_ to "" for
  cleaner help text (avoids [default: "NA"] display)
- Flip btw docs help fallback: try topic first, fall back to
  package listing (more intuitive for common usage)
- Add {package} syntax for explicitly requesting package listing
- Update tests to match new "" defaults and new fallback behavior
- Document {package} syntax for explicit package topic listing
- Document pkg::topic scoped help syntax
- Fix --path <dir> on btw pkg test (was missing <dir> placeholder)
Add JSON output support for `btw info platform`, `btw info packages`,
and `btw info packages --check`. The --json flag is a group-level
option that applies to all info subcommands.

Adds btw_json_output() helper using jsonlite::toJSON for structured
output suitable for piping to jq or other tools.
Add JSON output support for `btw cran search` and `btw cran info`.
The --json flag is a group-level option that applies to all cran
subcommands.

Handles serialization of pkgsearch S3 classes (numeric_version,
cran_package) that jsonlite doesn't natively support.
Add JSON output support for `btw pkg coverage`. The --json flag is
a subcommand-level option (only coverage produces structured data
in the pkg group).

Returns the coverage data frame as JSON, with filename and coverage
percentage columns for package-level, or line-level details when
--file is specified.
@gadenbuie gadenbuie merged commit 7e48b07 into main Mar 14, 2026
9 checks passed
@gadenbuie gadenbuie deleted the feat/btw-cli branch March 16, 2026 12:37
@vladimirobucina
Copy link
Copy Markdown

this is great, I just saw couple of days ago that new Rapp was published and now I see that you've added this to btw. Have you done some benchmarking to compare token consumption between CLI version and MCP version of these tools?

@gadenbuie
Copy link
Copy Markdown
Collaborator Author

Have you done some benchmarking to compare token consumption between CLI version and MCP version of these tools?

Thanks! I don't have any benchmarking, but I the CLI version will definitely use less tokens. The skill that comes with the CLI teaches the model how to use btw from the CLI in roughly the same number of tokens as a single tool description. In my experience so far, that skill is enough for the model to use btw effectively and it rarely needs to read the btw --help text. So that's one way the CLI reduces token usage.

The other is that if you're using btw tools via MCP you're likely including all of the tool definitions in your system prompt, which is also included even if you aren't doing anything with R in your session. With the CLI, however, less than 100 tokens are used for the skill description, so your system prompt overhead is much lower.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants