refactor: parsing cli arguments use cobra#513
Conversation
nikolasdehor
left a comment
There was a problem hiding this comment.
Thorough Cobra migration with an excellent verification matrix (28 SAME + 39 EXPECTED + 0 UNEXPECTED). The level of testing documented in the PR body gives strong confidence this is a safe migration.
A few observations:
-
RunEreturningnilon user-facing errors: Throughout the code, when an error occurs (e.g., config load failure), the pattern isfmt.Printf("Error: ..."); return nil. This means cobra treats it as success (exit 0). In the old code, some of these paths calledos.Exit(1). You may want to return the error (orreturn fmt.Errorf(...)) for cases that should exit non-zero, and use cobra'sSilenceErrors/SilenceUsageto control whether cobra prints its own message. That said, this preserves the existing behavior for the most part, so it's not a blocker. -
--version/-vhandling: TherootCmd.RunEchecks the version flag manually. Cobra has built-in version support viarootCmd.Version = versionthat handles--versionnatively. But your approach gives full control over the output format, so this is fine. -
Global
rootCmdvar: Using a package-levelvar rootCmdwithinit()is the standard Cobra pattern, though some projects prefer constructing the root command in a function for testability. Not a concern for a CLI binary. -
New dependency:
spf13/cobra+spf13/pflag+mousetrapadds ~3 transitive deps. For a CLI app this is reasonable — Cobra is the de facto standard. -
Unicode escapes: The emoji replacements (
"\u2713"for checkmark,"\U0001f50d"for magnifier) are functionally identical. Just noting this is a side effect of the rewrite, not intentional normalization.
Overall this is a significant improvement for maintainability. LGTM.
Signed-off-by: Kai Xia <kaix+github@fastmail.com>
Signed-off-by: Kai Xia <kaix+github@fastmail.com>
Signed-off-by: Kai Xia <kaix+github@fastmail.com>
Signed-off-by: Kai Xia <kaix+github@fastmail.com>
21e4236 to
c43ca92
Compare
|
@yinwm This looks good to me |
|
Note: #429 also migrates the CLI to Cobra. Both PRs address the same refactoring goal. Suggesting the maintainer pick one to avoid conflicting implementations. |
|
Closing in favour of #429 |
📝 Description
Parsing cli argument using os.Args is an anti pattern, let's move that to a better library.
🗣️ Type of Change
🤖 AI Code Generation
🔗 Related Issue
#429
#214
🧪 Test Environment
Irrelevant.
📸 Evidence (Optional)
☑️ Checklist