From 31760bb5a146b2d3fabc91eec6a9bb18d305f977 Mon Sep 17 00:00:00 2001 From: rsteube Date: Fri, 1 Sep 2023 15:44:27 +0200 Subject: [PATCH] command: support persistent flag --- pkg/command/command.go | 15 ++++++++++++--- pkg/command/flag.go | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/command/command.go b/pkg/command/command.go index d42cf0d..9aa173c 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -22,8 +22,17 @@ type Command struct { } func (c *Command) AddFlag(f Flag) { - if c.Flags == nil { - c.Flags = make(map[string]string) + switch { + case f.Persistent: + if c.PersistentFlags == nil { + c.PersistentFlags = make(map[string]string) + } + c.PersistentFlags[f.format()] = f.Usage + + default: + if c.Flags == nil { + c.Flags = make(map[string]string) + } + c.Flags[f.format()] = f.Usage } - c.Flags[f.format()] = f.Usage } diff --git a/pkg/command/flag.go b/pkg/command/flag.go index 982fbf3..e66fe77 100644 --- a/pkg/command/flag.go +++ b/pkg/command/flag.go @@ -9,6 +9,7 @@ type Flag struct { Value bool Hidden bool Required bool + Persistent bool } func (f Flag) format() string {