From 458f1292607d5095df9c2cd912b10857866a19b9 Mon Sep 17 00:00:00 2001 From: k1574 Date: Mon, 8 Mar 2021 04:35:25 +0500 Subject: [PATCH] noext: init. --- goblin.go | 2 ++ noext/noext.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 noext/noext.go diff --git a/goblin.go b/goblin.go index 160b7c2..cd5956c 100644 --- a/goblin.go +++ b/goblin.go @@ -17,6 +17,7 @@ import( "github.com/k1574/goblin/uniq" "github.com/k1574/goblin/quote" "github.com/k1574/goblin/urlprs" + "github.com/k1574/goblin/noext" ) func main() { @@ -40,6 +41,7 @@ func main() { "uniq" : uniq.Run, "quote" : quote.Run, "urlprs" : urlprs.Run, + "noext" : noext.Run, } if binBase := path.Base(os.Args[0]) ; binBase != "goblin" { diff --git a/noext/noext.go b/noext/noext.go new file mode 100644 index 0000000..b253c86 --- /dev/null +++ b/noext/noext.go @@ -0,0 +1,41 @@ +package noext +/* Print file path without extension. */ + +import( + "fmt" + "flag" + "os" +) + +var( + arg0 string + args []string + dot = '.' + slash = '.' +) + +func NoExt(p string) string { + l := len(p)-1 + i := l + for ; rune(p[i])!=dot ; i -= 1 { + if rune(p[i]) == slash || i==0 { + return p + } + } + return p[:i] +} + +func Run(argv []string) int { + status := 0 + arg0 = argv[0] + args = argv[1:] + flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) + flagSet.Usage = func() { + fmt.Fprintf(os.Stderr, "usage: %s [files]\n", arg0, arg0) + flagSet.PrintDefaults() + } + flagSet.Parse(args) + args = flagSet.Args() + fmt.Printf("%s", NoExt(args[0])) + return status +}