@@ -93,6 +93,17 @@ import (
9393 "strings"
9494)
9595
96+ // A CommandInfo holds information about the schema of a CLI command. This includes usage
97+ // information that can form a help message, as well as which options and arguments or
98+ // subcommands it should expect when parsing. This is data that must be known when parsing
99+ // in order to properly parse command line arguments. For example, consider the following
100+ // argument list: {"-a", "b"}. Is "-a" a boolean option and "b" an argument to the
101+ // command? Or is "b" the option value being provided to the non-boolean option "-a"? The
102+ // only way for the parser to know is to follow an outline that states which one it is.
103+ //
104+ // The methods on CommandInfo are available to guide library consumers through creating
105+ // this command schema. Once built, a CommandInfo would typically be used by calling
106+ // [CommandInfo.ParseOrExit] or [CommandInfo.Parse].
96107type CommandInfo struct {
97108 Name string
98109 Path []string
@@ -253,14 +264,16 @@ func Fatal(code int, v any) {
253264 os .Exit (code )
254265}
255266
256- // ParseOrExit will parse input based on this CommandInfo. If help was requested, it
257- // will print the help message and exit the program successfully (status code 0). If
258- // there is any other error, it will print the error and exit the program with failure
259- // (status code 1). The input parameter semantics are the same as [CommandInfo.Parse].
267+ // ParseOrExit calls [CommandInfo.ParseTheseOrExit] using os.Args as the command
268+ // line arguments. See that method's documentation for more info.
260269func (in CommandInfo ) ParseOrExit () * Command {
261270 return in .ParseTheseOrExit (os .Args [1 :]... )
262271}
263272
273+ // ParseTheseOrExit parses input against this CommandInfo using args as the command line
274+ // arguments. If there is a [HelpOrVersionRequested] error, it will print the message and
275+ // exit with status code 0. If there was any other error, it will print the error's
276+ // message to Stderr and exit with status code 1.
264277func (in CommandInfo ) ParseTheseOrExit (args ... string ) * Command {
265278 c , err := in .ParseThese (args ... )
266279 if err != nil {
@@ -274,12 +287,18 @@ func (in CommandInfo) ParseTheseOrExit(args ...string) *Command {
274287 return c
275288}
276289
277- // ParseOrExit will parse input based on this CommandInfo. If no function arguments
278- // are provided, the [os.Args] will be used .
290+ // Parse calls [ CommandInfo.ParseThese] using os.Args as the command
291+ // line arguments. See that method's documentation for more info .
279292func (in * CommandInfo ) Parse () (* Command , error ) {
280293 return in .ParseThese (os .Args [1 :]... )
281294}
282295
296+ // ParseThese prepares and validates this CommandInfo if it hasn't been already. This
297+ // involves setting the Path field of this command and all subcommands, as well as
298+ // ensuring there are no logical errors in the structure of this command (such as a
299+ // command containing duplicate option names). This method will panic if there are any
300+ // structural errors in this CommandInfo. Assuming a clean structure, this method then
301+ // parses input against this CommandInfo using args as the command line arguments.
283302func (in * CommandInfo ) ParseThese (args ... string ) (* Command , error ) {
284303 if ! in .isPrepped {
285304 in .prepareAndValidate ()
@@ -292,6 +311,8 @@ func (in *CommandInfo) ParseThese(args ...string) (*Command, error) {
292311 return c , err
293312}
294313
314+ // HelpOrVersionRequested is returned by the parsing code
315+ // to signal that a help or version option was encountered.
295316type HelpOrVersionRequested struct {
296317 Msg string
297318}
0 commit comments