From e2365b92220880b17df9c3aa0b1ba6709b6495c8 Mon Sep 17 00:00:00 2001 From: Steve Russo Date: Sun, 19 May 2024 14:32:09 -0400 Subject: [PATCH] refactor: use source directory name for default root command name --- examples/posargs/clap.gen.go | 6 +++--- examples/simple/clap.gen.go | 6 +++--- examples/simple_env/clap.gen.go | 6 +++--- parse.go | 9 ++++++++- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/posargs/clap.gen.go b/examples/posargs/clap.gen.go index 92d8167..b96c636 100644 --- a/examples/posargs/clap.gen.go +++ b/examples/posargs/clap.gen.go @@ -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] + posargs [options] options: -h Show this help message @@ -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()) } } diff --git a/examples/simple/clap.gen.go b/examples/simple/clap.gen.go index 98ef5c0..7fd7dc5 100644 --- a/examples/simple/clap.gen.go +++ b/examples/simple/clap.gen.go @@ -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] + simple [options] options: -upper Make the input string all uppercase @@ -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()) } } diff --git a/examples/simple_env/clap.gen.go b/examples/simple_env/clap.gen.go index d9bf972..839dd55 100644 --- a/examples/simple_env/clap.gen.go +++ b/examples/simple_env/clap.gen.go @@ -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 The value to prepend to the input string [$MY_PREFIX] @@ -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()) } } diff --git a/parse.go b/parse.go index ed04b06..fd131ea 100644 --- a/parse.go +++ b/parse.go @@ -6,6 +6,7 @@ import ( "go/ast" "go/parser" "go/token" + "path/filepath" "regexp" "strings" ) @@ -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 { @@ -59,7 +66,7 @@ func parse(srcDir, rootCmdTypeName string) (command, string, error) { root := command{ IsRoot: true, TypeName: rootCmdTypeName, - FieldName: rootCmdTypeName, + FieldName: rootCmdName, Data: data, }