Skip to content

Side Features

maxlandon edited this page Jan 8, 2023 · 3 revisions

This page contains additional details and features for generating commands.

Global parsing options

Global parsing options are inherited from the octago/sflags library. If you have gone through the validations page, you will have seen how to pass options to the Generate() function.

You can find a list of the global parsing options and their effect in the other godoc file of the flags library: this file is in the root package, while the one you might have encountered until now is in the gen/flags package.

Below is an example of multiple parsing options being passed to Generate():

package main

import (
	"github.com/reeflective/flags"
	"github.com/reeflective/flags/example/commands"
	genflags "github.com/reeflective/flags/gen/flags"
	"github.com/reeflective/flags/validator"
)

func main() {
    var opts []flags.OptFunc
        
    // Default parsing behavior
    opts = append(opts, DescTag("help")) // Use a custom description tag in addition to the builtin ones.
    opts = append(opts, FlagDivider("_")) // Generate `flag_name` instead of `flag-name`.

    // This option is seldom used:
    // If you pass a struct where every field is to be transformed
    // into a flag, you don't have to use struct tags: with this
    // option, all fields are generated into a flag object.
    opts = append(opts, ParseAll())
        
    // Add a validator handler
    opts = append(opts, flags.Validator(validator.New()))
        
    rootData := commands.Root{}
        
    // Pass the options to the generation function
    rootCmd := genflags.Generate(rootData, opts...)
}

Users coming from github.com/octago/sflags will notice all of the legacy options have been kept, and that ParseAll() has been added, as in the present library struct fields not tagged as flags will NOT be considered and generated by default.

Clone this wiki locally