cobra-modularity

Build modular Cobra-based CLIs with self-registering command modules.

1|Updated Dec 3, 2025
One-click install
npx skills add https://github.com/yurifrl/cly --skill cobra-modularity
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cobra-modularity
Source: https://github.com/yurifrl/cly/tree/main/.opencode/skill/cobra-modularity
Command: npx skills add https://github.com/yurifrl/cly --skill cobra-modularity

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Building scalable CLI tools often leads to monolithic root commands. This skill provides a modular Cobra-based architecture that lets each feature live in its own package and register itself with the root, avoiding centralized registration and making the codebase easier to extend and maintain.

Core Features & Use Cases

  • Modular command structure: Each module implements a Register(parent *cobra.Command) and wires its own subcommands.
  • Self-registration pattern: Modules automatically attach to the root without invasive root changes.
  • Cobra best practices: Demonstrates RunE usage, persistent vs local flags, PreRun hooks, and proper argument validation.
  • Use Case: Add a new feature module (e.g., a UUID generator) by placing it under modules/uuid and invoking Register on the root, enabling clean growth of CLI capabilities.

Quick Start

Create a new module directory under modules/yourmodule, implement a Register(parent *cobra.Command) function that attaches commands to the parent, and ensure subcommands self-register in init(). Example structure: modules/demo/ modules/uuid/

Frequently Asked Questions about cobra-modularity

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I build a modular CLI in Go without centralizing command registration?

To build a modular CLI, each feature package implements a Register function that attaches its own subcommands to the root command. This self-registering pattern avoids monolithic root files.

What is the best way to structure a scalable Cobra CLI architecture?

A scalable Cobra CLI architecture splits features into independent packages under directories like modules/uuid. Each module self-registers in its init block and wires nested subcommands to the root.

How do I add a new command module to an existing Cobra CLI?

To add a new command module, create a directory under modules/yourmodule, implement a Register(parent *cobra.Command) function that attaches commands, and ensure subcommands self-register in the init block.

Does this modular CLI pattern support RunE execution and persistent flags in Cobra?

Yes, the modular CLI pattern supports RunE-based command execution, persistent versus local flags, PreRun hooks, and proper argument validation following Cobra best practices.

Do I need the spf13/cobra library to use this modular command architecture?

Yes, you need Go and the spf13/cobra library to use this modular command architecture, as it relies on Cobra command structures and RunE-based execution logic.