mkideal / cli

CLI - A package for building command line app with go
MIT License
730 stars 43 forks source link

Printing self help #57

Closed suntong closed 4 years ago

suntong commented 4 years ago

Hi @mkideal,

Given a program, say https://github.com/mkideal/cli/blob/master/_examples/002-flag/main.go, if I want to do further logical process of the parameters, how do I print a self help if the given parameter is invalid.

Again, using https://github.com/mkideal/cli/blob/master/_examples/002-flag/main.go as the example, how to do:

 if argv.Port < 1024 { print self help }

where the self help is the output running the program with -h:

Options:

  -h, --help     display help information
  -p, --port     short and long format flags both are supported
  -x             boolean type
  -y             boolean type, too

thx!

suntong commented 4 years ago

Same question when-using/for child-command: https://github.com/mkideal/cli#example-8-child-command

mkideal commented 4 years ago

You can print help message directly:

if argv.Port < 1024 {
    ctx.String("Print yourself help information\n")
}
suntong commented 4 years ago

yeah, but I want to print a self help, "where the self help is the output of running the program with -h"

not construction it again myself.

mkideal commented 4 years ago

You can print help message as below:

if argv.Port < 1024 {
    ctx.WriteUsage()
}
suntong commented 4 years ago

Oh,

Thought it was https://godoc.org/github.com/mkideal/cli#Context.Usage, but it's actually https://godoc.org/github.com/mkideal/cli#Context.WriteUsage

Thanks