Skip to content

feat: Add a proper --help/-h flag with exit code 0 - #180

Merged
Tgenz1213 merged 8 commits into
mainfrom
worktree-177-help-flag
Sep 16, 2026
Merged

Tgenz1213 merged 8 commits into
mainfrom
worktree-177-help-flag

Conversation

@Tgenz1213

Copy link
Copy Markdown
Owner

Summary

  • archguard --help, -h, and help now print top-level usage and exit 0 — and work even outside a git repo, since help is detected before any git/config resolution.
  • archguard check --help/-h and archguard index --help/-h print a custom, per-flag usage listing (built from each subcommand's own flag descriptions via flag.FlagSet.VisitAll, not Go's default flag.PrintDefaults() output) and exit 0.
  • Genuine usage errors (missing command, unknown command, bad flag value) are unchanged and still exit 2 (ExitUsage).
  • check/index --help work even without an archguard.yaml, valid config, or API key set — a gap surfaced during final review (the initial implementation only handled help after config/provider setup, so it would fail with exit 1/3 in exactly the unconfigured state where a user is most likely to reach for --help). Flag definitions for check and index are now built by shared helpers (registerCheckFlags, newIndexFlagSet) so the early-help path and the normal parsing path can never drift out of sync.

Deviations from the plan

  • The original plan didn't require subcommand help to work without a git repo/config — that requirement surfaced during the final whole-branch review as an Important finding, and was fixed in a follow-up commit (bd0b088) rather than left for a separate issue, since it directly affects whether the feature is genuinely usable in the state where --help matters most.
  • Two minor, out-of-scope findings were deliberately deferred rather than fixed here: printIndexUsage's flags-section is currently dead code (since index has zero flags today), and archguard init --help still runs a real init instead of printing help (the issue never mentioned init). Both are small, independent follow-ups.

Test plan

  • go test ./... — all packages pass
  • go build -o archguard ./cmd/archguard — builds clean
  • Manual verification from a non-git-repo temp directory: archguard --help, check --help, index --help all exit 0 with readable usage and no config/API-key warning noise
  • Manual verification: archguard check --not-a-real-flag and archguard typo (unknown command) still exit 2, unaffected
  • New unit tests: TestIsTopLevelHelpRequest, TestExecute_TopLevelHelpExitsSuccess, TestRunCheck_HelpFlagExitsSuccessWithCustomUsage, TestRunIndexCommand_HelpFlagExitsSuccess, TestSubcommandHelpRequest
  • New e2e tests (through the real built binary): "Help flags exit success at every level" (7 cases) and TestE2E_SubcommandHelpWorksWithoutConfig (unconfigured-repo case)
  • golangci-lint run — could not verify in this environment. It panics locally on every branch, including unmodified main, due to a Go 1.27 vs. golangci-lint-v2.12.2 toolchain mismatch (confirmed the panic stack is entirely inside go/types/golangci-lint internals, unrelated to this diff). Docker wasn't available locally as a fallback. Please confirm this is clean via CI.

Closes #177

🤖 Generated with Claude Code

Tgenz1213 and others added 6 commits September 15, 2026 20:01
Adds printCheckUsage() to display custom per-flag help text instead of Go's
default auto-generated format, and handles flag.ErrHelp in runCheck() to
return ExitSuccess with the custom help text rather than ExitUsage.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Exercises the compiled archguard-e2e binary end-to-end for --help, -h,
help, check --help/-h, and index --help/-h, confirming exit code 0 and
usage text through the real CLI entry point rather than only in-process.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Extract check/index flag construction into registerCheckFlags/newCheckFlagSet
and newIndexFlagSet so Execute can detect and print check/index --help before
git.GetRepoRoot(), config loading, and provider construction run, without
duplicating flag definitions. Also trims isTopLevelHelpRequest's doc comment
to fit CLAUDE.md's 2-line comment cap, and documents both the help early-exit
paths and the runIndexCommand/runIndex invariant in CLAUDE.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings September 16, 2026 01:58

Copilot AI 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.

🟡 Changes recommended

Help detection remains incorrect when value-taking flags precede --help.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Adds successful top-level and subcommand help handling, including operation without repository configuration.

Changes:

  • Adds custom help output and early flag handling.
  • Refactors shared flag registration.
  • Adds unit/e2e tests and documentation.
File summaries
File Reviewed changes Findings
test/e2e_test.go Adds binary-level help coverage. None
internal/cli/cli.go Implements help detection and shared flag setup. Moderate (3 votes): separate flag values can prevent help detection. Nit (1 vote): use “flag package” wording.
internal/cli/cli_test.go Adds help behavior and exit-code tests. None
CLAUDE.md Documents CLI help and dispatch behavior. None
Review details

Suppressed comments (1)

internal/cli/cli.go:921

  • The Go package is named flag, not flag.Package; please use “flag package” in this comment.
			break // flag.Package stops parsing flags at the first positional arg
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread internal/cli/cli.go Outdated
Tgenz1213 and others added 2 commits September 15, 2026 21:06
golangci-lint's errcheck flagged the unchecked Fprintln/Fprintf calls
in printCheckUsage/printIndexUsage, added in this branch. Match the
existing _, _ = fmt.Fprintf(...) convention used elsewhere in this file.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
subcommandHelpRequest's manual loop broke at the first token not
starting with "-", so a value-taking flag's value (e.g. "json" in
--format json) was misread as a positional arg and any --help after
it went undetected. Parse with the real FlagSet instead, which already
knows each flag's arity.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Tgenz1213
Tgenz1213 merged commit d936cf6 into main Sep 16, 2026
7 checks passed
@Tgenz1213
Tgenz1213 deleted the worktree-177-help-flag branch September 16, 2026 03:19
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.

feat: Add a proper --help/-h flag with exit code 0

2 participants