open-code-review/cmd/opencodereview/root.go
kite 80a5794667
Some checks are pending
CI / test (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
refactor(cli): migrate to Cobra framework for shell completion support (#625)
* refactor(cli): migrate to Cobra framework for shell completion support

Replace the hand-rolled ocrFlagSet + switch dispatch with spf13/cobra,
enabling native bash/zsh/fish/powershell completion via `ocr completion`.

Key changes:
- Add root.go (rootCmd definition, version flag with -V shorthand)
- Add completion.go (ocr completion [bash|zsh|fish|powershell])
- Add shared_flags.go (reusable flag registration helpers + validation)
- Rewrite all *_cmd.go to use cobra.Command with RunE
- Delete flags.go (ocrFlagSet, expandShortFlags, parseXxxFlags)
- Add compat_test.go (test compatibility wrappers for existing tests)
- Promote github.com/spf13/cobra from indirect to direct dependency

Behavioral improvements over the previous implementation:
- Shell completion for all commands, flags, and enum values
- "Did you mean?" suggestions for misspelled commands
- cobra.NoArgs on review/scan prevents silent positional arg ignoring
- Cleaner error messages on unknown flags (no full flag dump)
- Consistent help output format across all subcommands

Closes #576

* fix(cli): add Args: cobra.NoArgs to viewerCmd

Prevents `ocr viewer localhost:3000` from silently ignoring the
positional argument and starting on the default address.
Consistent with reviewCmd and scanCmd.

* feat(cli): add "Did you mean?" suggestions for misspelled flags

Cobra only suggests corrections for unknown subcommands, not flags.
Add a levenshtein-distance based suggestion that fires when cobra
returns an "unknown flag" error, matching the same UX pattern.

Examples:
  --hel     → Did you mean this? --help
  --audienc → Did you mean this? --audience
  --comit   → Did you mean this? --commit

* fix(deps): promote spf13/pflag to direct dependency

After the Cobra migration, pflag is directly imported but was still
marked as indirect in go.mod, causing CI's go-mod-tidy check to fail.

* refactor(cli): use idiomatic cobra patterns for args validation and flag errors

Replace hand-rolled argument validation in configSetCmd/configUnsetCmd
with cobra.ExactArgs, and move flag typo suggestions from post-hoc error
string parsing into SetFlagErrorFunc where cobra provides the command
context directly.
2026-07-31 16:56:07 +08:00

56 lines
1.3 KiB
Go

package main
import (
"fmt"
"runtime"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "ocr",
Short: "OpenCodeReview - AI-Powered Code Review CLI",
Long: `OpenCodeReview - AI-Powered Code Review CLI
An AI-powered code review tool that reads git diffs, sends them to a
configurable LLM service, and generates review comments.`,
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
v, _ := cmd.Flags().GetBool("version")
if v {
printVersion()
return nil
}
return cmd.Help()
},
}
func init() {
rootCmd.SetFlagErrorFunc(flagErrorWithSuggestion)
rootCmd.Flags().BoolP("version", "V", false, "version for ocr")
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(reviewCmd)
rootCmd.AddCommand(scanCmd)
rootCmd.AddCommand(delegateCmd)
rootCmd.AddCommand(sessionCmd)
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(llmCmd)
rootCmd.AddCommand(rulesCmd)
rootCmd.AddCommand(viewerCmd)
rootCmd.AddCommand(completionCmd)
}
func versionString() string {
s := fmt.Sprintf("open-code-review %s", Version)
if GitCommit != "" {
s += fmt.Sprintf(" (%s)", GitCommit)
}
s += fmt.Sprintf(" %s/%s\n", runtime.GOOS, runtime.GOARCH)
if BuildDate != "" {
s += fmt.Sprintf("built at: %s\n", BuildDate)
}
s += "https://github.com/alibaba/open-code-review\n"
return s
}