Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Refine usage handlings
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Jul 7, 2023
1 parent 2c73688 commit 4a4476e
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions cmd/nix-headbump/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"flag"
"fmt"
"log"
Expand All @@ -17,14 +16,30 @@ var (
)

func main() {
usageError := errors.New("expected 'bump' or 'detect' subcommands")
const usage = `Usage: nix-headbump <subcommand> <flags>
$ nix-headbump detect -current
$ nix-headbump bump
$ nix-headbump -version`

detectCmd := flag.NewFlagSet("detect", flag.ExitOnError)
bumpCmd := flag.NewFlagSet("bump", flag.ExitOnError)
versionFlag := flag.Bool("version", false, "print the version of this program")
currentFlag := detectCmd.Bool("current", false, "print current nixpath without bumping")
lastFlag := detectCmd.Bool("last", false, "print git head ref without bumping")
target := detectCmd.Bool("target", false, "print which file will be bumped")

flag.Usage = func() {
// https://github.com/golang/go/issues/57059#issuecomment-1336036866
fmt.Printf("%s", usage+"\n\n")
fmt.Println("Usage of command:")
flag.PrintDefaults()
fmt.Println("")
detectCmd.Usage()
fmt.Println("")
bumpCmd.Usage()
}

flag.Parse()
if *versionFlag {
revision := commit[:7]
Expand All @@ -33,7 +48,7 @@ func main() {
}

if len(os.Args) < 2 {
fmt.Println(usageError.Error())
flag.Usage()
os.Exit(1)
}

Expand All @@ -49,7 +64,7 @@ func main() {
case "detect":
err := detectCmd.Parse(os.Args[2:])
if err != nil {
fmt.Println(usageError.Error())
flag.Usage()
}
if *target {
fmt.Println(path)
Expand All @@ -72,11 +87,11 @@ func main() {
return
}

detectCmd.PrintDefaults()
detectCmd.Usage()
case "bump":
err := bumpCmd.Parse(os.Args[2:])
if err != nil {
fmt.Println(usageError.Error())
flag.Usage()
}
last, err := nhb.GetLastVersion()
if err != nil {
Expand All @@ -85,8 +100,11 @@ func main() {
if err = nhb.Bump(path, last); err != nil {
log.Fatalf("Bumping the version has been failed: %s", err.Error())
}

bumpCmd.Usage()
default:
fmt.Println(usageError.Error())
flag.Usage()

os.Exit(1)
}
}

0 comments on commit 4a4476e

Please sign in to comment.