Commit Graph

75 Commits

Author SHA1 Message Date
Simon Frei 5a9c9c7864 Bubble errors instead of panicking (#194) 2021-08-19 18:00:53 +10:00
Cam Hutchison 89315e74ad Allow default commands with cmds/args/flags (#188)
* Move default command validation to build

Move the validation of default commands - checking if a node has
multiple default commands or a default command has children - to the
build phase rather than tracing. These errors are with the structure of
the CLI ast and are detectable before parsing the command line.

Add a couple of tests for some of the default command error cases.

* Disallow positional args on a default command

Do not allow a default command to have positional arguments. The current
check is only for branching args, but the error message implies that
positional args are not allowed either. So add a check for positional
args too, and add a test case for it.

This is breaking change to the API but is unlikely to have ill-effect as
positional args on a default command cannot be used without explicitly
naming the command (i.e. not using it as a default).

* Allow default commands with cmds/args/flags

Allow default commands to have sub-commands, args and flags when tagged
with `default:"withargs"`. This makes specifying the name of the
command on the CLI completely optional as long as the args to that
command are not ambiguous with other commands.
2021-07-12 14:19:37 +10:00
Alec Thomas cfbe844aa4 Fix xor + required combo.
See #112.
2021-07-11 21:23:18 +10:00
Alec Thomas c494f8b8f3 Support required combined with xor groups.
Fixes #112.
2021-06-21 19:25:54 +09:30
Alec Thomas 54558f65e8 Allow multiple xor:"" groups to be defined.
Fixes #113.
2021-06-21 19:04:54 +09:30
Alec Thomas 8aa52739ea Don't fail on required positional arguments that have envars.
Fixes #153.
Fixes #120.
2021-06-21 17:13:39 +09:30
Alec Thomas 6cebaa7b85 Hermitise and bump golangci-lint. 2021-06-09 12:48:47 +10:00
Mary Fesenko 1f5bbd5559 Fix issue with negatable flag being negated twice (#171) 2021-05-22 21:50:50 +10:00
Franklin "Snaipe" Mathieu d4dd709445 tag: add passthrough for positional arguments
This new tag tells the parser to stop processing flags after the
positional argument is encountered.

This is particularly useful for subcommands that forward their arguments
to an external program, and makes it possible to implement commands
like `kubectl exec` or `docker run`.

Fixes #80.
2021-04-06 07:53:16 +10:00
Joe Schmitt ed24960916 Implement code review feedback 2021-03-02 10:36:13 +11:00
Joe Schmitt 454f2c067e Add test case for inverting the negation
Not sure why anyone would do this, but it's covered now.
2021-03-02 10:36:13 +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
Joe Schmitt 2479d83cc0 Add support for command aliases (#130) 2021-01-11 07:36:13 +11:00
Alec Thomas 3d57e73d11 Ensure values aren't nil before decoding. 2020-12-04 16:20:06 +11:00
Sarah Franc 5a9b3ae012 Fix return err for PrintUsage 2020-11-12 11:32:37 +11:00
Alec Thomas 097bba54ec Revert UsageOnMissing.
This was breaking UsageOnMissing() when combined with validation of
positional arguments, in that it was allowing positional arguments to be
omitted. It also broke UsageOnMissing() somehow so that it did not work.
2020-10-27 17:33:46 +11:00
Alec Thomas 38db823367 Call Validate() functions on nodes if present. 2020-10-21 19:14:31 +11:00
hasheddan a062611ecf Do not run parent node on missing child err with usageOnMissing option
Refactors usageOnMissing option to not run parent commands after
printing usage when a child node is missing.

Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
2020-10-20 09:44:46 +11:00
hasheddan d1821a0666 Add UsageOnMissing option to Kong
Adds an UsageOnMissing option which configures Kong to omit errors and
display usage when a command is invalid due to a missing argument. This
can be useful if a user wishes for the top-level command to print usage
when no args are supplied, or if they would prefer child commands which
have their own subcommands to print usage specific to that command.

Signed-off-by: hasheddan <georgedanielmangum@gmail.com>
2020-09-11 17:10:18 -07:00
Alec Thomas 88ecc9c4e9 Fix enum's from envars not validating (fixes #107).
Also added a mapper for `*os.File`.
2020-09-08 13:46:23 +10:00
Alec Thomas 055081422d When using resolvers, use last value only.
This fixes an issue where the value and all its hooks would
be applied for each resolver, making it difficult to compose multiple
config files.
2020-07-10 16:37:03 +10:00
Alec Thomas 407c8229a6 Allow "-" as a positional or flag argument.
See #70.

There are some more elaborate ideas in that issue, but I think this is
sufficient for now.
2020-04-27 09:08:33 +10:00
Alec Thomas 860aaac388 Allow Run() on arbitrary nodes.
This is useful for dynamically running commands.
2020-04-26 19:01:43 +10:00
Alec Thomas b8c82fea7c Allow binds to be provided by a function.
This is useful for situations where the initialisation of some object
should be deferred, eg. when there are distinct "setup" and "use" phases
to a tools lifecycle.
2020-03-03 14:07:40 +11:00
Alec Thomas ada7d7bf11 Fix panic when "-" is not followed by a short flag. 2020-02-18 19:54:09 +11:00
Alec Thomas aeb6620dbe Don't select default command when displaying help.
See #41.
2020-01-23 21:19:23 +11:00
Alec Thomas b66dcf3279 Support default commands.
Fixes #41.
2020-01-23 21:02:55 +11:00
Alec Thomas a341a59d82 Don't validate enum values for unused required paths.
Fixes #40.
2019-11-20 17:37:51 +11:00
Alec Thomas d15c8fca8d Bind parent nodes when executing Run(). 2019-10-30 13:46:08 +11:00
Alec Thomas 77a613fb8b Much more thorough checking of enum values. 2019-10-11 16:55:10 +11:00
gak f830198fcd XOR groups for flags. (#35) 2019-07-20 20:31:04 +10:00
Alec Thomas 33b8d2b19c Quote enum values when displaying errors. 2019-06-21 15:34:26 +10:00
Alec Thomas 102a975844 Validate after everything is applied.
Fixes #27.
2019-06-21 12:09: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 439c674f7a Use interface{} instead of string in tokens.
This allows the scanner and resolvers to pass Go types around rather
than having to serialise/deserialise to/from strings.
2019-04-24 23:25:13 +10:00
Alec Thomas a6ce719390 Call Run(...) for all parent nodes.
Instead of just the leaf command. Fixes #30.
2019-01-13 21:33:33 +11:00
Alec Thomas 119a0d115b Ensure context bindings are used for Run(). 2018-09-27 14:58:03 +10: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 c112a076e7 Convert resolvers to an interface with a Validate() method. 2018-09-21 16:23:50 +10:00
Alec Thomas 25399cde9e Fix erroneously "fixed" error message. 2018-09-21 13:51:11 +10:00
Alec Thomas 3a832f8343 Treat envars as higher priority than resolvers. 2018-09-21 12:11:52 +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 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 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 e2c55d143e Really don't suggest hidden commands. 2018-08-08 14:45:26 +10:00