3

I created a Go CLI app with the cobra framework via the cobra add command. After building it with go build it works totally fine if I'm in the repository in the windows cmd. If I click on the .exe I get the following message on a terminal:

This is a command line tool.

You need to open cmd.exe and run it from there.

This is not suitable for my case. I'd like to have a console applocation like in C or C++ which opens directly and you can type in commands there. Do you have any suggestions how I could realize this while using cobra?

Thank you a lot in advance.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
alexfwulf
  • 199
  • 1
  • 13

1 Answers1

2

This is done on purpose as no arguments will be provided to the command when started through explorer.exe, as the main purpose of using cobra is for parsing arguments and stuff.

To disable this feature.

Add this line in your init()

func init(){
  cobra.MousetrapHelpText = ""
}

This let's your program run, and it exits immediately, to see if it's working fine. Use time.Sleep(), so that you can see the output.

nilsocket
  • 1,441
  • 9
  • 17
  • Thank you for your response. Right now I can see the first output. But I'd like to open a fully functional console so I don't have to navigate through my file system. – alexfwulf Aug 22 '18 at 14:24
  • 1
    >But I'd like to open a fully functional console so I don't have to navigate through my file system. I don't understand what you were asking. – nilsocket Aug 22 '18 at 14:25
  • I want to have the application and the corresponding database in one folder. If I click on the `.exe` it will open a terminal where I can make my inputs. Just as in a C console application. – alexfwulf Aug 22 '18 at 14:46
  • @alexfwulf Maybe you can consider using https://github.com/c-bata/go-prompt to implement what you require – W1M0R Nov 23 '20 at 13:05