Commit Graph

58 Commits

Author SHA1 Message Date
Alec Thomas 0c83d0f8dc Lint fix. 2021-03-13 21:13:23 +11:00
Alec Thomas 93f73cf38c Display multiple optional args in a more expected way.
Previously:

    Usage: command [<one>] [<two>]

Now:

    Usage: command [<one> [<two>]]

Thanks to @juliaogris for the inspiration!
2021-03-13 20:30:28 +11:00
Joe Schmitt 2fdddc4f51 Add negatable tag to set a bool to be negatable.
If negatable, add `--[no-]` prefix to help.
2021-03-02 10:36:13 +11:00
Joe Schmitt 2a0d7af9c5 Add support for negated boolean flags
Will automatically set a boolean Struct field to false if the flag starts with `--no-`, even when the default is `true`.

For example:
```
type Cmd struct {
  Output bool `default:"true"`
}
```

Calling
```
command
```
Will set `Output` to `true`, but
```
command --no-output
```
Will set `Output` to `false`.
2021-03-02 10:36:13 +11:00
Alec Thomas 7e27c8ecf5 Support a simplified kong.Groups{<key>: <title>} way of specifiying groups.
See #135.
2021-02-09 15:08:22 +11:00
Mickaël Menu b68e1aba63 Add support for groups in the default HelpPrinter (#135) 2021-02-09 06:58:43 +11:00
Joe Schmitt 2479d83cc0 Add support for command aliases (#130) 2021-01-11 07:36:13 +11:00
Cam Hutchison f19f1031c0 Format help placeholders with slice/map separators
Use the specified separators for a flag in the placeholders printed in
the help message for slices and maps. Do not print the separator and
ellipsis if the separator is disabled (`"none"`).

Previously the default separator was printed with an ellipsis, ignoring
the `sep` and `mapsep` tags.

Add tests for `Flag.FormatPlaceHolder()`.
2020-06-02 17:19:11 +10:00
Alec Thomas ed7caf6841 Revert "Convert env management to a resolver."
This reverts commit fbe8d48e4b.
2020-04-30 17:03:21 +10:00
Alec Thomas fbe8d48e4b Convert env management to a resolver.
This ensures that hooks correctly detect when values are set via
environment variables. Fixes #73.
2020-04-16 22:20:43 +10:00
gak f830198fcd XOR groups for flags. (#35) 2019-07-20 20:31:04 +10:00
Alec Thomas 95de7d2f0d Produce a more useful error when flag-like values are used for flag values.
eg.  myapp: error: --log-level: expected string value but got "--foo" (long flag); perhaps try --log-level="--foo"?
2019-06-21 10:57:21 +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 2ac3d43124 Make invalid value error messages more useful. 2019-04-01 10:07:10 +11:00
Alec Thomas 886c2d7b01 Better help for maps. 2019-01-12 20:59:24 +11:00
Alec Thomas aedca401da Hide sub-tree when parent is hidden. 2018-11-14 10:01:45 +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 e474873f84 Don't show ... in help for named slice types. 2018-11-06 11:15:35 +11:00
Alec Thomas fddfb973d6 Clean up error handling a bit. 2018-11-03 09:49:28 +11:00
Alec Thomas 5d7703774f Validate enums (finally). 2018-09-23 19:59:25 +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 fd197e5081 Support for flag prefixing. 2018-09-12 12:17:57 +10:00
Alec Thomas f0bd1294a7 Support detailed help.
Any command/arg implementing the HelpProvider interface will be used to
provide arbitrarily detailed help.
2018-08-14 14:43:55 +10:00
Alec Thomas 539214f23e Add contextual information to mapper errors. 2018-07-26 17:45:12 +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 653531d6bc Start making help slightly configurable. 2018-06-20 21:55:39 +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 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 73064a687f Add <command> to help when a sub-command is required. 2018-06-08 15:20:58 +10:00
Alec Thomas d30421d2b1 Get rid of troublesome [<flags>]. 2018-06-08 15:02:14 +10:00
Alec Thomas d559a6a785 Fix display of default value in placeholder. 2018-06-06 18:13:36 +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
Alec Thomas ca0490fe2a Add some helper functions to the models. 2018-06-03 07:41:37 +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 cf89213e1e Add hook support. 2018-05-27 18:01:10 +10:00
Alec Thomas 5df3b26bab Use new Tag type for slice separator. 2018-05-23 12:20:47 -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
Alec Thomas d7bb20f297 Factor out duplicate value reset code. 2018-05-19 22:15:14 +10: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