all repos — erl @ 6c854334bc9ee7338771dd0d034f3f48f0980674

Execute Reload Loop

feat: enable short and long command-line arguments

Alan Pearce
commit

6c854334bc9ee7338771dd0d034f3f48f0980674

parent

0050b8866f010cccee83caa020957b3aa199151f

4 files changed, 28 insertions(+), 11 deletions(-)

changed files
M go.modgo.mod
@@ -4,6 +4,8 @@ go 1.24.5
require github.com/fsnotify/fsnotify v1.9.0 +require github.com/jessevdk/go-flags v1.6.1 + require ( github.com/fatih/color v1.18.0 github.com/mattn/go-colorable v0.1.13 // indirect
M go.sumgo.sum
@@ -2,6 +2,8 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k= github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4= +github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
M gomod2nix.tomlgomod2nix.toml
@@ -7,6 +7,9 @@ hash = "sha256-pP5y72FSbi4j/BjyVq/XbAOFjzNjMxZt2R/lFFxGWvY="
[mod."github.com/fsnotify/fsnotify"] version = "v1.9.0" hash = "sha256-WtpE1N6dpHwEvIub7Xp/CrWm0fd6PX7MKA4PV44rp2g=" + [mod."github.com/jessevdk/go-flags"] + version = "v1.6.1" + hash = "sha256-Q5WFTgRxYio0+ay3sbQeBPKeJAFvOdiDVkaTVn3hoTA=" [mod."github.com/mattn/go-colorable"] version = "v0.1.13" hash = "sha256-qb3Qbo0CELGRIzvw7NVM1g/aayaz4Tguppk9MD2/OI8="
M main.gomain.go
@@ -2,7 +2,7 @@ package main
import ( "context" - "flag" + "errors" "io" "log" "os"
@@ -14,17 +14,18 @@ "syscall"
"time" "github.com/fsnotify/fsnotify" + "github.com/jessevdk/go-flags" "alin.ovh/erl/command" "alin.ovh/erl/state" "alin.ovh/erl/watcher" ) -var ( - Exec = flag.String("exec", "", "command to execute on file change") - Quiet = flag.Bool("quiet", false, "suppress own output") - Verbose = flag.Bool("verbose", false, "verbose output (print events)") -) +type Options struct { + Exec string `short:"x" long:"exec" description:"command to execute on file change"` + Quiet bool `short:"q" long:"quiet" description:"suppress own output"` + Verbose bool `short:"v" long:"verbose" description:"verbose output (print events)"` +} func Start(ctx context.Context, verbose *log.Logger, w watcher.Watcher, sm *state.StateMachine) { var wg sync.WaitGroup
@@ -102,11 +103,20 @@ wg.Wait()
} func main() { + var opts Options log.SetFlags(log.Lmsgprefix) - flag.Parse() + + fp := flags.NewParser(&opts, flags.Default) + args, err := fp.Parse() + if err != nil { + if errors.Is(err, flags.ErrHelp) { + os.Exit(0) + } + + log.Fatalf("failed to parse flags: %v", err) + } - program := *Exec - args := flag.Args() + program := opts.Exec if program == "" { program = "go"
@@ -132,12 +142,12 @@ )
defer cancel() copts := command.Options{} - if *Quiet { + if opts.Quiet { copts.Output = io.Discard } var logger *log.Logger - if *Verbose { + if opts.Verbose { logger = log.New(os.Stderr, "", 0) } else { logger = log.New(io.Discard, "", 0)