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 and intuitive API for defining CLI commands
- Support for command-line flags with validation
- Built-in help command that displays available commands and their flags
- Command locking mechanism to prevent concurrent execution
- Flexible output handling
- Minimal dependencies
go get github.com/golibry/go-cli-command
The package provides a straightforward way to create command-line applications:
- Define your commands by implementing the
Command
interface - Register your commands with the
CommandsRegistry
- Bootstrap your application with the provided arguments
For commands without flags, you can embed the CommandWithoutFlags
struct to avoid implementing empty methods.
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 struct that implements the Command
interface and provides file-based locking to prevent concurrent execution of commands.
Example usage:
-
Create a new FsLockableCommand for a command with a directory for the lock file:
lockableCmd := cli.NewLockableCommand(myCommand, os.TempDir())
-
Or with a custom lock name:
lockableCmd := cli.NewLockableCommandWithLockName(myCommand, os.TempDir(), "custom-lock-name")
-
Register the helper instead of the original command:
registry.Register(lockableCmd)
The helper uses file locks to ensure that only one instance of the command can run at a time, even across different processes. When a command is locked, the Exec
method will return a 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 examples of how to use this package, please see the _examples directory in this repository.
This project is licensed under the terms found in the LICENSE file.