liveBook · Manning

Flag.Parse

As explained in Learn Go with Pocket-Sized Projects

flag.Parse is a Go flag-package function that parses command-line arguments and stores the resulting values in registered flag variables.

flag.Parse scans command-line parameters after flags are registered and fills the variables or pointers associated with those flags. Define every flag before calling it and access parsed values afterward. section 2.4 section 2.4.1

A typical flag.Parse sequence is:

  • Register a flag with flag.StringVar, flag.IntVar, or another typed helper, supplying its destination, name, default, and description. section 2.4.1
  • Call flag.Parse after all flags are defined and before reading their values. section 6.4.1
  • Use the populated variable, or dereference the pointer returned by flag.String; positional arguments remain available through flag.Arg. section 6.4.1 section 2.4.1
  • Validate values such as language codes, because successful parsing does not establish that a value is supported or correctly formatted. section 2.4.1

Language-selectable greeting

var lang string flag.StringVar(&lang, "lang", "en", "The required language, e.g. en, ur...") flag.Parse() greeting := greet(language(lang)) fmt.Println(greeting) Running the program with a language flag changes lang; omitting it leaves the default value "en". section 2.4.1

Listing 2.17 main.go: Using flags
package main

import (
    "flag"
    "fmt"
)

func main() {
    var lang string 
flag.StringVar(&lang, "lang", "en", "The required language, e.g. en, ur...") #1 flag.Parse() greeting := greet(language(lang)) fmt.Println(greeting) } // language represents a language type language string // phrasebook holds greeting for each supported language var phrasebook = map[language]string{ ... } // greet says hello to the world func greet(l language) string { ... }

Use the complete listing to see flag registration, flag.Parse, conversion to a domain type, and greeting output together. section 2.4.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains registration, call ordering, destination variables, defaults, pointers, and positional arguments, stopping short of parser internals.
Examples
Uses a language-selectable greeting and a currency converter with source, target, and positional amount inputs.
What it assumes
Assumes basic Go functions and pointers while teaching flag registration, defaults, parsing order, and command-line inputs.
What you can do after
Operate/configure Go command-line programs by registering flags, parsing input, reading values, and validating required data.
How it connects
Connects command-line arguments, flags, pointers, default values, type conversion, validation, and usage messages.

Also explained in

Compare how 2 books explain flag.Parse →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.