fix(monorel): follow CLI flag conventions

This commit is contained in:
AJ ONeal 2026-09-08 15:10:21 -06:00
parent e82e3979d2
commit 115e92dff9
No known key found for this signature in database

View File

@ -12,6 +12,7 @@ package main
import ( import (
"bufio" "bufio"
"errors"
"flag" "flag"
"fmt" "fmt"
"go/parser" "go/parser"
@ -20,6 +21,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime/debug"
"slices" "slices"
"sort" "sort"
"strconv" "strconv"
@ -43,7 +45,26 @@ var (
// printVersion displays the version, commit, and build date. // printVersion displays the version, commit, and build date.
func printVersion(w io.Writer) { func printVersion(w io.Writer) {
_, _ = fmt.Fprintf(w, "%s v%s %s (%s)\n", name, version, commit[:7], date) buildCommit, buildDate := commit, date
if buildCommit == "0000000" {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if len(setting.Value) >= 7 {
buildCommit = setting.Value[:7]
}
case "vcs.time":
buildDate = setting.Value
case "vcs.modified":
if setting.Value == "true" {
buildDate += "+dirty"
}
}
}
}
}
_, _ = fmt.Fprintf(w, "%s v%s %s (%s)\n", name, version, buildCommit, buildDate)
_, _ = fmt.Fprintf(w, "%s\n", desc) _, _ = fmt.Fprintf(w, "%s\n", desc)
_, _ = fmt.Fprintf(w, "Copyright (C) %s %s\n", licenseYear, licenseOwner) _, _ = fmt.Fprintf(w, "Copyright (C) %s %s\n", licenseYear, licenseOwner)
_, _ = fmt.Fprintf(w, "Licensed under %s\n", licenseType) _, _ = fmt.Fprintf(w, "Licensed under %s\n", licenseType)
@ -166,6 +187,27 @@ func main() {
} }
} }
func parseSubcommandFlags(fs *flag.FlagSet, args []string) {
if len(args) > 0 {
switch args[0] {
case "-V", "-version", "--version", "version":
printVersion(os.Stdout)
os.Exit(0)
case "help", "-help", "--help":
printVersion(os.Stdout)
fmt.Fprintln(os.Stdout, "")
fs.Usage()
os.Exit(0)
}
}
if err := fs.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
os.Exit(0)
}
os.Exit(2)
}
}
func usage() { func usage() {
fmt.Fprintln(os.Stderr, "Usage:") fmt.Fprintln(os.Stderr, "Usage:")
fmt.Fprintln(os.Stderr, " monorel <subcommand> [options] <binary-path>...") fmt.Fprintln(os.Stderr, " monorel <subcommand> [options] <binary-path>...")
@ -185,7 +227,7 @@ func usage() {
// ── Subcommand: release ──────────────────────────────────────────────────── // ── Subcommand: release ────────────────────────────────────────────────────
func runRelease(args []string) { func runRelease(args []string) {
fs := flag.NewFlagSet("monorel release", flag.ExitOnError) fs := flag.NewFlagSet("monorel release", flag.ContinueOnError)
var showVersion bool var showVersion bool
var recursive, all, dryRun, yes, force, draft, prerelease bool var recursive, all, dryRun, yes, force, draft, prerelease bool
var almostAll, ios, androidNDK bool var almostAll, ios, androidNDK bool
@ -214,11 +256,7 @@ func runRelease(args []string) {
fmt.Fprintln(os.Stderr, "") fmt.Fprintln(os.Stderr, "")
fs.PrintDefaults() fs.PrintDefaults()
} }
if len(args) > 0 && args[0] == "-V" { parseSubcommandFlags(fs, args)
printVersion(os.Stdout)
os.Exit(0)
}
_ = fs.Parse(args)
if showVersion { if showVersion {
printVersion(os.Stdout) printVersion(os.Stdout)
os.Exit(0) os.Exit(0)
@ -261,7 +299,7 @@ func runRelease(args []string) {
// ── Subcommand: bump ─────────────────────────────────────────────────────── // ── Subcommand: bump ───────────────────────────────────────────────────────
func runBump(args []string) { func runBump(args []string) {
fs := flag.NewFlagSet("monorel bump", flag.ExitOnError) fs := flag.NewFlagSet("monorel bump", flag.ContinueOnError)
var showVersion bool var showVersion bool
var component string var component string
var recursive, all, force, dryRun bool var recursive, all, force, dryRun bool
@ -287,11 +325,7 @@ func runBump(args []string) {
fmt.Fprintln(os.Stderr, "") fmt.Fprintln(os.Stderr, "")
fs.PrintDefaults() fs.PrintDefaults()
} }
if len(args) > 0 && args[0] == "-V" { parseSubcommandFlags(fs, args)
printVersion(os.Stdout)
os.Exit(0)
}
_ = fs.Parse(args)
if showVersion { if showVersion {
printVersion(os.Stdout) printVersion(os.Stdout)
os.Exit(0) os.Exit(0)
@ -364,7 +398,7 @@ func runBump(args []string) {
// ── Subcommand: init ─────────────────────────────────────────────────────── // ── Subcommand: init ───────────────────────────────────────────────────────
func runInit(args []string) { func runInit(args []string) {
fs := flag.NewFlagSet("monorel init", flag.ExitOnError) fs := flag.NewFlagSet("monorel init", flag.ContinueOnError)
var showVersion bool var showVersion bool
var recursive, all, dryRun, cmd bool var recursive, all, dryRun, cmd bool
var almostAll, ios, androidNDK bool var almostAll, ios, androidNDK bool
@ -394,11 +428,7 @@ func runInit(args []string) {
fmt.Fprintln(os.Stderr, "") fmt.Fprintln(os.Stderr, "")
fs.PrintDefaults() fs.PrintDefaults()
} }
if len(args) > 0 && args[0] == "-V" { parseSubcommandFlags(fs, args)
printVersion(os.Stdout)
os.Exit(0)
}
_ = fs.Parse(args)
if showVersion { if showVersion {
printVersion(os.Stdout) printVersion(os.Stdout)
os.Exit(0) os.Exit(0)