noext: init.

This commit is contained in:
k1574 2021-03-08 04:35:25 +05:00
parent 9a226b8cd4
commit 458f129260
2 changed files with 43 additions and 0 deletions

View file

@ -17,6 +17,7 @@ import(
"github.com/k1574/goblin/uniq" "github.com/k1574/goblin/uniq"
"github.com/k1574/goblin/quote" "github.com/k1574/goblin/quote"
"github.com/k1574/goblin/urlprs" "github.com/k1574/goblin/urlprs"
"github.com/k1574/goblin/noext"
) )
func main() { func main() {
@ -40,6 +41,7 @@ func main() {
"uniq" : uniq.Run, "uniq" : uniq.Run,
"quote" : quote.Run, "quote" : quote.Run,
"urlprs" : urlprs.Run, "urlprs" : urlprs.Run,
"noext" : noext.Run,
} }
if binBase := path.Base(os.Args[0]) ; binBase != "goblin" { if binBase := path.Base(os.Args[0]) ; binBase != "goblin" {

41
noext/noext.go Normal file
View file

@ -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
}