Skip to content
This repository was archived by the owner on Jul 25, 2023. It is now read-only.
This repository was 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

@v0lkan

Description

@v0lkan
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)
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Enhancements

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions