Implemented basic useprog command.
This commit is contained in:
parent
3e2ee939c4
commit
aac4f7f5cd
2 changed files with 37 additions and 0 deletions
|
@ -24,6 +24,7 @@ import(
|
|||
"github.com/surdeus/goblin/src/tool/ftest"
|
||||
"github.com/surdeus/goblin/src/tool/grange"
|
||||
"github.com/surdeus/goblin/src/tool/in"
|
||||
"github.com/surdeus/goblin/src/tool/useprog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -50,6 +51,7 @@ func main() {
|
|||
"ftest" : mtool.Tool{ftest.Run, "filter files by specified features"},
|
||||
"range" : mtool.Tool{grange.Run, "too lazy"},
|
||||
"in" : mtool.Tool{in.Run, "filter strings from stdin that aren not in arguments"},
|
||||
"useprog" : mtool.Tool{useprog.Run, "print the name of the first existing program in arg list"},
|
||||
}
|
||||
|
||||
mtool.Main("goblin", tools)
|
||||
|
|
35
src/tool/useprog/main.go
Normal file
35
src/tool/useprog/main.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package useprog
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"fmt"
|
||||
"flag"
|
||||
)
|
||||
|
||||
func Run(args []string) {
|
||||
arg0 := args[0]
|
||||
args = args[1:]
|
||||
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
|
||||
flagSet.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "usage: %s <prog1> [prog2, ...prog3]\n", arg0)
|
||||
flagSet.PrintDefaults()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
flagSet.Parse(args)
|
||||
args = flagSet.Args()
|
||||
|
||||
if len(args) == 0 {
|
||||
flagSet.Usage()
|
||||
}
|
||||
|
||||
for _, v := range args {
|
||||
_, err := exec.LookPath(v)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
fmt.Printf("%s", v)
|
||||
break
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue