Skip to content

Commit

Permalink
refactor: use source directory name for default root command name
Browse files Browse the repository at this point in the history
  • Loading branch information
steverusso committed May 19, 2024
1 parent 54f2600 commit e2365b9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions examples/posargs/clap.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ func numError(err error) error {
}

func (*mycli) UsageHelp() string {
return `mycli - Print a few positional args
return `posargs - Print a few positional args
usage:
mycli [options] <f32> <text> <u16>
posargs [options] <f32> <text> <u16>
options:
-h Show this help message
Expand All @@ -142,6 +142,6 @@ func (c *mycli) Parse(args []string) {
}
_, err := p.parse(args)
if err != nil {
clapFatalf("mycli", err.Error())
clapFatalf("posargs", err.Error())
}
}
6 changes: 3 additions & 3 deletions examples/simple/clap.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func (v *clapString) Set(s string) error {
}

func (*mycli) UsageHelp() string {
return `mycli - Print a string with the option to make it uppercase
return `simple - Print a string with the option to make it uppercase
usage:
mycli [options] <input>
simple [options] <input>
options:
-upper Make the input string all uppercase
Expand All @@ -119,6 +119,6 @@ func (c *mycli) Parse(args []string) {
}
_, err := p.parse(args)
if err != nil {
clapFatalf("mycli", err.Error())
clapFatalf("simple", err.Error())
}
}
6 changes: 3 additions & 3 deletions examples/simple_env/clap.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ func numError(err error) error {
}

func (*mycli) UsageHelp() string {
return `mycli - Print a string with a prefix
return `simple_env - Print a string with a prefix
usage:
mycli [options] [input]
simple_env [options] [input]
options:
-prefix <arg> The value to prepend to the input string [$MY_PREFIX]
Expand All @@ -151,6 +151,6 @@ func (c *mycli) Parse(args []string) {
}
_, err := p.parse(args)
if err != nil {
clapFatalf("mycli", err.Error())
clapFatalf("simple_env", err.Error())
}
}
9 changes: 8 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"regexp"
"strings"
)
Expand Down Expand Up @@ -33,6 +34,12 @@ func parse(srcDir, rootCmdTypeName string) (command, string, error) {
srcDir = "."
}

absSrcDir, err := filepath.Abs(srcDir)
if err != nil {
return command{}, "", fmt.Errorf("getting absolute source directory path: %w", err)
}
rootCmdName := filepath.Base(absSrcDir)

fset := token.NewFileSet() // positions are relative to fset
parsedPkgs, err := parser.ParseDir(fset, srcDir, nil, parser.ParseComments)
if err != nil {
Expand All @@ -59,7 +66,7 @@ func parse(srcDir, rootCmdTypeName string) (command, string, error) {
root := command{
IsRoot: true,
TypeName: rootCmdTypeName,
FieldName: rootCmdTypeName,
FieldName: rootCmdName,
Data: data,
}

Expand Down

0 comments on commit e2365b9

Please sign in to comment.