Skip to content

Commit 307683e

Browse files
committed
Add the running command to Init
1 parent b9eb289 commit 307683e

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ type Commander interface {
1616
Run(ctx context.Context, cd *Commandeer, args []string) error
1717

1818
// Init called on all ancestors and the executing command itself, before execution, starting from the root.
19-
// This is the place to evaluate flags and set up the command.
20-
Init(*Commandeer) error
19+
// This is the place to evaluate flags and set up the this Commandeer.
20+
// The runner Commandeer holds the currently running command, which will be Init last.
21+
Init(this, runner *Commandeer) error
2122

2223
// WithCobraCommand is called when the cobra command is created.
2324
// This is where the flags, short and long description etc. are added.

‎simplecobra.go‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ type Commander interface {
1818
Run(ctx context.Context, cd *Commandeer, args []string) error
1919

2020
// Init called on all ancestors and the executing command itself, before execution, starting from the root.
21-
// This is the place to evaluate flags and set up the command.
22-
Init(*Commandeer) error
21+
// This is the place to evaluate flags and set up the this Commandeer.
22+
// The runner Commandeer holds the currently running command, which will be Init last.
23+
Init(this, runner *Commandeer) error
2324

2425
// WithCobraCommand is called when the cobra command is created.
2526
// This is where the flags, short and long description etc. are added.
@@ -88,7 +89,7 @@ func (c *Commandeer) init() error {
8889
// Init all of them starting from the root.
8990
for i := len(ancestors) - 1; i >= 0; i-- {
9091
cd := ancestors[i]
91-
if err := cd.Command.Init(cd); err != nil {
92+
if err := cd.Command.Init(cd, c); err != nil {
9293
return err
9394
}
9495
}
@@ -106,8 +107,12 @@ func (r *runErr) Error() string {
106107
}
107108

108109
func (c *Commandeer) compile() error {
110+
useCommandFlagsArgs := "[command] [flags]"
111+
if len(c.commandeers) == 0 {
112+
useCommandFlagsArgs = "[flags] [args]"
113+
}
109114
c.CobraCommand = &cobra.Command{
110-
Use: c.Command.Name(),
115+
Use: fmt.Sprintf("%s %s", c.Command.Name(), useCommandFlagsArgs),
111116
RunE: func(cmd *cobra.Command, args []string) error {
112117
if err := c.Command.Run(cmd.Context(), c, args); err != nil {
113118
return &runErr{err: err}

‎simplecobra_test.go‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ func TestSimpleCobra(t *testing.T) {
4747
c.Assert(tc.persistentFlagName, qt.Equals, "root_persistent")
4848
c.Assert(tc.persistentFlagNameC, qt.Equals, "root_persistent_rootCommand_compiled")
4949
c.Assert(tc.localFlagNameC, qt.Equals, "root_local_rootCommand_compiled")
50+
c.Assert(tc.initRunner, qt.Equals, cd)
51+
c.Assert(tc.initThis, qt.Equals, cd)
5052

5153
// Execute a level 1 command.
5254
// This may not be very realistic, but it works. The common use case for a CLI app is to run one command and then exit.
@@ -62,6 +64,8 @@ func TestSimpleCobra(t *testing.T) {
6264
c.Assert(tc2.localFlagNameC, qt.Equals, "bar_local_lvl1Command_compiled")
6365
c.Assert(tc.persistentFlagName, qt.Equals, "bar_persistent")
6466
c.Assert(tc.persistentFlagNameC, qt.Equals, "bar_persistent_rootCommand_compiled")
67+
c.Assert(tc2.rootCmd.initRunner, qt.Equals, cd2)
68+
c.Assert(tc2.rootCmd.initThis, qt.Equals, cd2.Root)
6569

6670
// Execute a level 2 command.
6771
args = []string{"bar", "baz", "--persistentFlagName", "baz_persistent"}
@@ -73,6 +77,8 @@ func TestSimpleCobra(t *testing.T) {
7377
c.Assert(tc3.rootCmd, qt.Equals, rootCmd)
7478
c.Assert(tc3.parentCmd, qt.Equals, tc2)
7579
c.Assert(tc3.ctx, qt.Equals, ctx)
80+
c.Assert(tc3.rootCmd.initRunner, qt.Equals, cd3)
81+
c.Assert(tc3.rootCmd.initThis, qt.Equals, cd3.Root)
7682

7783
}
7884

@@ -170,7 +176,9 @@ type rootCommand struct {
170176
localFlagNameC string
171177

172178
// For testing.
173-
ctx context.Context
179+
ctx context.Context
180+
initThis *simplecobra.Commandeer
181+
initRunner *simplecobra.Commandeer
174182

175183
// Sub commands.
176184
commands []simplecobra.Commander
@@ -180,10 +188,12 @@ func (c *rootCommand) Commands() []simplecobra.Commander {
180188
return c.commands
181189
}
182190

183-
func (c *rootCommand) Init(*simplecobra.Commandeer) error {
191+
func (c *rootCommand) Init(this, runner *simplecobra.Commandeer) error {
184192
c.isInit = true
185193
c.persistentFlagNameC = c.persistentFlagName + "_rootCommand_compiled"
186194
c.localFlagNameC = c.localFlagName + "_rootCommand_compiled"
195+
c.initThis = this
196+
c.initRunner = runner
187197
return nil
188198
}
189199

@@ -224,10 +234,10 @@ func (c *lvl1Command) Commands() []simplecobra.Commander {
224234
return c.commands
225235
}
226236

227-
func (c *lvl1Command) Init(cd *simplecobra.Commandeer) error {
237+
func (c *lvl1Command) Init(this, runner *simplecobra.Commandeer) error {
228238
c.isInit = true
229239
c.localFlagNameC = c.localFlagName + "_lvl1Command_compiled"
230-
c.rootCmd = cd.Root.Command.(*rootCommand)
240+
c.rootCmd = this.Root.Command.(*rootCommand)
231241
return nil
232242
}
233243

@@ -260,10 +270,10 @@ func (c *lvl2Command) Commands() []simplecobra.Commander {
260270
return nil
261271
}
262272

263-
func (c *lvl2Command) Init(cd *simplecobra.Commandeer) error {
273+
func (c *lvl2Command) Init(this, runner *simplecobra.Commandeer) error {
264274
c.isInit = true
265-
c.rootCmd = cd.Root.Command.(*rootCommand)
266-
c.parentCmd = cd.Parent.Command.(*lvl1Command)
275+
c.rootCmd = this.Root.Command.(*rootCommand)
276+
c.parentCmd = this.Parent.Command.(*lvl1Command)
267277
return nil
268278
}
269279

0 commit comments

Comments
 (0)