go-flags icon indicating copy to clipboard operation
go-flags copied to clipboard

Usage seems inconsistent between WriteHelp and WriteManPage

Open chipaca opened this issue 6 years ago • 3 comments

The output produced by WriteHelp and WriteManPage is inconsistent, and in particular WriteHelp seems wrong, when a command provides Usage().

Given

package main

import (
	"fmt"
	"os"

	"github.com/jessevdk/go-flags"
)

type Opts struct {
	Manpage bool `long:"man" hidden:"true"`
}

type CmdOpts struct {
	Positional struct {
		Foo string `positional-arg-name:"<foo>"`
	} `positional-args:"yes"`
}

func (*CmdOpts) Usage() string { return "|<- this is the output of foo's Usage() ->|" }

func main() {
	var opts Opts
	p := flags.NewParser(&opts, 0)
	p.Usage = "This is the toplevel usage."
	p.ShortDescription = "toplevel short desc."
	p.LongDescription = "This is the toplevel long description."

	var cmdOpts CmdOpts
	_, err := p.AddCommand("foo", "foo short desc", "this is the foo's long description", &cmdOpts)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	_, err = p.Parse()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if opts.Manpage {
		p.WriteManPage(os.Stdout)
	} else {
		p.WriteHelp(os.Stdout)
	}
}

then in the manpage output, usage of the foo command includes some autogenerated toplevel usage followed by foo's usage,

$ ./test foo --man  | COLUMNS=80 man -l - | cat
troff: automatically ending diversion `an-div' on exit
test(1)                     General Commands Manual                    test(1)

NAME
       test - toplevel short desc.

SYNOPSIS
       test This is the toplevel usage.

DESCRIPTION
       This is the toplevel long description.

OPTIONS
   Application Options
COMMANDS
   foo
       foo short desc

       this is the foo's long description

       Usage: test [OPTIONS] foo |<- this is the output of foo's Usage() ->|

$ 

but in the help output, usage of the foo command shows the manual toplevel usage, the command's usage, and some autogenerated usage bits:

$ ./test foo
Usage:
  test This is the toplevel usage. foo |<- this is the output of foo's Usage() ->| [<foo>]

this is the foo's long description
$ 

These can't be both right -- in fact I think they might both be wrong, in that I suspect what's needed is for there to be two different toplevel usages (one when used as part of a command usage, one standalone), and then the usage of a command is just the toplevel per-command usage + the command usage itself.

If that sounds right to you I could work on a patch for this.

chipaca avatar Jul 20 '18 07:07 chipaca

Thanks! Usage is broken indeed. Any progress with the patch?

wdscxsj avatar Aug 04 '21 09:08 wdscxsj

I heard nothing back from the project owner, so I didn't write a patch.

chipaca avatar Aug 05 '21 14:08 chipaca

Is this an abandoned project, since 2018? Such an important feature has been broken for over 3 years. Gee, I should have checked here before I investigate my time in it, :( :(

suntong avatar Jan 14 '22 22:01 suntong