Skip to content

feat: add --version flag with app-injected release string - #2

Merged
gauravharsha merged 2 commits into
mainfrom
feat-version-flag
Jun 24, 2026
Merged

feat: add --version flag with app-injected release string#2
gauravharsha merged 2 commits into
mainfrom
feat-version-flag

Conversation

@gauravharsha

@gauravharsha gauravharsha commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Add a first-class --version flag to the params parser, as a true sibling of --help.

  • version is a native argparse flag (added next to the built-in _help in libs/argparse/argparse.h), surfaced via help_requested() / version_requested() accessors. No params-layer bolt-on, no cached pointer, no raw-argv scan.
  • The library is version-agnostic: the application injects the release string via the params constructor (default "").
  • --version is command-line only (like --help); it is not INI-settable.
  • parse() returns false for an informational flag (--help or --version) — a stop signal. The library does not call std::exit(); the caller prints the message (on its chosen rank) and finalizes/exits. This suits the MPI programs that embed the parser.
  • help_or_version() prints the release string for --version, otherwise the help text. print() / help() also emit a Version: <value> line when a version is set.
  • define() rejects the reserved names help / ? / version, so a consumer parameter cannot silently shadow the built-in flags.

API

green::params::params p(description, GREEN_RELEASE);  // version injected at construction
if (!p.parse(argc, argv)) {        // false => informational flag (or args-before-defs)
  p.help_or_version();             // prints version string or help text
  // caller finalizes / exits as appropriate
}

Tests

In-process (no fork needed, since nothing exits): constructor injection round-trip, --help/--version symmetry via the accessors, help_or_version() output, the Version: line in print()/help(), --version alongside other args, and reserved-name rejection.

🤖 Generated with Claude Code

@codecov-commenter

codecov-commenter commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.10%. Comparing base (14d9818) to head (408dc68).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/green/params/params.h 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main       #2      +/-   ##
==========================================
+ Coverage   90.59%   91.10%   +0.51%     
==========================================
  Files           6        6              
  Lines         999     1068      +69     
==========================================
+ Hits          905      973      +68     
- Misses         94       95       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a reusable --version flag to the green::params::params argument parser, allowing applications to inject a release string and have the parser print it and exit successfully when requested.

Changes:

  • Registers a new --version flag in params and adds set_version() / version() APIs.
  • Implements version print-and-exit behavior during parsing.
  • Adds unit tests for the version string round-trip and the --version exit behavior (via fork/pipe capture).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/green/params/params.h Adds the --version flag registration, version storage/accessors, and version handling in parse().
test/params_test.cpp Adds tests for version setter/getter and end-to-end --version output/exit behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/green/params/params.h Outdated
Comment thread src/green/params/params.h Outdated
Comment thread test/params_test.cpp Outdated
Comment thread src/green/params/params.h Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

Comment thread src/green/params/params.h Outdated
Comment thread src/green/params/params.h
Comment thread src/green/params/params.h Outdated
Comment thread test/params_test.cpp
Comment thread test/params_test.cpp Outdated
@gauravharsha
gauravharsha force-pushed the feat-version-flag branch 2 times, most recently from 9ae7091 to 1721aef Compare June 23, 2026 15:40
@gauravharsha
gauravharsha requested a review from Copilot June 23, 2026 22:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

Comment thread src/green/params/params.h Outdated
Comment thread src/green/params/params.h Outdated
Comment thread src/green/params/params.h
Comment thread src/green/params/params.h
Comment thread src/green/params/params.h
@gauravharsha

Copy link
Copy Markdown
Contributor Author

Wasn't happy with initial implementation. Now updated and cleaner.
@egull requesting your review once again.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

Comments suppressed due to low confidence (1)

src/green/params/params.h:312

  • parse(const std::string&) allocates argv via get_argc_argv() (which uses new[]) but never frees it, leaking memory on every call (including in tests that parse repeatedly). Free the allocated array after parse(argc, argv) returns.
    bool parse(const std::string& s) {
      std::string to_parse = s;
      auto [argc, argv]    = get_argc_argv(to_parse);
      return parse(argc, argv);
    }

Comment thread src/green/params/params.h
Comment on lines 323 to +327
parsed_ = true;
if (parameters_map_.empty() && argc > 2)
return false; // we provided command line parameters but haven't defined any them yet
bool help_requested = build();
return !help_requested;
build();
return !help_requested() && !version_requested();
Comment thread src/green/params/params.h Outdated
Comment thread src/green/params/params.h Outdated
Comment thread src/green/params/params.h
Comment thread src/green/params/params.h
Comment on lines +362 to +370
/**
* Print the informational message that made parse() return false: the release string for
* --version, otherwise the help text. Call this on the rank that owns console output. Future
* informational flags should be handled here so callers can stay agnostic to the specific flag.
*/
void help_or_version() {
if (version_requested()) {
std::cout << version_ << std::endl;
return;
Comment thread libs/argparse/argparse.h
Comment on lines +700 to 703
if (_help || _version) {
// help();
return true;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it's over-engineering. But fine, we can do it.

@egull egull left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine. Lots of engineering for a bit of parameter parsing, but it works.

Comment thread libs/argparse/argparse.h Outdated
Comment thread libs/argparse/argparse.h
Comment on lines +700 to 703
if (_help || _version) {
// help();
return true;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it's over-engineering. But fine, we can do it.

Comment thread src/green/params/params.h
…tor)

Add a first-class --version flag as a sibling of --help:
- argparse: add a native `version` flag with help_requested()/version_requested()
  accessors; build() treats --help/--version uniformly (skip validation).
- params: inject the version string via the constructor (default ""), delegate
  flag state to argparse, and report informational flags by returning false from
  parse() (no std::exit; the caller prints and finalizes). help_or_version()
  prints the version string or the help text; print()/help() emit a "Version:"
  line when a version is set. define() rejects the reserved names
  help/?/version so a consumer parameter can't shadow the built-in flags.
- tests: cover constructor injection, --help/--version symmetry, help_or_version
  output, the Version line in print()/help(), and reserved-name rejection.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

src/green/params/params.h:317

  • parse(const std::string&) leaks the argv array allocated by get_argc_argv(). This can accumulate in long-running processes or in repeated in-process parses/tests. Use RAII (or delete[]) to ensure the allocated char** is freed even if parse(argc, argv) throws.
    bool parse(const std::string& s) {
      std::string to_parse = s;
      auto [argc, argv]    = get_argc_argv(to_parse);
      return parse(argc, argv);
    }

Comment thread test/params_test.cpp
Comment thread test/params_test.cpp
Comment thread test/params_test.cpp
@gauravharsha
gauravharsha merged commit 7b00054 into main Jun 24, 2026
1 of 2 checks passed
@gauravharsha
gauravharsha deleted the feat-version-flag branch June 24, 2026 07:11
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.

4 participants