Skip to content

refactor(cli): use Cobra validation for parent commands - #694

Merged
lizhengfeng101 merged 3 commits into
alibaba:mainfrom
amh1k:refactor/cobra-parent-command-validation
Aug 3, 2026
Merged

lizhengfeng101 merged 3 commits into
alibaba:mainfrom
amh1k:refactor/cobra-parent-command-validation

Conversation

@amh1k

@amh1k amh1k commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Description

Simplifies parent-command validation introduced in #660.

Parent commands now use Cobra’s built-in cobra.NoArgs validator instead of manually checking for unexpected arguments. This preserves the non-zero error behavior for unknown subcommands while removing duplicated validation logic.

The parent-command tests now use rootCmd directly, avoiding cloneRootCmd reparenting global Cobra command objects during tests.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

  • make test passes locally
  • Manual testing
go test ./cmd/opencodereview -run TestParentCommands -count=1 -v
go test ./cmd/opencodereview/...
make test
make check
git diff --check

Verified that:

  • unknown parent-command subcommands return an error;
  • known child commands still resolve;
  • bare parent commands still print help and return successfully.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable)
  • I have signed the CLA

Related Issues

Fixes #688

@github-actions

github-actions Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 2 issue(s) in this PR.

  • ✅ Successfully posted inline: 2 comment(s)

Comment thread cmd/opencodereview/config_cmd.go Resolved
Comment thread cmd/opencodereview/delegate_cmd.go Resolved
amh1k and others added 2 commits August 3, 2026 15:02
Fixing inconsistent indentation

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fix inconsistent indentation

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

@lizhengfeng101 lizhengfeng101 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.

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 8fce4c2 into alibaba:main Aug 3, 2026
7 checks passed
kall pushed a commit to kall/open-code-review that referenced this pull request Aug 14, 2026
Three breakage classes from the merge, in one unit because the build does
not compile until all three are fixed:

- Rewrite the old module path to github.com/alibaba/open-code-review in
  core_cmd.go, core_diff.go and core_diff_test.go (alibaba#526).
- Replace the last stale helper call with the model.Diff method the branch
  extracted it into (internal/agent/agent.go).
- Re-register the core command group as cobra.Command trees after main
  deleted cmd/opencodereview/flags.go in the Cobra migration (alibaba#625). The
  newOcrFlagSet-based parsing in five run helpers is gone; flags now bind
  through Flags() and the helpers take explicit parameters.

Conventions carried over from the surrounding commands: the parent rejects
unknown subcommands and prints help when bare (alibaba#660, alibaba#694); rule and prompt
validate positional count through exactArgs so the error carries the
command's own usage (alibaba#749, alibaba#892); repo and diff flags reuse the shared
helpers; prompt phases are declared in ValidArgs for completion (alibaba#625).

The manual dispatcher and its hand-written usage block are dropped -- Cobra
owns both now -- and core_cmd_test.go trades its argv-slice calls for tests
that exercise the wiring through rootCmd.
Githab-capibara added a commit to Githab-capibara/open-code-review that referenced this pull request Sep 17, 2026
* refactor(cli): use Cobra validation for parent commands

* Update cmd/opencodereview/delegate_cmd.go

Fixing inconsistent indentation

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update cmd/opencodereview/config_cmd.go

Fix inconsistent indentation

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

refactor: simplify parent command unknown-subcommand handling

2 participants