-1

I use the following code to create command which should run according to some flags that are passed from the cli.

I use the cobra repo https://github.com/spf13/cobra

when I run it with go run main.go echo test

I get

Print: test

which works.

Now I run go install open the bin directory and click on the file newApp (this my name of my app)

and it prints

Usage:
  MZR [command]

Available Commands:
  echo        Echo anything to the screen
  help        Help about any command
  print       Print anything to the screen

Flags:
  -h, --help   help for MZR

Use "MZR [command] --help" for more information about a command.


[Process completed]

And I cannot use any commands (like MZR echo) which I was able when I run it locally with go run main.go echo test

But I want to use it like following MZR -h or MZR echo , How I can do it ? (and also give to my friend the file from the bin that created after go install - which is Unix executable - 3.8 MB )

e.g. like this repo which use the same command line tools and to run it you use hoarder --server https://github.com/nanopack/hoarder

This is the code for example (to make it more simpler )

package main

import (
    "fmt"
    "strings"

    "github.com/spf13/cobra"
)

func main() {
    var echoTimes int

    var cmdPrint = &cobra.Command{
        Use:   "print [string to print]",
        Short: "Print anything to the screen",
        Long: `print is for printing anything back to the screen.
For many years people have printed back to the screen.`,
        Args: cobra.MinimumNArgs(1),
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println("Print: " + strings.Join(args, " "))
        },
    }

    var cmdEcho = &cobra.Command{
        Use:   "echo [string to echo]",
        Short: "Echo anything to the screen",
        Long: `echo is for echoing anything back.
Echo works a lot like print, except it has a child command.`,
        Args: cobra.MinimumNArgs(1),
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println("Print: " + strings.Join(args, " "))
        },
    }

    var cmdTimes = &cobra.Command{
        Use:   "times [# times] [string to echo]",
        Short: "Echo anything to the screen more times",
        Long: `echo things multiple times back to the user by providing
a count and a string.`,
        Args: cobra.MinimumNArgs(1),
        Run: func(cmd *cobra.Command, args []string) {
            for i := 0; i < echoTimes; i++ {
                fmt.Println("Echo: " + strings.Join(args, " "))
            }
        },
    }

    cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")

    var rootCmd = &cobra.Command{Use: "MZR"}
    rootCmd.AddCommand(cmdPrint, cmdEcho)
    cmdEcho.AddCommand(cmdTimes)
    rootCmd.Execute()
}
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88

1 Answers1

4

The name of the executable is taken from the directory name. Rename the directory newApp to MZR. With this change, the go install command will create a executable with the name MZR. If the executable is on your path, then you can run it from the command line using MZR -h or MZR echo,

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
  • Thanks, I did it as you said and it create `MZR` (Unix executable) file in the `users/i044442/go/bin` dir (I use Mac). how should I run it ? when I double click it prints everthing as before... you write: `If the executable is on your path` how should verify it? if it is on the bin it's not sufficient ? – 07_05_GuyT Dec 17 '17 at 16:04
  • In the terminal where you typed `go run main.go echo test`, type `MZR echo`. If that does not work, then add the bin directory to your path with the command `export PATH=$PATH:users/go/bin` and try again. – Charlie Tumahai Dec 17 '17 at 16:10
  • thanks 1+,In case I need to give it to my friend which use windows should I pack it differently ? – 07_05_GuyT Dec 17 '17 at 16:12
  • 1
    in addition in case I want to provide the option to install it and use it like the following repos: https://github.com/nanopack/hoarder and also this https://github.com/golang/dep , what I should do ? – 07_05_GuyT Dec 17 '17 at 16:19
  • 2
    @shopiaT Read https://golang.org/doc/code.html. It's all explained there. – Charlie Tumahai Dec 17 '17 at 17:20
  • Thanks, I read it already and it doesn't explain my last comment ...can you please provide the reference/exampe as a answer and i'll close the question. thanks! – 07_05_GuyT Dec 17 '17 at 18:29
  • Your top-level question is answered. Feel free to close it out. As far as the new question in your comment is concerned, please read https://golang.org/doc/code.html#remote. Start a new top-level question if you are still having trouble with the `go get` command. – Charlie Tumahai Dec 17 '17 at 19:06
  • So if i understand it well , if i put the program in github and some use go get he will able to use the command like mzr echo? Pls correct me if im wrong. Thanks – 07_05_GuyT Dec 17 '17 at 21:47
  • Ive read the link u provided but not sure how external user can use my ommands like when u use go dep package. Does the link u provide answer this question too.? – 07_05_GuyT Dec 17 '17 at 21:50
  • Publish the package to a repository supported by `go get`. Run `go help importpath` for all of the details. Your friend will then be able to `go get` the package to download and install to your friend's $GOPATH/bin. – Charlie Tumahai Dec 18 '17 at 05:07