package cli
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"
"testing"
"time"
)
var (
lastExitCode = 0
fakeOsExiter = func(rc int) {
lastExitCode = rc
}
fakeErrWriter = &bytes.Buffer{}
func init() {
OsExiter = fakeOsExiter
ErrWriter = fakeErrWriter
type opCounts struct {
Total, ShellComplete, OnUsageError, Before, CommandNotFound, Action, After, SubCommand int
func ExampleApp_Run() {
// set args for examples sake
os.Args = []string{"greet", "--name", "Jeremy"}
app := &App{
Name: "greet",
Flags: []Flag{
&StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
},
Action: func(c *Context) error {
fmt.Printf("Hello %v\n", c.String("name"))
return nil
UsageText: "app [first_arg] [second_arg]",
Authors: []*Author{{Name: "Oliver Allen", Email: "oliver@toyshop.example.com"}},
app.Run(os.Args)
// Output:
// Hello Jeremy
func ExampleApp_Run_subcommand() {
os.Args = []string{"say", "hi", "english", "--name", "Jeremy"}
Name: "say",
Commands: []*Command{
{
Name: "hello",
Aliases: []string{"hi"},
Usage: "use it to see a description",
Description: "This is how we describe hello the function",
Subcommands: []*Command{
Name: "english",
Aliases: []string{"en"},
Usage: "sends a greeting in english",
Description: "greets someone in english",
&StringFlag{
Name: "name",
Value: "Bob",
Usage: "Name of the person to greet",
fmt.Println("Hello,", c.String("name"))
_ = app.Run(os.Args)
// Hello, Jeremy
func ExampleApp_Run_appHelp() {
os.Args = []string{"greet", "help"}
Version: "0.1.0",
Description: "This is how we describe greet the app",
Authors: []*Author{
{Name: "Harrison", Email: "harrison@lolwut.com"},
{Name: "Oliver Allen", Email: "oliver@toyshop.com"},
Name: "describeit",
Aliases: []string{"d"},
Description: "This is how we describe describeit the function",
fmt.Printf("i like to describe things")
// NAME:
// greet - A new cli application
//
// USAGE:
// greet [global options] command [command options] [arguments...]
// VERSION:
// 0.1.0
// DESCRIPTION:
// This is how we describe greet the app
// AUTHORS:
// Harrison <harrison@lolwut.com>
// Oliver Allen <oliver@toyshop.com>
// COMMANDS:
// describeit, d use it to see a description
// help, h Shows a list of commands or help for one command
// GLOBAL OPTIONS:
// --name value a name to say (default: "bob")
// --help, -h show help (default: false)
// --version, -v print the version (default: false)
func ExampleApp_Run_commandHelp() {
os.Args = []string{"greet", "h", "describeit"}
// greet describeit - use it to see a description
// greet describeit [command options] [arguments...]
// This is how we describe describeit the function
// OPTIONS:
func ExampleApp_Run_noAction() {
app := App{}
app.Name = "greet"
_ = app.Run([]string{"greet"})