Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage seems inconsistent between WriteHelp and WriteManPage #264

Open
chipaca opened this issue Jul 20, 2018 · 3 comments
Open

Usage seems inconsistent between WriteHelp and WriteManPage #264

chipaca opened this issue Jul 20, 2018 · 3 comments

Comments

@chipaca
Copy link
Contributor

chipaca commented Jul 20, 2018

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.

@wdscxsj
Copy link

wdscxsj commented Aug 4, 2021

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

@chipaca
Copy link
Contributor Author

chipaca commented Aug 5, 2021

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

@suntong
Copy link

suntong commented Jan 14, 2022

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, :( :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants