diff --git a/args.go b/args.go index 3ab9487..6149108 100644 --- a/args.go +++ b/args.go @@ -14,6 +14,19 @@ func (a *argGroup) have() bool { return len(a.args) > 0 } +// GetArg gets an argument definition. +// +// This allows existing arguments to be modified after definition but before parsing. Useful for +// modular applications. +func (a *argGroup) GetArg(name string) *ArgClause { + for _, arg := range a.args { + if arg.name == name { + return arg + } + } + return nil +} + func (a *argGroup) Arg(name, help string) *ArgClause { arg := newArg(name, help) a.args = append(a.args, arg) diff --git a/cmd.go b/cmd.go index c3ebdc7..9bbc793 100644 --- a/cmd.go +++ b/cmd.go @@ -21,6 +21,14 @@ func (c *cmdGroup) defaultSubcommand() *CmdClause { return nil } +// GetArg gets a command definition. +// +// This allows existing commands to be modified after definition but before parsing. Useful for +// modular applications. +func (c *cmdGroup) GetCommand(name string) *CmdClause { + return c.commands[name] +} + func newCmdGroup(app *Application) *cmdGroup { return &cmdGroup{ app: app, diff --git a/flags.go b/flags.go index eb5ab69..b08afbc 100644 --- a/flags.go +++ b/flags.go @@ -19,6 +19,14 @@ func newFlagGroup() *flagGroup { } } +// GetFlag a flag definition. +// +// This allows existing flags to be modified after definition but before parsing. Useful for +// modular applications. +func (f *flagGroup) GetFlag(name string) *FlagClause { + return f.long[name] +} + // Flag defines a new flag with the given long name and help. func (f *flagGroup) Flag(name, help string) *FlagClause { flag := newFlag(name, help) diff --git a/flags_test.go b/flags_test.go index a1ddd29..d75ca4e 100644 --- a/flags_test.go +++ b/flags_test.go @@ -144,3 +144,15 @@ func TestDuplicateLongFlag(t *testing.T) { _, err := app.Parse([]string{}) assert.Error(t, err) } + +func TestGetFlagAndOverrideDefault(t *testing.T) { + app := newTestApp() + a := app.Flag("a", "").Default("default").String() + _, err := app.Parse([]string{}) + assert.NoError(t, err) + assert.Equal(t, "default", *a) + app.GetFlag("a").Default("new") + _, err = app.Parse([]string{}) + assert.NoError(t, err) + assert.Equal(t, "new", *a) +}