feat: add SetUsage to support custom help descriptions in CLI (by AI)

This commit is contained in:
AI Engineer 2026-05-13 00:55:29 +08:00
parent e797dc846a
commit 674ec88117
2 changed files with 15 additions and 2 deletions

View File

@ -64,6 +64,7 @@ func main() {
- **Trace ID Propagation**: Automatically generates a shared Trace ID for all services during startup, ensuring log correlation. - **Trace ID Propagation**: Automatically generates a shared Trace ID for all services during startup, ensuring log correlation.
- **Secure IPC**: Token-based Unix Domain Socket for `status` and `kill` commands. - **Secure IPC**: Token-based Unix Domain Socket for `status` and `kill` commands.
- **Zero Configuration PID**: Automatic PID management in the system temporary directory. - **Zero Configuration PID**: Automatic PID management in the system temporary directory.
- **Embedded Log Writer**: Automatically registered log writer service with high priority (-100).
## Commands ## Commands

View File

@ -29,6 +29,7 @@ var (
// Default configuration // Default configuration
appName = filepath.Base(os.Args[0]) appName = filepath.Base(os.Args[0])
appVersion = "1.0.1" appVersion = "1.0.1"
appUsage = ""
// Internal state // Internal state
commands = make(map[string]*command) commands = make(map[string]*command)
@ -64,6 +65,9 @@ func init() {
AddCommand("restart", "Restart the service", restartCmd) AddCommand("restart", "Restart the service", restartCmd)
AddCommand("status", "Show service status", statusCmd) AddCommand("status", "Show service status", statusCmd)
AddCommand("kill", "Send signal to a specific service: kill <svc_name> <signal_num>", killCmd) AddCommand("kill", "Send signal to a specific service: kill <svc_name> <signal_num>", killCmd)
// Auto-register log writer service with high priority
Register("log-writer", log.WriterService, -100, 0, 0)
} }
// Register adds a service to be managed by the starter. // Register adds a service to be managed by the starter.
@ -83,6 +87,11 @@ func SetAppInfo(name, version string) {
appVersion = version appVersion = version
} }
// SetUsage sets custom usage text to be displayed in help.
func SetUsage(text string) {
appUsage = text
}
// AddCommand adds a custom command. // AddCommand adds a custom command.
func AddCommand(name, desc string, fn func()) { func AddCommand(name, desc string, fn func()) {
commands[name] = &command{name: name, desc: desc, fn: fn} commands[name] = &command{name: name, desc: desc, fn: fn}
@ -117,8 +126,11 @@ func Run() {
} }
func showHelp() { func showHelp() {
fmt.Printf("%s (%s)\n\nUsage:\n %s [command] [options]\n\nCommands:\n", fmt.Printf("%s (%s)\n\n", appName, appVersion)
appName, appVersion, filepath.Base(os.Args[0])) if appUsage != "" {
fmt.Printf("%s\n\n", appUsage)
}
fmt.Printf("Usage:\n %s [command] [options]\n\nCommands:\n", filepath.Base(os.Args[0]))
var names []string var names []string
for cmdName := range commands { for cmdName := range commands {