loaders

package module
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 3 Imported by: 1

README

Loaders for cmdr/v2

Go GitHub go.mod Go version GitHub tag (latest SemVer)

Local configuration file loaders for various file formats, such as YAML, TOML, HCL, and much more.

This is an addon library especially for cmdr/v2.

The typical app is cmdr-test/examples/large.

image-20241111141228632

A tiny app using cmdr/v2 and cmdr-loaders is:

package main

import (
	"context"
	"os"

	loaders "github.com/hedzr/cmdr-loaders"
	"github.com/hedzr/cmdr/v2"
	"github.com/hedzr/cmdr/v2/cli"
	"github.com/hedzr/cmdr/v2/examples/common"
	"github.com/hedzr/cmdr/v2/examples/blueprint/cmd"
	"github.com/hedzr/cmdr/v2/examples/devmode"
	"github.com/hedzr/cmdr/v2/pkg/logz"
)

const (
	appName = "blueprint"
	desc    = `a good blueprint for you.`
	version = cmdr.Version
	author  = `The Examples Authors`
)

func main() {
	ctx := context.Background()	
	app := prepareApp(cmd.Commands...) // define your own commands implementations with cmd/*.go
	if err := app.Run(ctx); err != nil {
		logz.ErrorContext(ctx, "Application Error:", "err", err) // stacktrace if in debug mode/build
		os.Exit(app.SuggestRetCode())
	} else if rc := app.SuggestRetCode(); rc != 0 {
		os.Exit(rc)
	}
}

func prepareApp(commands ...cli.CmdAdder) cli.App {
	return loaders.Create(
		appName, version, author, desc,
		append([]cli.Opt{
			cmdr.WithAutoEnvBindings(true),  // default it's false
			cmdr.WithSortInHelpScreen(true), // default it's false
			// cmdr.WithDontGroupInHelpScreen(false), // default it's false
			// cmdr.WithForceDefaultAction(false),
		})...,
	).
		// importing devmode package and run its init():
		With(func(app cli.App) { logz.Debug("in dev mode?", "mode", devmode.InDevelopmentMode()) }).
		WithBuilders(
			common.AddHeadLikeFlagWithoutCmd, // add a `--line` option, feel free to remove it.
			common.AddToggleGroupFlags,       //
			common.AddTypedFlags,             //
			common.AddKilobytesFlag,          //
			common.AddValidArgsFlag,          //
		).
		WithAdders(commands...).
		Build()
}

and cmd/... at:

See also:

History

See full list in CHANGELOG

License

Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(appName, version, author, desc string, opts ...cli.Opt) cmdr.Creator

Create provides a concise interface to create an cli app easily.

func PrepareApp

func PrepareApp(appName, desc string, opts ...cli.Opt) func(adders ...cli.CmdAdder) (app cli.App)

PrepareApp will be used for cmdr-tests

Types

This section is empty.

Directories

Path Synopsis
_examples
small command
lite module

Jump to

Keyboard shortcuts

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