mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-14 02:57:52 +08:00
Let the main package handle flags.
This commit is contained in:
parent
88ff213deb
commit
b174b990ed
65
main.go
65
main.go
|
@ -2,8 +2,69 @@
|
|||
// programming language with an extensible, friendly user interface.
|
||||
package main
|
||||
|
||||
import "github.com/elves/elvish/run"
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime/pprof"
|
||||
|
||||
"github.com/elves/elvish/shell"
|
||||
"github.com/elves/elvish/util"
|
||||
)
|
||||
|
||||
var Logger = util.GetLogger("[main] ")
|
||||
|
||||
var (
|
||||
log = flag.String("log", "", "a file to write debug log to")
|
||||
dbname = flag.String("db", "", "path to the database")
|
||||
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
||||
help = flag.Bool("help", false, "show usage help and quit")
|
||||
cmd = flag.Bool("c", false, "take first argument as a command to execute")
|
||||
)
|
||||
|
||||
func usage() {
|
||||
fmt.Println("usage: elvish [flags] [script]")
|
||||
fmt.Println("flags:")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
func main() {
|
||||
run.Main()
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
args := flag.Args()
|
||||
|
||||
if *help {
|
||||
usage()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if len(args) > 1 {
|
||||
usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
if *log != "" {
|
||||
err := util.SetOutputFile(*log)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
if *cpuprofile != "" {
|
||||
f, err := os.Create(*cpuprofile)
|
||||
if err != nil {
|
||||
Logger.Fatal(err)
|
||||
}
|
||||
pprof.StartCPUProfile(f)
|
||||
defer pprof.StopCPUProfile()
|
||||
}
|
||||
|
||||
sh := shell.NewShell(*dbname, *cmd)
|
||||
|
||||
var arg *string
|
||||
if len(args) == 1 {
|
||||
arg = &args[0]
|
||||
}
|
||||
|
||||
os.Exit(sh.Main(arg))
|
||||
}
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
// Package run is the entry point of elvish.
|
||||
package run
|
||||
// Package shell is the entry point of elvish.
|
||||
package shell
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime/pprof"
|
||||
"syscall"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
@ -21,59 +19,26 @@ import (
|
|||
"github.com/elves/elvish/util"
|
||||
)
|
||||
|
||||
var Logger = util.GetLogger("[main] ")
|
||||
var Logger = util.GetLogger("[shell] ")
|
||||
|
||||
var (
|
||||
log = flag.String("log", "", "a file to write debug log to")
|
||||
dbname = flag.String("db", "", "path to the database")
|
||||
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
||||
help = flag.Bool("help", false, "show usage help and quit")
|
||||
cmd = flag.Bool("c", false, "take first argument as a command to execute")
|
||||
)
|
||||
// Shell keeps flags to the shell.
|
||||
type Shell struct {
|
||||
dbname string
|
||||
cmd bool
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Println("usage: elvish [flags] [script]")
|
||||
fmt.Println("flags:")
|
||||
flag.PrintDefaults()
|
||||
func NewShell(dbname string, cmd bool) *Shell {
|
||||
return &Shell{dbname, cmd}
|
||||
}
|
||||
|
||||
// Main is the entry point of elvish.
|
||||
func Main() {
|
||||
func (sh *Shell) Main(arg *string) int {
|
||||
defer rescue()
|
||||
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
args := flag.Args()
|
||||
|
||||
if len(args) > 1 {
|
||||
usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
if *help {
|
||||
usage()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if *log != "" {
|
||||
err := util.SetOutputFile(*log)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
if *cpuprofile != "" {
|
||||
f, err := os.Create(*cpuprofile)
|
||||
if err != nil {
|
||||
Logger.Fatal(err)
|
||||
}
|
||||
pprof.StartCPUProfile(f)
|
||||
defer pprof.StopCPUProfile()
|
||||
}
|
||||
|
||||
handleUsr1AndQuit()
|
||||
logSignals()
|
||||
|
||||
ev, st := newEvalerAndStore()
|
||||
ev, st := newEvalerAndStore(sh.dbname)
|
||||
defer func() {
|
||||
err := st.Close()
|
||||
if err != nil {
|
||||
|
@ -81,17 +46,19 @@ func Main() {
|
|||
}
|
||||
}()
|
||||
|
||||
if len(args) == 1 {
|
||||
if *cmd {
|
||||
sourceTextAndPrintError(ev, "code from -c", args[0])
|
||||
if arg != nil {
|
||||
if sh.cmd {
|
||||
sourceTextAndPrintError(ev, "code from -c", *arg)
|
||||
} else {
|
||||
script(ev, args[0])
|
||||
script(ev, *arg)
|
||||
}
|
||||
} else if !sys.IsATTY(0) {
|
||||
script(ev, "/dev/stdin")
|
||||
} else {
|
||||
interact(ev, st)
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func rescue() {
|
||||
|
@ -231,15 +198,13 @@ func handleUsr1AndQuit() {
|
|||
}()
|
||||
}
|
||||
|
||||
func newEvalerAndStore() (*eval.Evaler, *store.Store) {
|
||||
func newEvalerAndStore(db string) (*eval.Evaler, *store.Store) {
|
||||
dataDir, err := store.EnsureDataDir()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Warning: cannot create data dir ~/.elvish")
|
||||
}
|
||||
|
||||
var st *store.Store
|
||||
if err == nil {
|
||||
db := *dbname
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Warning: cannot create data dir ~/.elvish")
|
||||
} else {
|
||||
if db == "" {
|
||||
db = dataDir + "/db"
|
||||
}
|
Loading…
Reference in New Issue
Block a user