Skip to content

Commit

Permalink
Merge pull request #10 from sachaos/feature/use-pflag
Browse files Browse the repository at this point in the history
Use spf13/pflag to parse arguments
  • Loading branch information
sachaos authored Aug 21, 2021
2 parents baf7f65 + 655b716 commit 70880b8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 59 deletions.
86 changes: 38 additions & 48 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"strconv"
"time"

"github.com/spf13/pflag"
)

type Arguments struct {
Expand All @@ -27,68 +29,55 @@ var (
)

func parseArguments(args []string) (*Arguments, error) {
argument := Arguments{
interval: 2 * time.Second,
var argument Arguments

var intervalStr string

flagSet := pflag.NewFlagSet("", pflag.ExitOnError)
flagSet.StringVarP(&intervalStr, "interval", "n", "2s", "seconds to wait between updates")
flagSet.BoolVarP(&argument.isPrecise, "precise", "p", false, "attempt run command in precise intervals")
flagSet.BoolVarP(&argument.isClockwork, "clockwork", "c", false, "run command in precise intervals forcibly")
flagSet.BoolVar(&argument.isDebug, "debug", false, "")
flagSet.BoolVarP(&argument.isDiff, "differences", "d", false, "highlight changes between updates")
flagSet.BoolVarP(&argument.isNoTitle, "no-title", "t", false, "turn off header")
flagSet.BoolVarP(&argument.isHelp, "help", "h", false, "display this help and exit")
flagSet.BoolVarP(&argument.isVersion, "version", "v", false, "output version information and exit")

flagSet.SetInterspersed(false)

if err := flagSet.Parse(args); err != nil {
return &argument, err
}
var err error

LOOP:
for len(args) != 0 {
arg := args[0]
args = args[1:]

switch arg {
case "-n", "--interval":
if len(args) == 0 {
return nil, errors.New("-n or --interval require argument")
}
interval := args[0]
args = args[1:]
argument.interval, err = time.ParseDuration(interval)
if err != nil {
seconds, err := strconv.Atoi(interval)
if err != nil {
return nil, err
}
argument.interval = time.Duration(seconds) * time.Second
}
case "-p", "--precise":
argument.isPrecise = true
case "-c", "--clockwork":
argument.isClockwork = true
case "--debug":
argument.isDebug = true
case "-d", "--differences":
argument.isDiff = true
case "-t", "--no-title":
argument.isNoTitle = true
case "-h", "--help":
argument.isHelp = true
case "-v", "--version":
argument.isVersion = true
default:
args = append([]string{arg}, args...)
break LOOP

interval, err := time.ParseDuration(intervalStr)
if err != nil {
intervalFloat, err := strconv.ParseFloat(intervalStr, 64)
if err != nil {
return &argument, err
}
interval = time.Duration(intervalFloat * float64(time.Second))
}
argument.interval = interval

if len(args) == 0 {
return &argument, NoCommand
if interval < 10 * time.Millisecond {
return nil, IntervalTooSmall
}

if argument.interval < 10*time.Millisecond {
return nil, IntervalTooSmall
rest := flagSet.Args()

if len(rest) == 0 {
return &argument, NoCommand
}

argument.cmd = args[0]
argument.args = args[1:]
argument.cmd = rest[0]
argument.args = rest[1:]

return &argument, nil
}

func help() {
fmt.Println(`
Viddy well, gopher. viddy well.
Viddy well, gopher. Viddy well.
Usage:
viddy [options] command
Expand All @@ -103,3 +92,4 @@ Options:
-h, --help display this help and exit
-v, --version output version information and exit`)
}

36 changes: 35 additions & 1 deletion args_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func Test_parseArguments(t *testing.T) {
Expand Down Expand Up @@ -35,6 +36,39 @@ func Test_parseArguments(t *testing.T) {
args: []string{"-l"},
},
},
{
name: "-n1 tail -n 1 hoge",
args: []string{"-n1", "tail", "-n", "1", "hoge"},
exp: &Arguments{
interval: 1 * time.Second,
isPrecise: false,
isClockwork: false,
cmd: "tail",
args: []string{"-n", "1", "hoge"},
},
},
{
name: "tail -n 1 hoge",
args: []string{"tail", "-n", "1", "hoge"},
exp: &Arguments{
interval: 2 * time.Second,
isPrecise: false,
isClockwork: false,
cmd: "tail",
args: []string{"-n", "1", "hoge"},
},
},
{
name: "-n 0.5 ls",
args: []string{"-n", "0.5", "ls"},
exp: &Arguments{
interval: 500 * time.Millisecond,
isPrecise: false,
isClockwork: false,
cmd: "ls",
args: []string{},
},
},
{
name: "invalid interval",
args: []string{"-n", "1ms", "ls", "-l"},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ require (
github.com/gdamore/tcell/v2 v2.3.3
github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2
github.com/sergi/go-diff v1.2.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
Expand Down
19 changes: 9 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ var version string

func main() {
arguments, err := parseArguments(os.Args[1:])
if err == NoCommand {
if arguments.isHelp {
help()
os.Exit(0)
}

if arguments.isVersion {
fmt.Printf("viddy version: %s\n", version)
os.Exit(0)
}
if arguments.isHelp {
help()
os.Exit(0)
}

if arguments.isVersion {
fmt.Printf("viddy version: %s\n", version)
os.Exit(0)
}

if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}


var mode ViddyIntervalMode
switch {
case arguments.isPrecise:
Expand Down

0 comments on commit 70880b8

Please sign in to comment.