Skip to content

Commit

Permalink
[usage] Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 28, 2024
1 parent 02adede commit 5dbeb81
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- `[knf]` Added helper `Q`
- `[fmtc]` Code refactoring
- `[usage]` Code refactoring

### [13.9.1](https://kaos.sh/ek/13.9.1)

Expand Down
48 changes: 28 additions & 20 deletions usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ func NewInfo(args ...string) *Info {

// AddGroup adds new command group
func (i *Info) AddGroup(group string) {
if i == nil {
if i == nil || group == "" {
return
}

i.curGroup = group
}

// AddCommand adds command (name, description, args)
func (i *Info) AddCommand(a ...string) *Command {
if i == nil || len(a) < 2 {
// AddCommand adds command
func (i *Info) AddCommand(name, desc string, args ...string) *Command {
if i == nil || name == "" || desc == "" {
return nil
}

Expand All @@ -198,9 +198,9 @@ func (i *Info) AddCommand(a ...string) *Command {
}

cmd := &Command{
Name: a[0],
Desc: a[1],
Args: a[2:],
Name: name,
Desc: desc,
Args: args,
Group: group,
info: i,
}
Expand All @@ -211,17 +211,17 @@ func (i *Info) AddCommand(a ...string) *Command {
}

// AddOption adds option (name, description, args)
func (i *Info) AddOption(a ...string) *Option {
if i == nil || len(a) < 2 {
func (i *Info) AddOption(name, desc string, args ...string) *Option {
if i == nil || name == "" || desc == "" {
return nil
}

long, short := parseOptionName(a[0])
long, short := parseOptionName(name)
opt := &Option{
Long: long,
Short: short,
Desc: a[1],
Arg: strings.Join(a[2:], " "),
Desc: desc,
Arg: strings.Join(args, " "),
info: i,
}

Expand All @@ -231,25 +231,33 @@ func (i *Info) AddOption(a ...string) *Option {
}

// AddExample adds example of application usage
func (i *Info) AddExample(a ...string) {
if i == nil || len(a) == 0 {
func (i *Info) AddExample(cmd string, desc ...string) {
if i == nil || cmd == "" {
return
}

a = append(a, "")
var cmdDesc string

if len(desc) != 0 {
cmdDesc = desc[0]
}

i.Examples = append(i.Examples, &Example{a[0], a[1], false, i})
i.Examples = append(i.Examples, &Example{cmd, cmdDesc, false, i})
}

// AddRawExample adds example of application usage without command prefix
func (i *Info) AddRawExample(a ...string) {
if i == nil || len(a) == 0 {
func (i *Info) AddRawExample(cmd string, desc ...string) {
if i == nil || cmd == "" {
return
}

a = append(a, "")
var cmdDesc string

if len(desc) != 0 {
cmdDesc = desc[0]
}

i.Examples = append(i.Examples, &Example{a[0], a[1], true, i})
i.Examples = append(i.Examples, &Example{cmd, cmdDesc, true, i})
}

// AddSpoiler adds spoiler
Expand Down
16 changes: 9 additions & 7 deletions usage/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,28 @@ func (s *UsageSuite) TestUsage(c *C) {

info.AddSpoiler("This is usage of spoiler with {#240}c{#241}o{#242}l{#243}o{#244}r{#245}s {#246}s{#247}u{#248}p{#249}p{#250}o{#251}r{#252}t{!}")

info.AddCommand() // will be ignored
info.AddCommand("", "") // will be ignored
info.AddCommand("test", "") // will be ignored
info.AddCommand("print", "Print command")

info.AddGroup("Command group")

info.AddCommand("read")
info.AddCommand("read", "Read command")
info.AddCommand("read1", "Read command with arguments", "arg1", "arg2")
info.AddCommand("read2", "Read command with optional argument and very very very {*b}long{!} and {c}colored{!} description", "?arg")

info.AddOption("t:test")
info.AddOption("", "") // will be ignored
info.AddOption("test", "") // will be ignored
info.AddOption("t:test", "Test option ")
info.AddOption("test1", "Test option with argument", "arg")
info.AddOption("test2", "Test option with optional argument and very very very {*b}long{!} and {c}colored{!} description", "?arg")

info.BoundOptions("read", "t:test", "test1")

info.AddExample() // will be ignored
info.AddExample("") // will be ignored
info.AddExample("abc")
info.AddExample("abc", "Example with very long description that not fits default 88 symbols limit and link https://domain.com/#EC103814B9CCB1E305CE20D6A25E681D3735D2301D5BB631B8DFA0ABB2394A99631B8DFA0ABB2394A99")
info.AddRawExample() // will be ignored
info.AddRawExample("") // will be ignored
info.AddRawExample("echo 123 | myapp")
info.AddRawExample("echo 123 | myapp", "Example with description")

Expand Down Expand Up @@ -184,9 +185,10 @@ func (s *UsageSuite) TestVersionInfo(c *C) {

func (s *UsageSuite) TestNils(c *C) {
var i *Info

c.Assert(func() { i.AddGroup("test") }, NotPanics)
c.Assert(func() { i.AddCommand("test") }, NotPanics)
c.Assert(func() { i.AddOption("test") }, NotPanics)
c.Assert(func() { i.AddCommand("test", "test") }, NotPanics)
c.Assert(func() { i.AddOption("test", "test") }, NotPanics)
c.Assert(func() { i.AddExample("test") }, NotPanics)
c.Assert(func() { i.AddRawExample("test") }, NotPanics)
c.Assert(func() { i.AddSpoiler("test") }, NotPanics)
Expand Down

0 comments on commit 5dbeb81

Please sign in to comment.