Sentinel is getting too crowded with flags; maybe it’s time to introduce subcommands:
Opened this issue · 1 comments
v0lkan commented
package main
import (
"fmt"
"os"
"github.com/akamensky/argparse"
)
func main() {
parser := argparse.NewParser("program", "A program")
if len(os.Args) <= 1 {
fmt.Printf("No command provided. Exiting.\n")
os.Exit(1)
}
switch os.Args[1] {
case "command1":
// Define arguments
arg1 := parser.String("a", "argument", &argparse.Options{Required: false, Help: "Argument for command1"})
// Parse
err := parser.Parse(os.Args[2:])
if err != nil {
fmt.Print(parser.Usage(err))
}
// Use your arguments
fmt.Printf("command1, arg1: %s\n", *arg1)
case "command2":
// Similar to command1 but with different args
default:
fmt.Printf("Unknown command: %s\n", os.Args[1])
os.Exit(1)
}
}
v0lkan commented
But, that will also remove the ability to auto-generate help text, so we may need to manually wire that in:
case "command1":
arg1 := parser.String("a", "argument", &argparse.Options{Required: false, Help: "Argument for command1"})
err := parser.Parse(os.Args[2:])
if err != nil {
fmt.Print(parser.Usage(err))
}
fmt.Printf("command1, arg1: %s\n", *arg1)
// manually check for help flag
for _, arg := range os.Args[2:] {
if arg == "-h" || arg == "--help" {
fmt.Println("Help for command1: [detailed explanation]")
}
}
Or alternatively we can use something like https://cobra.dev, but that would require a larger rewrite.
I’d rather keep things simple and “feeel the pain” for a while.
If this effort turn into an uphill battle, there’s always an option to switch the libary.
argparse is lightweight and simpler, which also means less dependencies and more security.