Skip to content

Commit

Permalink
added many more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ComedicChimera committed May 21, 2021
1 parent 57ee9d7 commit c5da615
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Go Report Card](https://goreportcard.com/badge/github.com/ComedicChimera/olive)](https://goreportcard.com/report/github.com/ComedicChimera/olive)
[![Build Status](https://travis-ci.org/ComedicChimera/olive.svg?branch=master)](https://travis-ci.org/ComedicChimera/olive)
[![Coverage Status](https://coveralls.io/repos/github/ComedicChimera/olive/badge.svg?branch=master)](https://coveralls.io/github/ComedicChimera/olive?branch=master)

Olive is a delightful, little argument parsing library for Golang designed to
replace the builtin *flag* package. Olive is lightweight, easy to use, and
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module olive

go 1.16

require github.com/eidolon/wordwrap v0.0.0-20161011182207-e0f54129b8bb // indirect
require (
bou.ke/monkey v1.0.2 // indirect
github.com/eidolon/wordwrap v0.0.0-20161011182207-e0f54129b8bb // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
github.com/eidolon/wordwrap v0.0.0-20161011182207-e0f54129b8bb h1:ioQwBmKdOCpMVS/bDaESqNWXIE/aw4+gsVtysCGMWZ4=
github.com/eidolon/wordwrap v0.0.0-20161011182207-e0f54129b8bb/go.mod h1:ZAPs+OyRzeVJFGvXVDVffgCzQfjg3qU9Ig8G/MU3zZ4=
24 changes: 16 additions & 8 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ func (hb *helpBuilder) buildMessage() string {
hb.buildUsageLine()

if len(hb.c.subcommands) > 0 {
hb.b.WriteString("\n\nCommands:\n\n")
hb.b.WriteString("\nCommands:\n\n")

hb.buildSubcommandsList()
}

if hb.c.primaryArg != nil {
hb.b.WriteString("\n\nPrimary Argument:\n\n")
hb.b.WriteString("\nPrimary Argument:\n\n")

hb.b.WriteString(wordwrap.Indent(
fmt.Sprintf("%s %s", hb.c.primaryArg.name, hb.c.primaryArg.desc), " ", false),
)
}

if len(hb.c.args) > 0 {
hb.b.WriteString("\n\nArguments:\n\n")
hb.b.WriteString("\nArguments:\n\n")

hb.buildArgumentsList()
}

if len(hb.c.flags) > 0 {
hb.b.WriteString("\n\nFlags:\n\n")
hb.b.WriteString("\nFlags:\n\n")

hb.buildFlagsList()
}
Expand Down Expand Up @@ -100,6 +100,8 @@ func (hb *helpBuilder) buildUsageLine() {
ub.WriteString(fmt.Sprintf("[-%s|--%s] ", flag.shortName, flag.name))
}

ub.WriteRune('\n')

hb.b.WriteString(wordwrap.Indent(ub.String(), " ", true))
}

Expand All @@ -123,6 +125,8 @@ func (hb *helpBuilder) buildSubcommandsList() {
" "+cmd.Name+strings.Repeat(" ", maxCmdNameColLength-len(cmd.Name)),
false,
))

hb.b.WriteRune('\n')
}
}

Expand All @@ -144,13 +148,15 @@ func (hb *helpBuilder) buildArgumentsList() {
hb.b.WriteString(wordwrap.Indent(
wdesc(arg.Description()),
fmt.Sprintf(
" -%s, --%s%s",
arg.Name(),
" -%s, --%s%s ",
arg.ShortName(),
arg.Name(),
strings.Repeat(" ", maxArgNameColLength-len(arg.Name())-len(arg.ShortName())-5),
),
false,
))

hb.b.WriteRune('\n')
}
}

Expand All @@ -172,12 +178,14 @@ func (hb *helpBuilder) buildFlagsList() {
hb.b.WriteString(wordwrap.Indent(
wdesc(flag.desc),
fmt.Sprintf(
" -%s, --%s%s",
flag.name,
" -%s, --%s%s ",
flag.shortName,
flag.name,
strings.Repeat(" ", maxFlagNameColLength-len(flag.name)-len(flag.shortName)-5),
),
false,
))

hb.b.WriteRune('\n')
}
}
23 changes: 16 additions & 7 deletions olive.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ func ParseArgs(cli *Command, args []string) (*ArgParseResult, error) {
// AddSubcommand adds a subcommand to the command
func (c *Command) AddSubcommand(name, desc string, helpEnabled bool) *Command {
if c.primaryArg != nil {
log.Fatalf("Command `%s` cannot both take a primary argument and have subcommands", c.Name)
log.Fatalf("command `%s` cannot both take a primary argument and have subcommands", c.Name)
}

if _, ok := c.subcommands[name]; ok {
log.Fatalf("multiple subcommands named `%s`", name)
}

subc := newCommand(name, desc, helpEnabled)
Expand All @@ -88,7 +92,7 @@ func (c *Command) AddSubcommand(name, desc string, helpEnabled bool) *Command {
// AddPrimaryArg adds a primary argument to the command
func (c *Command) AddPrimaryArg(name, desc string) {
if len(c.subcommands) > 0 {
log.Fatalf("Command `%s` cannot both take a primary argument and have subcommands", c.Name)
log.Fatalf("command `%s` cannot both take a primary argument and have subcommands", c.Name)
}

c.primaryArg = &PrimaryArgument{name: name, desc: desc}
Expand Down Expand Up @@ -209,16 +213,16 @@ func (c *Command) EnableHelp() {

// DisableHelp disables the help flag (`--help` or `-h`).
func (c *Command) DisableHelp() {
if _, ok := c.args["help"]; ok {
delete(c.args, "help")
delete(c.argsByShortName, "h")
if _, ok := c.flags["help"]; ok {
delete(c.flags, "help")
delete(c.flagsByShortName, "h")
}
}

// -----------------------------------------------------------------------------

// GetFlag checks if a flag has been set during argument parsing
func (apr *ArgParseResult) GetFlag(name string) bool {
// HasFlag checks if a flag has been set during argument parsing
func (apr *ArgParseResult) HasFlag(name string) bool {
_, ok := apr.flags[name]
return ok
}
Expand All @@ -239,3 +243,8 @@ func (apr *ArgParseResult) Subcommand() (string, *ArgParseResult, bool) {
func (c *Command) Help() {
fmt.Println(getHelpMessage(c))
}

// HelpMessage returns the stringified help message for a given command
func (c *Command) HelpMessage() string {
return getHelpMessage(c)
}
Loading

0 comments on commit c5da615

Please sign in to comment.