capp

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package capp provides a simple command line application build.

  • Support add multiple commands
  • Support add aliases for command

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	*cflag.CFlags // save global flags

	Name string
	Desc string
	// Version for app
	Version string
	// NameWidth max width for command name
	NameWidth  int
	HelpWriter io.Writer

	// OnAppFlagParsed hook func
	OnAppFlagParsed func(app *App) bool
	// AfterHelpBuild hook
	AfterHelpBuild func(buf *strutil.Buffer)

	// BeforeRun each command hook func
	//  - cmdArgs: input raw args for current command.
	//  - return false to stop run.
	BeforeRun func(c *Cmd, cmdArgs []string) bool
	// AfterRun command hook func
	AfterRun func(c *Cmd, err error)
	// contains filtered or unexported fields
}

App struct

func New

func New(fns ...func(app *App)) *App

New App instance

Usage:

app := capp.New(func(app *cflag.App) {})
app.Name = "mycli"
app.Version = "0.0.1"
app.Desc = "mycli is a command line tool"

func NewApp

func NewApp(fns ...func(app *App)) *App

NewApp instance. alias of New()

Example
package main

import (
	"github.com/gookit/goutil/cflag/capp"
	"github.com/gookit/goutil/dump"
)

func main() {
	app := capp.NewApp()
	app.Desc = "this is my cli application"
	app.Version = "1.0.2"

	var c1Opts = struct {
		age  int
		name string
	}{}

	c1 := capp.NewCmd("demo", "this is a demo command")
	c1.OnAdd = func(c *capp.Cmd) {
		c.IntVar(&c1Opts.age, "age", 0, "this is a int option;;a")
		c.StringVar(&c1Opts.name, "name", "", "this is a string option and required;true")

		c.AddArg("arg1", "this is arg1", true, nil)
		c.AddArg("arg2", "this is arg2", false, nil)
	}

	c1.Func = func(c *capp.Cmd) error {
		dump.P(c1Opts, c.Args())
		return nil
	}

	var c2Opts = struct {
		str1 string
		lOpt string
		bol  bool
	}{}

	c2 := capp.NewCmd("other", "this is another demo command")
	{
		c2.StringVar(&c2Opts.str1, "str1", "def-val", "this is a string option with default value;;s")
		c2.StringVar(&c2Opts.lOpt, "long-opt", "", "this is a string option with shorts;;lo")

		c2.Func = func(c *capp.Cmd) error {
			dump.P(c2Opts)
			return nil
		}
	}

	app.Add(c1, c2)
	app.Run()
}

func NewWith

func NewWith(name, version, desc string, fns ...func(app *App)) *App

NewWith name and desc and option functions

func (*App) Add

func (a *App) Add(cmds ...*Cmd)

Add command(s) to app. panic if error.

NOTE: command object should create use NewCmd()

Usage:

app.Add(
	cflag.NewCmd("cmd1", "desc1"),
	cflag.NewCmd("cmd2", "desc2"),
)

Or:

app.Add(cflag.NewCmd("cmd1", "desc1"))
app.Add(cflag.NewCmd("cmd2", "desc2"))

func (*App) AddOrErr

func (a *App) AddOrErr(cmds ...*Cmd) error

AddOrErr add command(s) to app.

func (*App) Run

func (a *App) Run()

Run app by os.Args

func (*App) RunWithArgs

func (a *App) RunWithArgs(args []string) error

RunWithArgs run app by input args

func (*App) WithConfigFn

func (a *App) WithConfigFn(fns ...func(app *App)) *App

WithConfigFn config app

type Cmd

type Cmd struct {
	*cflag.CFlags

	Name string
	Desc string // desc for command, will sync set to CFlags.Desc
	// Aliases name for command
	Aliases []string
	// OnAdd hook func. fire on add to App
	//  - you can add some cli options or arguments.
	OnAdd func(c *Cmd)
	// Func for run command, will call after options parsed. will sync set to CFlags.Func
	Func func(c *Cmd) error
	// contains filtered or unexported fields
}

Cmd for App

func NewCmd

func NewCmd(name, desc string, runFunc ...func(c *Cmd) error) *Cmd

NewCmd instance

func (*Cmd) Config

func (c *Cmd) Config(fns ...CmdOptionFn) *Cmd

Config the cmd. eg: bing flags

func (*Cmd) MustParse

func (c *Cmd) MustParse(args []string)

MustParse parse flags and run command, will auto handle error

func (*Cmd) MustRun

func (c *Cmd) MustRun(args []string)

MustRun parse flags and run command. alias of MustParse()

func (*Cmd) Parse

func (c *Cmd) Parse(args []string) error

Parse flags and run command func

If args is nil, will parse os.Args

func (*Cmd) QuickRun

func (c *Cmd) QuickRun()

QuickRun parse OS flags and run command, will auto handle error

func (*Cmd) WithConfigFn

func (c *Cmd) WithConfigFn(fns ...CmdOptionFn) *Cmd

WithConfigFn config cmd, alias of ConfigCmd()

type CmdOptionFn

type CmdOptionFn func(c *Cmd)

CmdOptionFn for one command

func WithAliases

func WithAliases(aliases ...string) CmdOptionFn

WithAliases set aliases for command

Jump to

Keyboard shortcuts

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