From e9b7f24dba8192436970d7e04388f2a22c505768 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 18 May 2019 12:50:04 -0600 Subject: [PATCH] mcfg: update docs --- mcfg/cli.go | 9 ++++----- mcfg/cli_test.go | 8 +++++++- mcfg/mcfg.go | 17 ++++++++++++----- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/mcfg/cli.go b/mcfg/cli.go index 62e45da..13e8df6 100644 --- a/mcfg/cli.go +++ b/mcfg/cli.go @@ -26,11 +26,10 @@ type cliTail struct { } // WithCLITail returns a Context which modifies the behavior of SourceCLI's -// Parse, if SourceCLI is used with that Context at all. Normally when SourceCLI -// encounters an unexpected Arg it will immediately return an error. This -// function modifies the Context to indicate to Parse that the unexpected Arg, -// and all subsequent Args (i.e. the tail) should be set to the returned -// []string value. +// Parse. Normally when SourceCLI encounters an unexpected Arg it will +// immediately return an error. This function modifies the Context to indicate +// to Parse that the unexpected Arg, and all subsequent Args (i.e. the tail), +// should be set to the returned []string value. // // The descr (optional) will be appended to the "Usage" line which is printed // with the help document when "-h" is passed in. diff --git a/mcfg/cli_test.go b/mcfg/cli_test.go index 800ca88..9381ac3 100644 --- a/mcfg/cli_test.go +++ b/mcfg/cli_test.go @@ -304,9 +304,12 @@ func TestWithCLISubCommand(t *T) { } func ExampleWithCLISubCommand() { + // Create a new Context with a parameter "foo", which can be used across all + // sub-commands. ctx := context.Background() ctx, foo := WithInt(ctx, "foo", 0, "Description of foo.") + // Create a sub-command "a", which has a parameter "bar" specific to it. var bar *int ctx, aFlag := WithCLISubCommand(ctx, "a", "Description of a.", func(ctx context.Context) context.Context { @@ -314,6 +317,7 @@ func ExampleWithCLISubCommand() { return ctx }) + // Create a sub-command "b", which has a parameter "baz" specific to it. var baz *int ctx, bFlag := WithCLISubCommand(ctx, "b", "Description of b.", func(ctx context.Context) context.Context { @@ -321,13 +325,15 @@ func ExampleWithCLISubCommand() { return ctx }) + // Use Populate with manually generated CLI arguments, calling the "a" + // sub-command. args := []string{"a", "--foo=1", "--bar=2"} if _, err := Populate(ctx, &SourceCLI{Args: args}); err != nil { panic(err) } fmt.Printf("foo:%d bar:%d aFlag:%v bFlag:%v\n", *foo, *bar, *aFlag, *bFlag) - // reset output for another Populate + // reset output for another Populate, this time calling the "b" sub-command. *aFlag = false args = []string{"b", "--foo=1", "--baz=3"} if _, err := Populate(ctx, &SourceCLI{Args: args}); err != nil { diff --git a/mcfg/mcfg.go b/mcfg/mcfg.go index b9a6832..ff742cb 100644 --- a/mcfg/mcfg.go +++ b/mcfg/mcfg.go @@ -1,5 +1,12 @@ -// Package mcfg provides a simple foundation for complex service/binary -// configuration, initialization, and destruction +// Package mcfg implements the creation of different types of configuration +// parameters and various methods of filling those parameters from external +// configuration sources (e.g. the command line and environment variables). +// +// Parameters are registered onto a context, and that same context is used later +// to collect and fulfill those parameters. When used with the mctx package's +// child/parent context functionality, the path of a context is incorporated +// into the parameter's full name in order to namespace parameters which exist +// in different contexts. package mcfg import ( @@ -45,9 +52,9 @@ func sortParams(params []Param) { }) } -// CollectParams returns all Params gathered by recursively retrieving them from -// this Context and its children. Returned Params are sorted according to their -// Path and Name. +// CollectParams gathers all Params by recursively retrieving them from this +// Context and its children. Returned Params are sorted according to their Path +// and Name. func CollectParams(ctx context.Context) []Param { var params []Param