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

Sentinel is getting too crowded with flags; maybe it’s time to introduce subcommands: #474

Open
v0lkan opened this issue Jul 6, 2023 · 1 comment

Comments

@v0lkan
Copy link
Contributor

v0lkan commented Jul 6, 2023

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 v0lkan added this to Aegis Jul 6, 2023
@v0lkan v0lkan converted this from a draft issue Jul 6, 2023
@v0lkan
Copy link
Contributor Author

v0lkan commented Jul 6, 2023

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Status: Enhancements
Development

No branches or pull requests

1 participant