A lightweight, flexible framework for building command-line applications in Go. This package
provides a simple way to define, register, and execute CLI commands with support for flags and
help documentation.
Migrated from https://github.com/rsgcata/go-cli-command
- Simple, intuitive API for defining CLI commands
- First-class support for command-line flags with validation
- Built-in
helpcommand that lists commands and their flags with nicely wrapped descriptions - File-based command locking to prevent concurrent execution (cross-process safe)
- Panic-safe command runner with error reporting and non-zero exit codes on failure
- Flexible output handling via injectable
io.Writer - Minimal dependencies (uses
github.com/golibry/go-fsfor file locking) - Small, test-covered core
go get github.com/golibry/go-cli-commandCreate command-line applications by:
- Implementing the
Commandinterface for each command - Registering your commands in a
CommandsRegistry - Bootstrapping the application with the provided arguments
For commands without flags, embed CommandWithoutFlags to avoid boilerplate.
The Command interface defines the methods that a command must implement:
Id() string: Unique identifier for the commandDescription() string: Description shown in helpExec(stdWriter io.Writer) error: Execute the commandDefineFlags(flagSet *flag.FlagSet): Define command-specific flagsValidateFlags() error: Validate the parsed flags
A helper that wraps any Command to enforce exclusive execution using a file lock. This ensures only one instance runs at a time, even across processes. When a lock is already held, Exec returns the sentinel CommandLocked error.
For commands that don't need flags, you can embed this struct to avoid implementing empty methods.
Manages the registration and retrieval of commands. Use NewCommandsRegistry() to create a new registry and Register() to add commands.
The main entry point for your CLI application, which processes arguments, runs commands, and handles output.
For complete, runnable examples (including command implementation, registration, bootstrapping, and locking), see the _examples directory in this repository.
This project is licensed under the terms found in the LICENSE file.