middleware

package
v0.0.0-...-ecd900c Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package middleware provides useful middleware for a clir.Router.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Args

func Args(cb func(as *ArgSet)) clir.Middleware

Args middleware allows you to set positional arguments on a route.

Example
package main

import (
	"os"

	"maragu.dev/clir"
	"maragu.dev/clir/middleware"
)

func main() {
	r := clir.NewRouter()

	var name *string
	r.Use(middleware.Args(func(as *middleware.ArgSet) {
		name = as.String("name", "world", "name to greet")
	}))

	r.RouteFunc("", func(ctx clir.Context) error {
		ctx.Printfln("Hello, %s!", *name)
		return nil
	})

	_ = r.Run(clir.Context{
		Args: []string{"Funky Person"},
		Out:  os.Stdout,
	})
}
Output:
Hello, Funky Person!

func Flags

func Flags(cb func(fs *flag.FlagSet)) clir.Middleware

Flags middleware allows you to set flags on a route.

Example
package main

import (
	"flag"
	"os"

	"maragu.dev/clir"
	"maragu.dev/clir/middleware"
)

func main() {
	r := clir.NewRouter()

	var v *bool
	r.Use(middleware.Flags(func(fs *flag.FlagSet) {
		v = fs.Bool("v", false, "verbose output")
	}))

	r.RouteFunc("", func(ctx clir.Context) error {
		if *v {
			ctx.Println("Hello!")
		}
		return nil
	})

	_ = r.Run(clir.Context{
		Args: []string{"-v"},
		Out:  os.Stdout,
	})
}
Output:
Hello!

Types

type ArgSet

type ArgSet struct {
	Usage func()
	// contains filtered or unexported fields
}

ArgSet is like flag.FlagSet but for positional arguments. The order of calls is significant.

func (*ArgSet) Args

func (a *ArgSet) Args() []string

func (*ArgSet) Bool

func (a *ArgSet) Bool(name string, value bool, usage string) *bool

Bool defines a bool positional argument.

func (*ArgSet) BoolVar

func (a *ArgSet) BoolVar(p *bool, name string, value bool, usage string)

BoolVar defines a bool positional argument with a pointer.

func (*ArgSet) Float64

func (a *ArgSet) Float64(name string, value float64, usage string) *float64

Float64 defines a float64 positional argument.

func (*ArgSet) Float64Var

func (a *ArgSet) Float64Var(p *float64, name string, value float64, usage string)

Float64Var defines a float64 positional argument with a pointer.

func (*ArgSet) Int

func (a *ArgSet) Int(name string, value int, usage string) *int

Int defines an int positional argument.

func (*ArgSet) IntVar

func (a *ArgSet) IntVar(p *int, name string, value int, usage string)

IntVar defines an int positional argument with a pointer.

func (*ArgSet) Parse

func (a *ArgSet) Parse(args []string) error

func (*ArgSet) SetOutput

func (a *ArgSet) SetOutput(w io.Writer)

func (*ArgSet) String

func (a *ArgSet) String(name string, value string, usage string) *string

String defines a string positional argument.

func (*ArgSet) StringVar

func (a *ArgSet) StringVar(p *string, name string, value string, usage string)

StringVar defines a string positional argument with a pointer.

func (*ArgSet) Var

func (a *ArgSet) Var(value flag.Value, name string, usage string)

Jump to

Keyboard shortcuts

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