ctngoargs

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ctngoargs

Go Reference

Small helper to parse -- flags from a string slice and run a callback per flag.

Requirements

  • Go 1.25 or newer

Install package

go get github.com/CTNOriginals/ctngoargs

Usage

Define an ArgDefinitionList: each entry lists full flag strings in Flags (including --), optional Description / Inputs for help text, and an Action that receives the inputs after the flag until the next --.

Call ParseArgs with your definitions and something like os.Args. Unknown flags are printed to stdout and skipped.

For help strings, use GetHelpString or GetFullHelpString. To only parse a subset of flags (or exclude some), see ParseSpecificArgs.

Example

--help must call GetHelpString on the same ArgDefinitionList you pass to ParseArgs. A package-level var defs = ArgDefinitionList{...} cannot use defs inside an Action because Go reports an initialization cycle. Use one variable for the literal and another that you assign to that list before ParseArgs, and call GetHelpString on the second.

package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/CTNOriginals/ctngoargs"
)

var definitions = ctngoargs.ArgDefinitionList{
	{
		Flags: []string{"--help"},
		Description: []string{
			"List flags. Pass a name without -- for more detail on that flag.",
		},
		Inputs: ctngoargs.ArgInputs{
			{
				Name:        "flag",
				Description: []string{"Flag name without the leading --."},
			},
		},
		Action: func(inputs []string) {
			fmt.Println(clone.GetHelpString(inputs))
		},
	},
	{
		Flags: []string{"--full-help"},
		Description: []string{
			"Like --help, but include full detail for every flag.",
		},
		Action: func(_ []string) {
			fmt.Println(clone.GetFullHelpString())
		},
	},
	{
		Flags:       []string{"--echo"},
		Description: []string{"Print text after the flag until the next flag."},
		Action: func(inputs []string) {
			fmt.Println(strings.Join(inputs, " "))
		},
	},
}

var clone ctngoargs.ArgDefinitionList

func main() {
	clone = definitions
	ctngoargs.ParseArgs(definitions, os.Args)
}

MIT License. See LICENSE.md.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseArgs

func ParseArgs(definitions ArgDefinitionList, args []string)

ParseArgs groups parameters under each "--" flag and runs the matching definition's action. Arguments before the first "--" flag are ignored (e.g. the program name in os.Args).

func ParseSpecificArgs

func ParseSpecificArgs(definitions ArgDefinitionList, args []string, include bool, list ...string)

Parse specific args removes any args and their params from args if they are not included in list or if include is false, the args in list will be remove from args instead.

Types

type ArgDefinition

type ArgDefinition struct {
	Flags       []string
	Description []string

	Inputs ArgInputs
	Action func(inputs []string)
}

ArgDefinition holds flags or section labels, help text, parameter metadata, and an action. Action runs when an --arg flag equals an entry in Flags

func (ArgDefinition) String

func (this ArgDefinition) String() string

String formats this definition for display: flags, description, and nested inputs.

type ArgDefinitionList

type ArgDefinitionList []*ArgDefinition

func (ArgDefinitionList) GetDefinitionByFlag

func (this ArgDefinitionList) GetDefinitionByFlag(flag string) (def *ArgDefinition, exists bool)

GetDefinitionByFlag returns the ArgDefinition whose flags contain flag and whether it was found.

func (ArgDefinitionList) GetFullHelpString

func (this ArgDefinitionList) GetFullHelpString() string

GetFullHelpString returns detailed help for every definition.

func (ArgDefinitionList) GetHelpString

func (this ArgDefinitionList) GetHelpString(filter []string) string

GetHelpString returns formatted help for these definitions. With a non-empty filter, detailed help always uses the first name (no leading "--"); the same block is emitted once per filter entry.

type ArgInputs

type ArgInputs []InputDefinition

func (ArgInputs) String

func (this ArgInputs) String() string

String formats the inputs section for display.

type InputDefinition

type InputDefinition struct {
	Name        string
	Description []string
}

InputDefinition documents one value that can follow a flag on the command line

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL