aboutsummaryrefslogtreecommitdiffstats
path: root/sym.go
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2025-10-23 21:19:00 -0700
committerRose Hogenson <rosehogenson@posteo.net>2025-10-23 21:19:00 -0700
commit9a08b03b83ff28a26b0c20ccebc178df29cf7312 (patch)
treeca2d39e668737634f1165be2c8942fc6aa124f30 /sym.go
parent3c2c07a69daf75d377ba196c28524c0fc6a16db6 (diff)
downloadsym-9a08b03b83ff28a26b0c20ccebc178df29cf7312.tar.zst
Use one binary with subcommands
Diffstat (limited to 'sym.go')
-rw-r--r--sym.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/sym.go b/sym.go
new file mode 100644
index 0000000..2405feb
--- /dev/null
+++ b/sym.go
@@ -0,0 +1,48 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "path/filepath"
+)
+
+type subcommand interface {
+ registerFlags(*flag.FlagSet)
+ run(...string) error
+}
+
+func whichSubcommand(name string) (subcommand, bool) {
+ switch filepath.Base(name) {
+ case "enc":
+ return &defaultEncryptOptions, true
+ case "dec":
+ return &defaultDecryptOptions, true
+ default:
+ return nil, false
+ }
+}
+
+func run(args []string) error {
+ cmd, ok := whichSubcommand(args[0])
+ if !ok {
+ if len(args) < 2 {
+ return fmt.Errorf("missing subcommand")
+ }
+ cmd, ok = whichSubcommand(args[1])
+ if !ok {
+ return fmt.Errorf("invalid subcommand %q", args[1])
+ }
+ args = args[1:]
+ }
+ cmd.registerFlags(flag.CommandLine)
+ flag.CommandLine.Parse(args[1:])
+ return cmd.run(flag.Args()...)
+}
+
+func main() {
+ if err := run(os.Args); err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+}