noext: init.
This commit is contained in:
parent
9a226b8cd4
commit
458f129260
2 changed files with 43 additions and 0 deletions
|
@ -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" {
|
||||
|
|
41
noext/noext.go
Normal file
41
noext/noext.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue