summaryrefslogtreecommitdiff
path: root/srv/cfg/cfg.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2021-08-10 13:07:14 -0600
committerBrian Picciano <mediocregopher@gmail.com>2021-08-10 13:07:14 -0600
commitac5275353c0d0f33ebe47c3e177d0b35b7bd6581 (patch)
treee0828f25f072fbe41e5c79ae1e3a9c6bd588f0f3 /srv/cfg/cfg.go
parentca27cd6c677f5effe7b08639a8db92371065b4e3 (diff)
implement publish sub-cmd of mailinglist-cli
Diffstat (limited to 'srv/cfg/cfg.go')
-rw-r--r--srv/cfg/cfg.go26
1 files changed, 25 insertions, 1 deletions
diff --git a/srv/cfg/cfg.go b/srv/cfg/cfg.go
index 08a9e53..685248c 100644
--- a/srv/cfg/cfg.go
+++ b/srv/cfg/cfg.go
@@ -19,12 +19,14 @@ type Cfg struct {
*flag.FlagSet
hooks []func(ctx context.Context) error
+ args []string
}
// New initializes and returns a new instance of *Cfg.
func New() *Cfg {
return &Cfg{
FlagSet: flag.NewFlagSet("", flag.ExitOnError),
+ args: os.Args[1:],
}
}
@@ -38,7 +40,7 @@ func (c *Cfg) OnInit(cb func(context.Context) error) {
// called. If one returns an error that error is returned and no further hooks
// are run.
func (c *Cfg) Init(ctx context.Context) error {
- if err := c.FlagSet.Parse(os.Args[1:]); err != nil {
+ if err := c.FlagSet.Parse(c.args); err != nil {
return err
}
@@ -50,3 +52,25 @@ func (c *Cfg) Init(ctx context.Context) error {
return nil
}
+
+// SubCmd should be called _after_ Init. Init will have consumed all arguments
+// up until the first non-flag argument. This non-flag argument is a
+// sub-command, and is returned by this method. This method also resets Cfg's
+// internal state so that new options can be added to it.
+//
+// If there is no sub-command following the initial set of flags then this will
+// return empty string.
+func (c *Cfg) SubCmd() string {
+ c.args = c.FlagSet.Args()
+ if len(c.args) == 0 {
+ return ""
+ }
+
+ subCmd := c.args[0]
+
+ c.FlagSet = flag.NewFlagSet(subCmd, flag.ExitOnError)
+ c.hooks = nil
+ c.args = c.args[1:]
+
+ return subCmd
+}