kong

package
v0.1.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package kong parses command line flags into jsonnext.Config using kong.

import "github.com/alecthomas/kong"

Kong is a flag type library supporting complex command-line structures with minimal developer effort: CLI args are expressed as Go types and field tags.

This package provides a Config struct that extends jsonnext.Config, adding fields and tags for kong to build a CLI parser. Some fields in jsonnext.Config already have kong field tags, but others need special handling as the default kong features do not support the style of CLI parsing that is compatible with the standard jsonnet CLI.

This functionality is split into a separate sub-package so users of jsonnext do not need to depend on kong if the do not use it.

Example
package main

import (
	"fmt"
	"os"

	jxkong "foxygo.at/jsonnext/kong"
	"github.com/alecthomas/kong"
)

func main() {
	// Define kong CLI struct embedding jxkong.Config, adding your own
	// application-specific flags and args.
	cli := struct {
		jxkong.Config
		Verbose  bool
		Filename string `arg:""`
	}{
		Config: *jxkong.NewConfig(), // foxygo.at/jsonnext/kong imported as jxkong
	}

	// Simulate command line arguments
	os.Args = []string{
		"prog",
		"--ext-str=extvar=hello", // string external var
		`--tla-code=tlavar=1+1`,  // code top-level arg
		"--verbose",              // application-specific flag
		"filename",               // application-specific arg
	}

	// Use kong to parse command line into our CLI struct
	kong.Parse(&cli)

	// Create and configure jsonnet VM with command line args, and JPATH env var
	vm := cli.Config.MakeVM("JPATH")

	// Evaluate jsonnet snippet that references defined vars
	code := "function(tlavar) std.repeat(std.extVar('extvar'), tlavar)"
	result, _ := vm.EvaluateAnonymousSnippet("<literal>", code)

	fmt.Println(cli.Filename)
	fmt.Printf("verbose: %v\n", cli.Verbose)
	fmt.Println(result)
}
Output:

filename
verbose: true
"hellohello"

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	*jsonnext.Config
	// contains filtered or unexported fields
}

Config embeds jsonnext.Config to add kong command line parsing for external variables and top-level arguments. The flags for these options are parsed into the VMVarMaps in the embedded jsonnext.Config field.

Since Config embeds jsonnext.Config, the method set of jsonnext.Config is also present on Config.

func NewConfig

func NewConfig() *Config

NewConfig returns an initialised Config struct embedding a jsonnext.Config.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL