Skip to content

Instantly share code, notes, and snippets.

@qbig
Created August 14, 2018 13:46
Show Gist options
  • Select an option

  • Save qbig/7a9a76efc6512b1fe120b0bc876b7233 to your computer and use it in GitHub Desktop.

Select an option

Save qbig/7a9a76efc6512b1fe120b0bc876b7233 to your computer and use it in GitHub Desktop.
Using command-line flags

Working with flags

 package main

        import (
             "flag"
             "fmt"
        )

        // Config will be the holder for our flags
        type Config struct {
             subject string
             isAwesome bool
             howAwesome int
             countTheWays CountTheWays
        }

        // Setup initializes a config from flags that
        // are passed in
        func (c *Config) Setup() {
            // you can set a flag directly like so:
            // var someVar = flag.String("flag_name", "default_val",           
            // "description")
            // but in practice putting it in a struct is generally 
            // better longhand
            flag.StringVar(&c.subject, "subject", "", "subject is a           
            string, it defaults to empty")
            // shorthand
            flag.StringVar(&c.subject, "s", "", "subject is a string, 
            it defaults to empty (shorthand)")

           flag.BoolVar(&c.isAwesome, "isawesome", false, "is it 
           awesome or what?")
           flag.IntVar(&c.howAwesome, "howawesome", 10, "how awesome 
           out of 10?")

           // custom variable type
           flag.Var(&c.countTheWays, "c", "comma separated list of 
           integers")
        }

        // GetMessage uses all of the internal
        // config vars and returns a sentence
        func (c *Config) GetMessage() string {
            msg := c.subject
            if c.isAwesome {
                msg += " is awesome"
            } else {
                msg += " is NOT awesome"
            }

            msg = fmt.Sprintf("%s with a certainty of %d out of 10. Let 
            me count the ways %s", msg, c.howAwesome, 
            c.countTheWays.String())
            return msg
        }
 package main

        import (
            "fmt"
            "strconv"
            "strings"
        )

        // CountTheWays is a custom type that
        // we'll read a flag into
        type CountTheWays []int

        func (c *CountTheWays) String() string {
            result := ""
            for _, v := range *c {
                if len(result) > 0 {
                    result += " ... "
                }
                result += fmt.Sprint(v)
            }
            return result
        }

        // Set will be used by the flag package
        func (c *CountTheWays) Set(value string) error {
            values := strings.Split(value, ",")

            for _, v := range values {
                i, err := strconv.Atoi(v)
                if err != nil {
                    return err
                }
                *c = append(*c, i)
            }

            return nil
        }
package main

        import (
            "flag"
            "fmt"
        )

        func main() {
            // initialize our setup
            c := Config{}
            c.Setup()

            // generally call this from main
            flag.Parse()

            fmt.Println(c.GetMessage())
        }
@qbig
Copy link
Copy Markdown
Author

qbig commented Aug 14, 2018

      $ go build 
      $ ./flags -h 
      Usage of ./flags:
       -c value
       comma separated list of integers
       -howawesome int
       how awesome out of 10? (default 10)
       -isawesome
       is it awesome or what? (default false)
       -s string
       subject is a string, it defaults to empty (shorthand)
       -subject string
       subject is a string, it defaults to empty
      $ ./flags -s Go -isawesome -howawesome 10 -c 1,2,3 
      Go is awesome with a certainty of 10 out of 10. Let me count 
      the ways 1 ... 2 ... 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment