150 Commits

Author SHA1 Message Date
Alec Thomas c8b3fc9840 A few extra fixes and tests. 2018-11-21 11:59:53 +11:00
Alec Thomas 010e3134ca Ensure flag defaults (or envars) aren't scanned in as untyped tokens.
This avoids treating default values starting with "-" as flags.
2018-11-06 14:40:02 +11:00
Alec Thomas fddfb973d6 Clean up error handling a bit. 2018-11-03 09:49:28 +11:00
Alec Thomas f72f53d947 Support for adding bindings to the Context.
This is very useful for hooks to pre-construct objects that can be used
by all subsequent downstream commands, for example.
2018-09-27 14:20:16 +10:00
Alec Thomas 5d7703774f Validate enums (finally). 2018-09-23 19:59:25 +10:00
Alec Thomas 026359efd8 Call reset() in Trace().
This ensures default values are all set prior to any hooks.
2018-09-21 11:11:13 +10:00
Alec Thomas 6fa83bdc0e Apply hooks to default values. 2018-09-20 20:33:55 +10:00
Alec Thomas 54338bd8b1 Add an "embed" tag. 2018-09-19 13:23:18 +10:00
Alec Thomas 467352418f Ignore fields tagged with kong:"-". 2018-09-17 10:27:38 +10:00
Alec Thomas 37d57a878b Support for embedding interfaces. 2018-09-12 13:19:56 +10:00
Alec Thomas fd197e5081 Support for flag prefixing. 2018-09-12 12:17:57 +10:00
Alec Thomas 844494faff Allow bool flags to be set to true or false explicitly. 2018-09-11 23:57:18 +10:00
Alec Thomas 862837e6fa Add ConfigFlag for loading configuration through a flag. 2018-09-11 10:02:23 +10:00
Alec Thomas ebe508cf46 Fix incorrect error missing for missing required args. 2018-08-14 21:05:58 +10:00
Alec Thomas a13c5a0039 Add hook callback methods.
`BeforeHook()` and `AfterHook()` may be implemented on CLI nodes to
trigger hooks. Use the `Bind()` option to bind potential arguments.
2018-07-04 22:31:29 +10:00
Alec Thomas 1f1e9d0f0f Convert Option to an interface and make Vars a map conforming to the interface. 2018-07-03 23:42:51 +10:00
Alec Thomas 1bb0c0b4b2 Support limited variable interpolation.
Kong supports limited variable interpolation into help strings, enum lists and
default values.

Variables are in the form:

    ${<name>}

Variables are set with the `Vars(map[string]string)` option. Undefined
variable references in the grammar will result in an error at construction
time.
2018-06-27 21:09:56 +10:00
Alec Thomas 6408010083 Clean up disparity between Context and Kong.
Previously, there was a confusing mix of functionality shared between
the two wherein you would need to use the Kong type for printing errors,
etc. but it did not have access to the context in order to print
context-sensitive usage information. This has been fixed.

Additionally, there are now fuzzy correction suggestions for flags and
commands

Also added a server example which shows how Kong can be used for parsing
in interactive shells. Run with:

    $ go run ./_examples/server/*.go

Then interact with:

    $ ssh -p 6740 127.0.0.1
2018-06-27 09:11:11 +10:00
Alec Thomas a2ec050947 Implement a robust Context.Run().
This helps when composing large applications from separate command
structs.
2018-06-21 21:50:30 +10:00
Alec Thomas 3a2f3eebdd Indent multi-line messages/errors. 2018-06-20 12:45:57 +10:00
Alec Thomas 9a68d32e72 Support for embedded structs + sep:"none".
Map key/value separator is now hardcoded to `=`. This allows the
`sep` tag to continue to be used for slice separation, cascading to
support maps with slice values eg. `--set a=1,2,3`.
2018-06-14 11:31:50 +10:00
Alec Thomas ecf21e4cc9 Add support for maps. 2018-06-13 22:17:28 +10:00
Alec Thomas 54386f7fa5 Track trace values externally to the path.
This allows accumulating mappers to work correctly. This also means
that resolvers are not even triggered if a command-line value is passed,
which is more desirable behaviour.
2018-06-13 21:41:55 +10:00
Alec Thomas c7dca86dad Rename Value.Value to Value.Target to correctly reflect its purpose. 2018-06-13 21:12:56 +10:00
Alec Thomas 29fe92f286 Only reset grammar when Apply()ing.
Also add a Kong.Help() function for writing context-sensitive help.
2018-06-13 12:10:04 +10:00
Alec Thomas 232faad0a0 Add configuration loading + docs + linter fixes. 2018-06-13 10:34:19 +10:00
Alec Thomas e9d88d6528 Implement flag "resolvers". (#24)
* Propagate errors.
* Use junit test output.
* Expand role of DecodeContext to include Scanner.
* Inject resolved flags as Path elements in the Context.
  This allows all existing logic to apply seamlessly: hooks, required
flags, etc.
* Clarify that hooks can be called multiple times.
2018-06-12 07:20:55 +10:00
Alec Thomas 9cc1872297 Fix short flags. 2018-06-06 19:07:01 +10:00
Alec Thomas 2afd4ba47b Help! 2018-06-04 13:55:38 +10:00
Gerald Kaszuba ed123d1c06 Allow "bare" tags and moved tag tests into tag_test.go. Fixes #19 2018-06-01 12:26:26 +10:00
Alec Thomas fdc7230e22 Separate validation into a distinct step.
This allows help to be called even when the parse trace is invalid.
Without this, the command-line would have to be valid in order to use
help at all, which defeats the purpose.
2018-05-31 15:20:45 +10:00
Alec Thomas afbb431641 Convert Hook to an Option. 2018-05-31 15:20:45 +10:00
Alec Thomas cf89213e1e Add hook support. 2018-05-27 18:01:10 +10:00
Alec Thomas d20b44baf4 Remove a bunch of duplicate recover code. 2018-05-26 15:32:45 -04:00
Alec Thomas 0bb304449c Add Trace() function. 2018-05-26 10:04:10 -04:00
Gerald Kaszuba 886edb6f5a Extend Tag for non-core decoders (Fixes #14) 2018-05-23 11:50:46 -04:00
Alec Thomas ab5cf7e6ef Tracing parser (#11)
* Add tracing to the parser.

* Synthesize a --help flag.

* Parsing now occurs in multiple phases.

1. Reset target.
2. Parse command-line into a "trace" (no values are written to target).
3. Apply traced, parsed values to the target fields.

This is another step in facilitating context-sensitive help and
completion.

* Detect duplicate flags.
2018-05-22 14:07:43 +10:00
Gerald Kaszuba 3eb5e285ed Use "kong" as tag keys fixes #9 (#10) 2018-05-21 09:25:19 -04:00
Alec Thomas c27dd50be6 Move .Set = true into Decode(). 2018-05-19 21:04:23 +10:00
Gerald Kaszuba 8e96da517d Fixes #3 Required and optional flags+args (#6) 2018-05-19 20:54:26 +10:00
Gerald Kaszuba a9179cd8ec Allow parent flags to be processed #4 2018-05-19 14:49:39 +10:00
Alec Thomas 3b21a54094 More validation. 2018-05-18 21:30:44 +10:00
Alec Thomas d03571a873 Use variadic options to configure Kong. 2018-05-18 21:17:48 +10:00
Gerald Kaszuba b4b5827044 Matching arg field (#1)
Check if the first field of an argument branch matches the parent struct field.
2018-05-18 18:38:53 +10:00
Alec Thomas 67b1a12059 Require cmd:"" or arg:"" for branching commands/args. 2018-05-18 15:39:40 +10:00
Alec Thomas c6776fe4b8 Docs. 2018-05-18 13:38:07 +10:00
Alec Thomas 31fe51f9d8 Support cumulative positional arguments. 2018-05-17 20:16:12 +10:00
Alec Thomas b9d002b746 Slice support. 2018-05-17 19:39:48 +10:00
Alec Thomas 8f26b13088 Slice support. 2018-05-17 10:40:46 +10:00
Alec Thomas 1d00dfef7b Implemented most of the base parser.
This includes branching arguments as well as commands, eg.

    app user create <id> <first> <last>
    app user <id> delete
    app user <id> rename <to>

Of note, required/optional flags and positional arguments are not
currently enforced.
2018-05-16 20:33:18 +10:00