| 1 | // Package cli provides a minimal framework for creating and organizing command line |
| 2 | // Go applications. cli is designed to be easy to understand and write, the most simple |
| 3 | // cli application can be written as follows: |
| 4 | // |
| 5 | // func main() { |
| 6 | // (&cli.Command{}).Run(context.Background(), os.Args) |
| 7 | // } |
| 8 | // |
| 9 | // Of course this application does not do much, so let's make this an actual application: |
| 10 | // |
| 11 | // func main() { |
| 12 | // cmd := &cli.Command{ |
| 13 | // Name: "greet", |
| 14 | // Usage: "say a greeting", |
| 15 | // Action: func(c *cli.Context) error { |
| 16 | // fmt.Println("Greetings") |
| 17 | // return nil |
| 18 | // }, |
| 19 | // } |
| 20 | // |
| 21 | // cmd.Run(context.Background(), os.Args) |
| 22 | // } |
| 23 | package cli |
| 24 | |
| 25 | import ( |
| 26 | "fmt" |
| 27 | "os" |
| 28 | "runtime" |
| 29 | "strings" |
| 30 | ) |
| 31 | |
| 32 | var isTracingOn = os.Getenv("URFAVE_CLI_TRACING") == "on" |
| 33 | |
| 34 | func tracef(format string, a ...any) { |
| 35 | if !isTracingOn { |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | if !strings.HasSuffix(format, "\n") { |
| 40 | format = format + "\n" |
| 41 | } |
| 42 | |
| 43 | pc, file, line, _ := runtime.Caller(1) |
| 44 | cf := runtime.FuncForPC(pc) |
| 45 | |
| 46 | fmt.Fprintf( |
| 47 | os.Stderr, |
| 48 | strings.Join([]string{ |
| 49 | "## URFAVE CLI TRACE ", |
| 50 | file, |
| 51 | ":", |
| 52 | fmt.Sprintf("%v", line), |
| 53 | " ", |
| 54 | fmt.Sprintf("(%s)", cf.Name()), |
| 55 | " ", |
| 56 | format, |
| 57 | }, ""), |
| 58 | a..., |
| 59 | ) |
| 60 | } |