112 Commits

Author SHA1 Message Date
Alec Thomas 73ecfde9b2 Automatically add envar to help.
See #57.
2020-02-19 15:52:02 +11:00
Alec Thomas b21bf1031a Support interpolating "$env".
Fixes #57.
2020-02-19 13:27:12 +11:00
Alec Thomas 102a975844 Validate after everything is applied.
Fixes #27.
2019-06-21 12:09:04 +10:00
Alec Thomas 9d0bd59611 Print error after usage when UsageOnError is set.
Previously, the underlying error was often lost when usage output was
large.
2019-06-21 09:53:05 +10:00
Alec Thomas 7be398e79f ApplyDefaults() now only applies defaults if the value is not already otherwise set. 2019-06-12 13:26:36 +10:00
Alec Thomas c8b3fc9840 A few extra fixes and tests. 2018-11-21 11:59:53 +11:00
Alec Thomas 072357f8ba Make ExpandPath public. 2018-11-06 11:37:26 +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 c935ea07dd Fix some more Resolver changes. 2018-09-21 17:13:37 +10:00
Alec Thomas c112a076e7 Convert resolvers to an interface with a Validate() method. 2018-09-21 16:23:50 +10:00
Alec Thomas 3a832f8343 Treat envars as higher priority than resolvers. 2018-09-21 12:11:52 +10:00
Alec Thomas 6406edf15f Add Visitor function for walking the model. 2018-09-21 09:47:55 +10:00
Alec Thomas 6fa83bdc0e Apply hooks to default values. 2018-09-20 20:33:55 +10:00
Alec Thomas 2e29ff8981 Add support for setting variables via tag.
This provides much more convenient composition when reusing structs in
different parts of the command grammar.

eg.

	type Embedded struct {
		Key string `help:"A key from ${where}."`
	}

	var cli struct {
		Embedded `set:"where=somewhere"`
	}
2018-09-19 12:57:59 +10:00
Alec Thomas 862837e6fa Add ConfigFlag for loading configuration through a flag. 2018-09-11 10:02:23 +10:00
Alec Thomas 99ff32a85f Bind Vars. 2018-07-06 10:44:09 +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 212ea2a356 Add path, existingfile and existingdir types.
- Document custom types.
- Add docker example.
2018-06-21 16:19:08 +10:00
Alec Thomas cdcdf49f67 Display usage information on error. 2018-06-21 10:08:01 +10:00
Alec Thomas 653531d6bc Start making help slightly configurable. 2018-06-20 21:55:39 +10:00
Alec Thomas 3a2f3eebdd Indent multi-line messages/errors. 2018-06-20 12:45:57 +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 0fb3de514f Add a default-enabled EnvResolver for the env tag. 2018-06-12 23:27:58 +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 dd03e4c48a Ensure help Flag has Tag. 2018-06-08 14:54:10 +10:00
Alec Thomas 01fa22ca03 Generalise post-build options. 2018-06-08 13:44:55 +10:00
Alec Thomas c535900861 Don't apply Options twice. This is less confusing. 2018-06-08 13:41:43 +10:00
Alec Thomas 149a83692d Bug fix. 2018-06-07 22:35:09 +10:00
Alec Thomas 9498118314 Prefix "error: " to error messages. 2018-06-06 18:34:05 +10:00
Alec Thomas 96fa9c43d5 Improved documentation and help. 2018-06-05 11:36:51 +10:00
Alec Thomas 2afd4ba47b Help! 2018-06-04 13:55:38 +10:00
Alec Thomas 48af58cefa Decoders are now field mappers.
Mappers are responsible for mapping from command-line input to Go. This
is typically just decoding, but also includes other information such as
if the field is a bool.
2018-06-04 13:13:09 +10:00
Gerald Kaszuba c8b487e49c Removed double parsing of options. 2018-06-03 17:24:27 +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 ecd369e622 Kong tag PR fixes (#12)
* Removed bubbling errors in favour of panic
* Added a test for Parse with a bad build and an arg.
* Removed is() function in favour of a switch with strings
* Collect key and value in parseCSV, reducing complexity.
* Fix in global to not use parser instance on error.
2018-05-22 09:18:08 -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 6a5fba0967 Move ParseContext into its own file. 2018-05-19 22:39:01 +10:00
Alec Thomas d7bb20f297 Factor out duplicate value reset code. 2018-05-19 22:15:14 +10:00
Alec Thomas d1acfab2e6 Factor parsing out into a ParseContext type.
This is groundwork for supporting context-sensitive help and completion.

Fixes #7.
2018-05-19 21:55:22 +10:00