Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ name and description will be used to construct the help message for the app:
func App(name, desc string) *Cli {
return &Cli{
Cmd: &Cmd{
name: name,
desc: desc,
optionsIdx: map[string]*container.Container{},
argsIdx: map[string]*container.Container{},
ErrorHandling: flag.ExitOnError,
name: name,
desc: desc,
optionsIdx: map[string]*container.Container{},
argsIdx: map[string]*container.Container{},
ErrorHandling: flag.ExitOnError,
PrintHelpOnExit: true,
},
}
}
Expand Down
7 changes: 6 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"errors"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -35,6 +36,8 @@ type Cmd struct {
// The command error handling strategy
ErrorHandling flag.ErrorHandling

PrintHelpOnExit bool

init CmdInitializer
name string
aliases []string
Expand Down Expand Up @@ -677,7 +680,9 @@ func (c *Cmd) parse(args []string, entry, inFlow, outFlow *flow.Step) error {

if err := c.fsm.Parse(args[:nargsLen]); err != nil {
fmt.Fprintf(stdErr, "Error: %s\n", err.Error())
c.PrintHelp()
if c.PrintHelpOnExit || errors.Is(err, fsm.ErrIncorrectUsage) {
c.PrintHelp()
}
c.onError(err)
return err
}
Expand Down
9 changes: 6 additions & 3 deletions internal/fsm/fsm.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package fsm

import (
"errors"
"sort"

"fmt"

"github.com/jawher/mow.cli/internal/container"
"github.com/jawher/mow.cli/internal/matcher"
"github.com/jawher/mow.cli/internal/values"
)

var (
ErrIncorrectUsage = errors.New("incorrect usage")
)

/*
State is the basic building block in the FSM.
A State can be final or not, and has transitions to other states
Expand Down Expand Up @@ -121,7 +124,7 @@ func (s *State) Parse(args []string) error {
pc := matcher.NewParseContext()
ok := s.apply(args, pc)
if !ok {
return fmt.Errorf("incorrect usage")
return ErrIncorrectUsage
}

if err := fillContainers(pc.Opts); err != nil {
Expand Down