Skip to content

Commit

Permalink
Changed 'Options' to a field
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraimgm committed Sep 25, 2019
1 parent c998dbb commit ebb5f20
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 54 deletions.
7 changes: 0 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func NewApp(name, brief string) *App {
app.Cmd = newCmd()
app.Name = name
app.Brief = brief
app.options = Options{}

return &app
}
Expand All @@ -92,9 +91,3 @@ func (app *App) Run() error {
func (app *App) RunArgs(args []string) error {
return app.doRun(args)
}

// Configure change the App settings to the specified options object.
// Please see the Options struct documentation for details.
func (app *App) Configure(options Options) {
app.options = options
}
23 changes: 2 additions & 21 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ type Cmd struct {
breadcrumbs string
commands map[string]*Cmd
parentCmd *Cmd
configured bool
options Options
Options Options
}

func newCmd() *Cmd {
Expand Down Expand Up @@ -99,27 +98,9 @@ func (cmd *Cmd) CommandRun(name, brief string, callback RunCallback) {
}
}

func (cmd *Cmd) configure() {
if cmd.configured {
return
}

if cmd.parentCmd != nil {
cmd.parentCmd.configure()
cmd.options = cmd.parentCmd.options
}

cmd.setupHelp()
cmd.configured = true
}

func (cmd *Cmd) setupHelp() {
if cmd.options.OnHelp == nil {
cmd.options.OnHelp = automaticHelp
}

// no automatic '-h' flag
if cmd.options.SuppressHelpFlag {
if cmd.Options.SuppressHelpFlag {
return
}

Expand Down
6 changes: 3 additions & 3 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSubCommand(t *testing.T) {

for i, test := range tests {
app := libcmd.NewApp("app", "")
app.Configure(libcmd.Options{HelpOutput: ioutil.Discard})
app.Options.HelpOutput = ioutil.Discard
var c1, c11, c12, c2 bool

i := i //pin
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestCommandArgReuse(t *testing.T) {

for i, test := range tests {
app := libcmd.NewApp("", "")
app.Configure(libcmd.Options{HelpOutput: ioutil.Discard})
app.Options.HelpOutput = ioutil.Discard

var c1, c2 bool

Expand Down Expand Up @@ -171,7 +171,7 @@ func TestCommandArgSameName(t *testing.T) {

for i, test := range tests {
app := libcmd.NewApp("", "")
app.Configure(libcmd.Options{HelpOutput: ioutil.Discard})
app.Options.HelpOutput = ioutil.Discard
s1 := app.String("s1", "", "s1", "")
s2 := app.String("s2", "", "s2", "")

Expand Down
11 changes: 8 additions & 3 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import (
// Last, but not least, if you need to override the actual text of the help, set
// the OnHelp field of the App options instance.
func (cmd *Cmd) Help() {
cmd.configure()
output := cmd.options.HelpOutput
output := cmd.Options.HelpOutput

if output == nil {
output = os.Stdout
Expand All @@ -41,7 +40,13 @@ func (cmd *Cmd) Help() {
// PrintHelp prints the help text to the specified writer.
// It functions exactly like the Help method.
func (cmd *Cmd) PrintHelp(writer io.Writer) {
cmd.options.OnHelp(cmd, writer)
handler := automaticHelp

if cmd.Options.OnHelp != nil {
handler = cmd.Options.OnHelp
}

handler(cmd, writer)
}

func automaticHelp(cmd *Cmd, writer io.Writer) {
Expand Down
34 changes: 17 additions & 17 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ import (
"github.com/ibraimgm/libcmd"
)

func compareHelpOutput(app *libcmd.App, options libcmd.Options, args []string, goldenfile string) error {
func compareHelpOutput(app *libcmd.App, args []string, goldenfile string) error {
bytes, err := ioutil.ReadFile(goldenfile)
if err != nil {
return err
}
expected := string(bytes)

var b strings.Builder
options.HelpOutput = &b
app.Configure(options)
app.Options.HelpOutput = &b

if len(args) == 0 {
app.Help()
Expand All @@ -38,15 +37,15 @@ func compareHelpOutput(app *libcmd.App, options libcmd.Options, args []string, g
func TestBasic(t *testing.T) {
app := libcmd.NewApp("app", "some brief description")

if err := compareHelpOutput(app, libcmd.Options{}, []string{}, "testdata/basic.golden"); err != nil {
if err := compareHelpOutput(app, []string{}, "testdata/basic.golden"); err != nil {
t.Error(err)
}
}

func TestNoBrief(t *testing.T) {
app := libcmd.NewApp("app", "")

if err := compareHelpOutput(app, libcmd.Options{}, []string{}, "testdata/nobrief.golden"); err != nil {
if err := compareHelpOutput(app, []string{}, "testdata/nobrief.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -55,7 +54,7 @@ func TestNoUsage(t *testing.T) {
app := libcmd.NewApp("app", "some brief description")
app.Usage = "-"

if err := compareHelpOutput(app, libcmd.Options{}, []string{}, "testdata/nousage.golden"); err != nil {
if err := compareHelpOutput(app, []string{}, "testdata/nousage.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -64,7 +63,7 @@ func TestCustomUsage(t *testing.T) {
app := libcmd.NewApp("app", "some brief description")
app.Usage = "my custom usage text"

if err := compareHelpOutput(app, libcmd.Options{}, []string{}, "testdata/usage.golden"); err != nil {
if err := compareHelpOutput(app, []string{}, "testdata/usage.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -73,7 +72,7 @@ func TestLong(t *testing.T) {
app := libcmd.NewApp("app", "some brief description")
app.Long = "this is a very long description"

if err := compareHelpOutput(app, libcmd.Options{}, []string{}, "testdata/long.golden"); err != nil {
if err := compareHelpOutput(app, []string{}, "testdata/long.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -85,7 +84,7 @@ func TestArgs(t *testing.T) {
app.String("astring", "s", "", "sets a string value")
app.Int("aint", "i", 0, "sets a int value")

if err := compareHelpOutput(app, libcmd.Options{}, []string{"-h"}, "testdata/args.golden"); err != nil {
if err := compareHelpOutput(app, []string{"-h"}, "testdata/args.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -97,19 +96,20 @@ func TestArgsPartial(t *testing.T) {
app.String("", "s", "", "sets a string value")
app.Int("aint", "", 0, "sets a int value")

if err := compareHelpOutput(app, libcmd.Options{}, []string{"-h"}, "testdata/partial.golden"); err != nil {
if err := compareHelpOutput(app, []string{"-h"}, "testdata/partial.golden"); err != nil {
t.Error(err)
}
}

func TestArgsNoHelp(t *testing.T) {
app := libcmd.NewApp("app", "some brief description")
app.Long = "this is a very long description"
app.Options.SuppressHelpFlag = true

app.String("astring", "s", "", "sets a string value")
app.Int("aint", "i", 0, "sets a int value")

if err := compareHelpOutput(app, libcmd.Options{SuppressHelpFlag: true}, []string{}, "testdata/nohelp.golden"); err != nil {
if err := compareHelpOutput(app, []string{}, "testdata/nohelp.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -121,7 +121,7 @@ func TestDefault(t *testing.T) {
app.String("astring", "s", "somevalue", "sets a string value")
app.Int("aint", "i", 100, "sets a int value")

if err := compareHelpOutput(app, libcmd.Options{}, []string{"-h"}, "testdata/default.golden"); err != nil {
if err := compareHelpOutput(app, []string{"-h"}, "testdata/default.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -133,7 +133,7 @@ func TestCommand(t *testing.T) {
app.Command("add", "Sums two numbers.", nil)
app.Command("sub", "Subtract two numbers.", nil)

if err := compareHelpOutput(app, libcmd.Options{}, []string{"-h"}, "testdata/command.golden"); err != nil {
if err := compareHelpOutput(app, []string{"-h"}, "testdata/command.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -148,7 +148,7 @@ func TestCommandArgs(t *testing.T) {
app.Command("add", "Sums two numbers.", nil)
app.Command("sub", "Subtract two numbers.", nil)

if err := compareHelpOutput(app, libcmd.Options{}, []string{"-h"}, "testdata/commandargs.golden"); err != nil {
if err := compareHelpOutput(app, []string{"-h"}, "testdata/commandargs.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -165,7 +165,7 @@ func TestSubcommand(t *testing.T) {
})
app.Command("sub", "Subtract two numbers.", nil)

if err := compareHelpOutput(app, libcmd.Options{}, []string{"add"}, "testdata/subcommand.golden"); err != nil {
if err := compareHelpOutput(app, []string{"add"}, "testdata/subcommand.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -183,7 +183,7 @@ func TestSubcommandShallow(t *testing.T) {
})
app.Command("sub", "Subtract two numbers.", nil)

if err := compareHelpOutput(app, libcmd.Options{}, []string{"add"}, "testdata/shallow.golden"); err != nil {
if err := compareHelpOutput(app, []string{"add"}, "testdata/shallow.golden"); err != nil {
t.Error(err)
}
}
Expand All @@ -203,7 +203,7 @@ func TestSubcommandDeep(t *testing.T) {
})
app.Command("sub", "Subtract two numbers.", nil)

if err := compareHelpOutput(app, libcmd.Options{}, []string{"add", "deep"}, "testdata/deep.golden"); err != nil {
if err := compareHelpOutput(app, []string{"add", "deep"}, "testdata/deep.golden"); err != nil {
t.Error(err)
}
}
7 changes: 4 additions & 3 deletions opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (cmd *Cmd) doParse(args []string) error {
}

func (cmd *Cmd) doRun(args []string) error {
cmd.configure()
cmd.setupHelp()

if err := cmd.doParse(args); err != nil {
if cmd.errHandler != nil {
Expand All @@ -199,6 +199,7 @@ func (cmd *Cmd) doRun(args []string) error {
name := cmd.args[0]

if subCommand, ok := cmd.commands[name]; ok {
subCommand.Options = cmd.Options
if subCommand.callback != nil {
subCommand.callback(subCommand)
}
Expand All @@ -219,7 +220,7 @@ func (cmd *Cmd) runLeafCommand() error {

// check for the automatic handling of
// the '-h' and '--help' flags
if !cmd.options.SupressPrintHelpWhenSet {
if !cmd.Options.SupressPrintHelpWhenSet {
arg := cmd.shortopt["-h"]
if arg == nil {
arg = cmd.longopt["--help"]
Expand Down Expand Up @@ -247,7 +248,7 @@ func (cmd *Cmd) runLeafCommand() error {

// the last resort is to run the help if we're a "partial"
// subcommand
if !cmd.options.SuppressPrintHelpPartialCommand {
if !cmd.Options.SuppressPrintHelpPartialCommand {
cmd.Help()
}

Expand Down

0 comments on commit ebb5f20

Please sign in to comment.