Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when setting default command to gcli app, ./appcli can't run with default command #130

Open
justz0 opened this issue Jul 15, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@justz0
Copy link

justz0 commented Jul 15, 2024

System (please complete the following information):

  • OS: windows 10
  • GO Version: 1.21.6
  • Pkg Version: v3.2.3

Describe the bug

I set a ‘run’ command for the app through ‘app.SetDefaultCommand’, and want the app to execute the ‘run’ command by default (./app). But after compiling, I run ‘./app’ and get an error ‘ERROR: unknown input command "run"’

To Reproduce

package main

import (
	"fmt"
	"github.com/gookit/gcli/v3"
	"log"
)

var run = &gcli.Command{
	Name:    "run",
	Desc:    "run app client",
	Func: func(c *gcli.Command, args []string) error {
		//server.Run()
		fmt.Println("test default command")
		return nil
	},
	//Hidden: true,
}

func Run() {
	app := gcli.NewApp()
	app.Version = "1.0.0"
	app.Name = "Test Client"
	app.Desc = ""
	app.Add(run)
	app.SetDefaultCommand(run.Name)
	app.Run(nil)
}

func main() {
	Run()
}

Expected behavior

I expected to happen: execute "./app" command, then the app will execute 'run' command,just like execute "./app run"

@justz0
Copy link
Author

justz0 commented Jul 15, 2024

func (app *App) prepareRun() (code int, name string) {
	// find command name.
	name = app.findCommandName()
	if name == HelpCommand {
		if len(app.args) == 0 { // like 'help'
			app.showApplicationHelp()
		} else {
			// like 'help COMMAND'
			code = app.showCommandHelp(app.args)
		}
		return
	}

	// not input and not set defaultCommand
	if name == "" {
		if app.Func != nil {
			code = app.doRunFunc(app.args)
		} else {
			app.showApplicationHelp()
		}
		return
	}
    
	// name is not empty, but is not command.
	if app.inputName == "" {
		Logf(VerbDebug, "input the command is not an registered: %s", name)
		hookData := map[string]any{"name": name, "args": app.args}

		// fire events
		if stop := app.Fire(events.OnAppCmdNotFound, hookData); stop {
			return
		}
		if stop := app.Fire(events.OnCmdNotFound, hookData); stop {
			return
		}

		app.showCommandTips(name)
		return
	}

	// is valid command name.
	app.commandName = name
	return GOON, name
}

I debug to this code , app.inputName is empty ,Caused the default command to fail

@inhere inhere added the bug Something isn't working label Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants