Command Line Flags/Arguments

Hello, i want to use command line flags/argument.
Like ,

MYSOFT -g FILE

in flags.String , it will work properly but if we do not pass any argument after -g flag then it will say :

flag needs an argument: -g
Usage of ./MYSOFT:
  -g string
         (default "install")

i want that if flag arugment is empty that it should show :
Please Parse an argument to install

actually, the thing i want is, if someone run MYSOFT command then the output should like this :

Usage: <options> software

MYSOFT Options:
-i, --install, install     Install Software

 You must have the name of the software you want to install or update, so you can use this command.

and if someone run MYSOFT -i , then the output should like this :

Please enter any software, the selected software doesn't exists

and if someone run MYSOFT -i CALCULATOR

it will execute some codes

i have tried to do it also via os.Args , but in everything it doesn’t works properly.
when i have add these lines via fmt.Println :

Usage: <options> software

MYSOFT Options:
-i, --install, install     Install Software

 You must have the name of the software you want to install or update, so you can use this command.

then if i run MYSOFT -i CALCULATOR then the above string also comes

anyone know how can i do it

Yo!
maybe something like that? oh, ok, I can see a problem with my code

package main

import (
	"flag"
	"fmt"
)

type Flags struct {
	name *string
}

func parse() *Flags {
	result := &Flags{}
	result.name = flag.String("i", "default", "enter app name")
	flag.Parse()
	if *result.name == "" {
		fmt.Println("you must enter something")
	}

	return result
}

func main() {
	f := parse()
	fmt.Println(*f.name)
}

so, I think, that you need to search for custom flag parsers like this: https://github.com/cosiner/flag

Umm, it’s pretty hard and confusing

What if you change the default string from “default” to “”?

result.name = flag.String("i", "", "enter app name")

still the problem:
when you run go run . -i and don’t enter anything, the command will display default help string and exit with code 2

Oh yes, my fault. I confused passing -i with no parameter with not passing -i at all.

I guess for custom error handling, a FlagSet might be a solution

hmm, I think, that it still prints Usage() when no flag value
my code using FlagSet

package main

import (
	"flag"
	"fmt"
	"os"
)

type Flags struct {
	name *string
}

func parse() *Flags {
	result := &Flags{}
	fs := flag.NewFlagSet("arguments", flag.ContinueOnError)
	result.name = fs.String("i", "default", "enter app name")
	if err := fs.Parse(os.Args[1:]); err != nil {
		fmt.Println(err)
		// return result
	}

	if *result.name == "" {
		fmt.Println("you must enter something")
	}

	return result
}

func main() {
	f := parse()
	fmt.Println(*f.name)
}
`

Found the solution. The FlagSet struct contains a field Usage of type func(). Here you can set your own usage message, like so:

fs.Usage = func() {
	  fmt.Println("Usage: blah...")
}