| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- package cli
- import (
- "fmt"
- "io/ioutil"
- "os"
- "time"
- )
- // App is the main structure of a cli application. It is recomended that
- // and app be created with the cli.NewApp() function
- type App struct {
- // The name of the program. Defaults to os.Args[0]
- Name string
- // Description of the program.
- Usage string
- // Version of the program
- Version string
- // List of commands to execute
- Commands []Command
- // List of flags to parse
- Flags []Flag
- // Boolean to enable bash completion commands
- EnableBashCompletion bool
- // Boolean to hide built-in help command
- HideHelp bool
- // Boolean to hide built-in version flag
- HideVersion bool
- // An action to execute when the bash-completion flag is set
- BashComplete func(context *Context)
- // An action to execute before any subcommands are run, but after the context is ready
- // If a non-nil error is returned, no subcommands are run
- Before func(context *Context) error
- // The action to execute when no subcommands are specified
- Action func(context *Context)
- // Execute this function if the proper command cannot be found
- CommandNotFound func(context *Context, command string)
- // Compilation date
- Compiled time.Time
- // Author
- Author string
- // Author e-mail
- Email string
- }
- // Tries to find out when this binary was compiled.
- // Returns the current time if it fails to find it.
- func compileTime() time.Time {
- info, err := os.Stat(os.Args[0])
- if err != nil {
- return time.Now()
- }
- return info.ModTime()
- }
- // Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
- func NewApp() *App {
- return &App{
- Name: os.Args[0],
- Usage: "A new cli application",
- Version: "0.0.0",
- BashComplete: DefaultAppComplete,
- Action: helpCommand.Action,
- Compiled: compileTime(),
- }
- }
- // Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
- func (a *App) Run(arguments []string) error {
- // append help to commands
- if a.Command(helpCommand.Name) == nil && !a.HideHelp {
- a.Commands = append(a.Commands, helpCommand)
- a.appendFlag(HelpFlag)
- }
- //append version/help flags
- if a.EnableBashCompletion {
- a.appendFlag(BashCompletionFlag)
- }
- if !a.HideVersion {
- a.appendFlag(VersionFlag)
- }
- // parse flags
- set := flagSet(a.Name, a.Flags)
- set.SetOutput(ioutil.Discard)
- err := set.Parse(arguments[1:])
- nerr := normalizeFlags(a.Flags, set)
- if nerr != nil {
- fmt.Println(nerr)
- context := NewContext(a, set, set)
- ShowAppHelp(context)
- fmt.Println("")
- return nerr
- }
- context := NewContext(a, set, set)
- if err != nil {
- fmt.Printf("Incorrect Usage.\n\n")
- ShowAppHelp(context)
- fmt.Println("")
- return err
- }
- if checkCompletions(context) {
- return nil
- }
- if checkHelp(context) {
- return nil
- }
- if checkVersion(context) {
- return nil
- }
- if a.Before != nil {
- err := a.Before(context)
- if err != nil {
- return err
- }
- }
- args := context.Args()
- if args.Present() {
- name := args.First()
- c := a.Command(name)
- if c != nil {
- return c.Run(context)
- }
- }
- // Run default Action
- a.Action(context)
- return nil
- }
- // Another entry point to the cli app, takes care of passing arguments and error handling
- func (a *App) RunAndExitOnError() {
- if err := a.Run(os.Args); err != nil {
- os.Stderr.WriteString(fmt.Sprintln(err))
- os.Exit(1)
- }
- }
- // Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
- func (a *App) RunAsSubcommand(ctx *Context) error {
- // append help to commands
- if len(a.Commands) > 0 {
- if a.Command(helpCommand.Name) == nil && !a.HideHelp {
- a.Commands = append(a.Commands, helpCommand)
- a.appendFlag(HelpFlag)
- }
- }
- // append flags
- if a.EnableBashCompletion {
- a.appendFlag(BashCompletionFlag)
- }
- // parse flags
- set := flagSet(a.Name, a.Flags)
- set.SetOutput(ioutil.Discard)
- err := set.Parse(ctx.Args().Tail())
- nerr := normalizeFlags(a.Flags, set)
- context := NewContext(a, set, ctx.globalSet)
- if nerr != nil {
- fmt.Println(nerr)
- if len(a.Commands) > 0 {
- ShowSubcommandHelp(context)
- } else {
- ShowCommandHelp(ctx, context.Args().First())
- }
- fmt.Println("")
- return nerr
- }
- if err != nil {
- fmt.Printf("Incorrect Usage.\n\n")
- ShowSubcommandHelp(context)
- return err
- }
- if checkCompletions(context) {
- return nil
- }
- if len(a.Commands) > 0 {
- if checkSubcommandHelp(context) {
- return nil
- }
- } else {
- if checkCommandHelp(ctx, context.Args().First()) {
- return nil
- }
- }
- if a.Before != nil {
- err := a.Before(context)
- if err != nil {
- return err
- }
- }
- args := context.Args()
- if args.Present() {
- name := args.First()
- c := a.Command(name)
- if c != nil {
- return c.Run(context)
- }
- }
- // Run default Action
- if len(a.Commands) > 0 {
- a.Action(context)
- } else {
- a.Action(ctx)
- }
- return nil
- }
- // Returns the named command on App. Returns nil if the command does not exist
- func (a *App) Command(name string) *Command {
- for _, c := range a.Commands {
- if c.HasName(name) {
- return &c
- }
- }
- return nil
- }
- func (a *App) hasFlag(flag Flag) bool {
- for _, f := range a.Flags {
- if flag == f {
- return true
- }
- }
- return false
- }
- func (a *App) appendFlag(flag Flag) {
- if !a.hasFlag(flag) {
- a.Flags = append(a.Flags, flag)
- }
- }
|