Skip to content

Commit

Permalink
Change cli arguments parser to kingpin, add short commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sponomarev committed Sep 28, 2017
1 parent 7f181bb commit 6f420db
Show file tree
Hide file tree
Showing 39 changed files with 9,725 additions and 26 deletions.
37 changes: 11 additions & 26 deletions cmd/httplab/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/user"
"time"

"github.com/alecthomas/kingpin"
"github.com/gchaincl/httplab/ui"
"github.com/jroimartin/gocui"
"github.com/rs/cors"
Expand Down Expand Up @@ -45,38 +45,23 @@ func defaultConfigPath() string {
return u.HomeDir + "/" + path
}

func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nBindings:\n%s", ui.Bindings.Help())
}

func Version() {
fmt.Fprintf(os.Stdout, "%s\n", VERSION)
os.Exit(0)
}

func main() {
var (
port int
config string
version bool
cors bool
config = kingpin.Flag("config", "Specifies custom config path.").Short('c').String()
cors = kingpin.Flag("cors", "Enable CORS").Default("false").Bool()
port = kingpin.Flag("port", "Specifies the port where HTTPLab will bind to.").Default("false").Default("10080").Int()
bindings = kingpin.Flag("bindings", "Show keyboard bindings").Bool()
)

flag.Usage = usage
flag.IntVar(&port, "port", 10080, "Specifies the port where HTTPLab will bind to.")
flag.StringVar(&config, "config", "", "Specifies custom config path.")
flag.BoolVar(&version, "version", false, "Prints current version.")
flag.BoolVar(&cors, "cors", false, "Enable CORS.")

flag.Parse()
kingpin.Version(VERSION)
kingpin.Parse()

if version {
Version()
if *bindings {
fmt.Fprintf(os.Stderr, "\nBindings:\n%s", ui.Bindings.Help())
os.Exit(0)
}

if err := run(config, port, cors); err != nil && err != gocui.ErrQuit {
if err := run(*config, *port, *cors); err != nil && err != gocui.ErrQuit {
log.Println(err)
}
}
Expand Down
19 changes: 19 additions & 0 deletions vendor/github.com/alecthomas/kingpin/COPYING

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6f420db

Please sign in to comment.