hzDocs
hzDocs
Articles / Postshedzr.comIntroduction

Guide

Your First CLI AppConcise Version
Step by step
Concepts
CommandCommand: Invoke programCommand: Presetting ArgsCommand: RedirectToCommand: DynCommandCommand: Aliases from ConfigCommand: Event HandlersFlagFlag: RequiredFlag: Toggle GroupFlag: Valid ArgsFlag: `Head -1` styleFlag: External EditorFlag: NegatableFlag: Leading Plus Sign `+`Flag: Event Handlers解析结果Builtin Commands & Flags帮助子系统Shared App辨析Package level functionsWithOptsBackstage
Howto ...
Auto-close the ClosersRead config into structUsing is DetectorsUsing Store

References

What's New
Packages

Others

Examples
Blueprint
产品发布
产品发布之前

Your First CLI App

Getting Started

tiny1 Example

With cmdr, a tiny CLI app can be written like this,

./examples/tiny1/main.go
package main

import (
	"context"
	"os"

	"github.com/hedzr/cmdr/v2"
	"github.com/hedzr/cmdr/v2/cli"
)

func main() {
	ctx := context.Background() // with cancel can be passed thru in your actions
	app := prepareApp(ctx)
	if err := app.Run(ctx); err != nil {
		println("Application Error:", err)
		os.Exit(app.SuggestRetCode())
	}
}

func prepareApp(ctx context.Context, opts ...cli.Opt) (app cli.App) {
	app = cmdr.New(opts...).
		Info("tiny1-app", "0.3.1").
		Author("The Example Authors") // .Description(``).Header(``).Footer(``)

	app.Cmd("jump").
		Description("jump command").
		Examples(`jump example`). // {{.AppName}}, {{.AppVersion}}, {{.DadCommands}}, {{.Commands}}, ...
		OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) {
			println("jump command:", cmd)
			return
		}).
		Build()
	return
}

It's simple, neither Store, nor external loaders integrated with it.

Next Step

Want More Conciser?

What is Next?

How is this guide?

Last updated on

Introduction

cmdr is a POSIX-compliant command-line argument parser and app framework, an app setting manager included.

Concise Version

Best practise for project layout

On this page

tiny1 Example
Next Step