Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Nov 20, 2024
1 parent cb2c0f0 commit f16d03d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 15 additions & 15 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,22 +318,22 @@ type Command struct {

// NewCommand creates a new command.
func NewCommand(f func(context.Context, []string) error, opts ...Option) (*Command, error) {
c := &Command{
cmd := &Command{
Exec: f,
Descs: make([]Desc, 1),
}
for _, o := range opts {
if err := o.apply(c); err != nil {
if err := o.apply(cmd); err != nil {
return nil, err
}
}
switch noName := c.Descs[0].Name == ""; {
case noName && c.Parent == nil:
c.Descs[0].Name = filepath.Base(os.Args[0])
switch noName := cmd.Name() == ""; {
case noName && cmd.Parent == nil:
cmd.Descs[0].Name = filepath.Base(os.Args[0])
case noName:
return nil, ErrCommandUsageNotSet
}
return c, nil
return cmd, nil
}

// Run executes the command with the context, validating passed arguments.
Expand All @@ -349,20 +349,20 @@ func (cmd *Command) Run(ctx context.Context, args []string) error {

// Sub creates a sub command.
func (cmd *Command) Sub(f func(context.Context, []string) error, opts ...Option) error {
c, err := NewCommand(f, prepend(opts, parent(cmd))...)
sub, err := NewCommand(f, prepend(opts, parent(cmd))...)
if err != nil {
return err
}
cmd.Commands = append(cmd.Commands, c)
cmd.Commands = append(cmd.Commands, sub)
return nil
}

// Command returns the sub command with the name.
func (cmd *Command) Command(name string) *Command {
for _, c := range cmd.Commands {
for _, d := range c.Descs {
for _, sub := range cmd.Commands {
for _, d := range sub.Descs {
if d.Name == name {
return c
return sub
}
}
}
Expand All @@ -388,10 +388,10 @@ func (cmd *Command) Flag(name string) *Flag {

// Get returns a sub command.
func (cmd *Command) Get(name string) *Command {
for _, c := range cmd.Commands {
for _, d := range c.Descs {
for _, sub := range cmd.Commands {
for _, d := range sub.Descs {
if d.Name == name {
return c
return sub
}
}
}
Expand All @@ -408,7 +408,7 @@ func (cmd *Command) Tree() []string {
return v
}

// Name returns the command's primary name.
// Name returns the command's name.
func (cmd *Command) Name() string {
if len(cmd.Descs) != 0 {
return cmd.Descs[0].Name
Expand Down
2 changes: 1 addition & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func testDump(t *testing.T, name string) func(ctx context.Context, args []string
_, _ = fmt.Fprintln(Stdout(ctx), "exec:", name)
_, _ = fmt.Fprintln(Stdout(ctx), "root:", RootName(ctx))
cmd := Cmd(ctx)
_, _ = fmt.Fprintln(Stdout(ctx), "name:", cmd.Descs[0].Name)
_, _ = fmt.Fprintln(Stdout(ctx), "name:", cmd.Name())
_, _ = fmt.Fprintln(Stdout(ctx), "tree:", cmd.Tree())
_, _ = fmt.Fprintln(Stdout(ctx), "args:", args)
vars, _ := VarsOK(ctx)
Expand Down

0 comments on commit f16d03d

Please sign in to comment.