@@ -15,9 +15,108 @@ go get github.com/broothie/cli@latest
1515
1616## Documentation
1717
18- https://pkg.go.dev/github.com/broothie/cli
18+ Detailed documentation can be found at [ pkg.go.dev ] ( https://pkg.go.dev/github.com/broothie/cli ) .
1919
20- ## To Do
20+ ## Usage
21+
22+ Using ` cli ` is as simple as:
23+
24+ ``` go
25+ // Create and run a command called "fileserver".
26+ // `Run` automatically passes down a `context.Background()` and parses `os.Args[1:]`.
27+ // If an error is returned, and it is either a `cli.ExitError` or an `*exec.ExitError`, the error's exit code will be used.
28+ // For any other errors returned, it exits with code 1.
29+ cli.Run (" fileserver" , " An HTTP server." ,
30+
31+ // Add an optional positional argument called "root" which will default to ".".
32+ cli.AddArg (" root" , " Directory to serve from" , cli.SetArgDefault (" ." )),
33+
34+ // Add an optional flag called "port" which will default to 3000.
35+ cli.AddFlag (" port" , " Port to run server." , cli.SetFlagDefault (3000 )),
36+
37+ // Register a handler for this command.
38+ // If no handler is registered, it will simply print help and exit.
39+ cli.SetHandler (func (ctx context.Context ) error {
40+ // Extract the value of the "root" argument.
41+ root , _ := cli.ArgValue [string ](ctx, " root" )
42+
43+ // Extract the value of the "port" flag.
44+ port , _ := cli.FlagValue [int ](ctx, " port" )
45+
46+ addr := fmt.Sprintf (" :%d " , port)
47+ return http.ListenAndServe (addr, http.FileServer (http.Dir (root)))
48+ }),
49+ )
50+
51+ ```
52+
53+ Here's an example using the complete set of options:
54+
55+ ``` go
56+ cmd , err := cli.NewCommand (" git" , " Modern version control." ,
57+ // Set command version
58+ cli.SetVersion (" 2.37.0" ),
59+
60+ // Add a "--version" flag with a short flag "-V" for printing the command version
61+ cli.AddVersionFlag (cli.AddFlagShort (' V' )),
62+
63+ // Add a "--help" flag
64+ cli.AddHelpFlag (
65+
66+ // Add a short flag "-h" to help
67+ cli.AddFlagShort (' h' ),
68+
69+ // Make this flag inherited by sub-commands
70+ cli.SetFlagIsInherited (true ),
71+ ),
72+
73+ // Add a hidden "--debug" flag
74+ cli.AddFlag (" debug" , " Enable debugging" ,
75+ cli.SetFlagDefault (false ), // Default parser for flags is cli.StringParser
76+
77+ // Make it hidden
78+ cli.SetFlagIsHidden (true ),
79+ ),
80+
81+ // Add a sub-command "clone"
82+ cli.AddSubCmd (" clone" , " Clone a repository." ,
83+
84+ // Add a required argument "<url>"
85+ cli.AddArg (" url" , " Repository to clone." ,
86+
87+ // Parse it into a *url.URL
88+ cli.SetArgParser (cli.URLParser ),
89+ ),
90+
91+ // Add optional argument "<dir?>"
92+ cli.AddArg (" dir" , " Directory to clone repo into." ,
93+
94+ // Set its default value to "."
95+ cli.SetArgDefault (" ." ),
96+ ),
97+
98+ // Add a flag "--verbose"
99+ cli.AddFlag (" verbose" , " Be more verbose." ,
100+
101+ // Add a short "-v"
102+ cli.AddFlagShort (' v' ),
103+
104+ // Make it a boolean that defaults to false
105+ cli.SetFlagDefault (false ),
106+ ),
107+ ),
108+ )
109+ if err != nil {
110+ cli.ExitWithError (err)
111+ }
112+
113+ // Pass in your `context.Context` and args
114+ if err := cmd.Run (context.TODO (), os.Args [1 :]); err != nil {
115+ cli.ExitWithError (err)
116+ }
117+ ```
118+
119+ ## Roadmap
21120
22121- [ ] Audit bare ` err ` returns
23122 - [ ] Two types of errors: config and parse
0 commit comments