From 4e43b0f8c032e6075eff1eb065887cd932729699 Mon Sep 17 00:00:00 2001 From: Derek Parker Date: Sun, 8 Mar 2015 21:26:45 -0500 Subject: [PATCH] Use subcommands instead of flags --- README.md | 4 ++-- client/cli/cli.go | 13 +++++++++---- cmd/dlv/main.go | 27 +++++++++++++++++---------- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 78dc9ec1..8b2952f1 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ The debugger can be launched in three ways: * Compile, run, and attach in one step: ``` - $ dlv -run + $ dlv run ``` * Provide the name of the program you want to debug, and the debugger will launch it for you. @@ -62,7 +62,7 @@ The debugger can be launched in three ways: * Provide the pid of a currently running process, and the debugger will attach and begin the session. ``` - $ sudo dlv -pid 44839 + $ sudo dlv attach 44839 ``` ### Breakpoints diff --git a/client/cli/cli.go b/client/cli/cli.go index 859ed53b..530c64f0 100644 --- a/client/cli/cli.go +++ b/client/cli/cli.go @@ -7,6 +7,7 @@ import ( "os/exec" "os/signal" "strings" + "strconv" sys "golang.org/x/sys/unix" @@ -18,7 +19,7 @@ import ( const historyFile string = ".dbg_history" -func Run(run bool, pid int, args []string) { +func Run(args []string) { var ( dbp *proctl.DebuggedProcess err error @@ -26,8 +27,8 @@ func Run(run bool, pid int, args []string) { ) defer line.Close() - switch { - case run: + switch args[0] { + case "run": const debugname = "debug" cmd := exec.Command("go", "build", "-o", debugname, "-gcflags", "-N -l") err := cmd.Run() @@ -40,7 +41,11 @@ func Run(run bool, pid int, args []string) { if err != nil { die(1, "Could not launch program:", err) } - case pid != 0: + case "attach": + pid, err := strconv.Atoi(args[1]) + if err != nil { + die(1, "Invalid pid", args[1]) + } dbp, err = proctl.Attach(pid) if err != nil { die(1, "Could not attach to process:", err) diff --git a/cmd/dlv/main.go b/cmd/dlv/main.go index ba83e5db..e8f59a59 100644 --- a/cmd/dlv/main.go +++ b/cmd/dlv/main.go @@ -11,6 +11,20 @@ import ( const version string = "0.5.0.beta" +var usage string = fmt.Sprintf(`Delve version %s + +flags: + -v - Print version + +Invoke with the path to a binary: + +dlv ./path/to/prog + +or use the following commands: + run - Build, run, and attach to program + attach - Attach to running process +`, version) + func init() { // We must ensure here that we are running on the same thread during // the execution of dbg. This is due to the fact that ptrace(2) expects @@ -19,19 +33,12 @@ func init() { } func main() { - var ( - pid int - run bool - printv bool - ) + var printv bool - flag.IntVar(&pid, "pid", 0, "Pid of running process to attach to.") - flag.BoolVar(&run, "run", false, "Compile program and begin debug session.") - flag.BoolVar(&printv, "v", false, "Print version number and exit.") flag.Parse() if flag.NFlag() == 0 && len(flag.Args()) == 0 { - flag.Usage() + fmt.Println(usage) os.Exit(0) } @@ -40,5 +47,5 @@ func main() { os.Exit(0) } - cli.Run(run, pid, flag.Args()) + cli.Run(os.Args[1:]) }