Practical Go. Amit Saha

Читать онлайн книгу.

Practical Go - Amit Saha


Скачать книгу
Duplicate Error Messages

      You may have noticed that errors were being displayed twice. This is caused by the following code snippet in the main() function:

      c, err := parseArgs(os.Stderr, os.Args[1:]) if err != nil { . fmt.Println(err) os.Exit(1) }

      When the Parse() function call encountered an error, it was displaying that error to the output writer instance set in the fs.SetOutput() call. Subsequently, the returned error was also being printed in the main() function via the snippet above. It may seem like an easy fix not to print the error in the main() function. However, that will mean that any custom errors returned, such as when positional arguments are specified, will also not be shown. Hence, what we will do is create a custom error value and return that instead. We will only print the error if it matches that custom error, else we will skip printing it.

      A custom error value can be created as follows:

      var errPosArgSpecified = errors.New("Positional arguments specified")

      Then, in the parseArgs() function, we return the following error:

      if fs.NArg() != 0 { . return c, errPosArgSpecified }

      Then in main(), we update the code as follows:

       c, err := parseArgs(os.Stderr, os.Args[1:]) if err != nil { if errors.Is(err, errPosArgSpecified) { fmt.Fprintln(os.Stdout, err) } os.Exit(1) }

      The errors.Is() function is used to check whether the error value err matches the error value errPosArgSpecified. The error is displayed only if a match is found.

      Customizing Usage Message

      fs.Usage = func() { var usageString = ` A greeter application which prints the name you entered a specified number of times. Usage of %s: ` fmt.Fprintf(w, usageString, fs.Name()) fmt.Fprintln(w) fs.PrintDefaults() }

      Once we set the Usage attribute of the FlagSet object to a custom function, it is called whenever there is an error parsing the specified options. Note that the preceding function is defined as an anonymous function so that it can access the specified writer object, w, to display the custom usage message. Inside the function, we access the name of the FlagSet using the Name() method. Then we print a new line and call the PrintDefaults() method, which prints the various options that have been defined along with their type and default values. The updated parseArgs() function is as follows:

      func parseArgs(w io.Writer, args []string) (config, error) { c := config{} fs := flag.NewFlagSet("greeter", flag.ContinueOnError) fs.SetOutput(w) fs.Usage = func() { var usageString = ` A greeter application which prints the name you entered a specified number of times. Usage of %s: <options> [name]` fmt.Fprintf(w, usageString, fs.Name()) fmt.Fprintln(w) fmt.Fprintln(w, "Options: ") fs.PrintDefaults() } fs.IntVar(&c.numTimes, "n", 0, "Number of times to greet") err := fs.Parse(args) if err != nil { return c, err } if fs.NArg()> 1 { return c, errInvalidPosArgSpecified } if fs.NArg() == 1 { c.name = fs.Arg(0) } return c, nil }

      Next, you will implement the final improvement. The greeter program will now allow specifying the name via a positional argument as well. If one is not specified, you will ask for the name interactively.

      Accept Name via a Positional Argument

      First, update the config struct to have a name field of type string as follows:

      type config struct { numTimes int name string }

      Then the greetUser() function will be updated to the following:

      func greetUser(c config, w io.Writer) { msg := fmt.Sprintf("Nice to meet you %s\n", c.name) for i := 0; i < c.numTimes; i++ { fmt.Fprintf(w, msg) } }

      Next, we update the custom error value as follows:

      var errInvalidPosArgSpecified = errors.New("More than one positional argument specified")

      We update the parseArgs() function now to look for a positional argument and, if one is found, set the name attribute of the config object appropriately:

       if fs.NArg()> 1 { return c, errInvalidPosArgSpecified } if fs.NArg() == 1 { c.name = fs.Arg(0) }

      The runCmd() function is updated so that it only asks the user to input the name interactively if not specified, or if an empty string was specified:

      Create a new directory, chap1/flag-improvements/, and initialize a module inside it:

      $ mkdir -p chap1/flag-improvements $ cd chap1/flag-improvements $ go mod init github.com/username/flag-improvements

      Next, save Listing 1.7 as main.go. Build it as follows:

      $ go build -o application

      Run the built application code with -help, and you will see the custom usage message:

      $ ./application -help A greeter application which prints the name you entered a specified number of times. Usage of greeter: <options> [name] Options: -n int Number of times to greet

      Now let's specify a name as a positional argument:

      $ ./application -n 1 "Jane Doe" Nice to meet you Jane Doe

      Next let's specify a bad input—a string as value to the -n option:


Скачать книгу