feat: add enums to args (#45)

This commit is contained in:
Jeroen Op 't Eynde 2023-02-16 19:57:15 +01:00 committed by GitHub
parent 5e45c19fbe
commit cf4ff4b950
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 8 deletions

View file

@ -18,14 +18,14 @@ local d = import "github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet"
## Index ## Index
* [`fn arg(name, type, default)`](#fn-arg) * [`fn arg(name, type, default, enums)`](#fn-arg)
* [`fn fn(help, args)`](#fn-fn) * [`fn fn(help, args)`](#fn-fn)
* [`fn obj(help, fields)`](#fn-obj) * [`fn obj(help, fields)`](#fn-obj)
* [`fn pkg(name, url, help, filename='', version='master')`](#fn-pkg) * [`fn pkg(name, url, help, filename='', version='master')`](#fn-pkg)
* [`fn render(obj)`](#fn-render) * [`fn render(obj)`](#fn-render)
* [`fn val(type, help, default)`](#fn-val) * [`fn val(type, help, default)`](#fn-val)
* [`obj argument`](#obj-argument) * [`obj argument`](#obj-argument)
* [`fn new(name, type, default)`](#fn-argumentnew) * [`fn new(name, type, default, enums)`](#fn-argumentnew)
* [`obj func`](#obj-func) * [`obj func`](#obj-func)
* [`fn new(help, args)`](#fn-funcnew) * [`fn new(help, args)`](#fn-funcnew)
* [`fn withArgs(args)`](#fn-funcwithargs) * [`fn withArgs(args)`](#fn-funcwithargs)
@ -45,7 +45,7 @@ local d = import "github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet"
### fn arg ### fn arg
```ts ```ts
arg(name, type, default) arg(name, type, default, enums)
``` ```
`arg` is a shorthand for `argument.new` `arg` is a shorthand for `argument.new`
@ -107,10 +107,22 @@ Utilities for creating function arguments
#### fn argument.new #### fn argument.new
```ts ```ts
new(name, type, default) new(name, type, default, enums)
```
`new` creates a new function argument, taking the `name`, the `type`. Optionally it
can take a `default` value and `enum`-erate potential values.
Examples:
```jsonnet
[
d.argument.new('foo', d.T.string),
d.argument.new('bar', d.T.string, default='loo'),
d.argument.new('baz', d.T.number, enums=[1,2,3]),
]
``` ```
new creates a new function argument, taking the name, the type and optionally a default value
### obj func ### obj func

View file

@ -119,11 +119,30 @@
'#argument': d.obj('Utilities for creating function arguments'), '#argument': d.obj('Utilities for creating function arguments'),
argument:: { argument:: {
'#new': d.fn('new creates a new function argument, taking the name, the type and optionally a default value', [d.arg('name', d.T.string), d.arg('type', d.T.string), d.arg('default', d.T.any)]), '#new': d.fn(|||
new(name, type, default=null): { `new` creates a new function argument, taking the `name`, the `type`. Optionally it
can take a `default` value and `enum`-erate potential values.
Examples:
```jsonnet
[
d.argument.new('foo', d.T.string),
d.argument.new('bar', d.T.string, default='loo'),
d.argument.new('baz', d.T.number, enums=[1,2,3]),
]
```
|||, [
d.arg('name', d.T.string),
d.arg('type', d.T.string),
d.arg('default', d.T.any),
d.arg('enums', d.T.array),
]),
new(name, type, default=null, enums=null): {
name: name, name: name,
type: type, type: type,
default: default, default: default,
enums: enums,
}, },
}, },
'#arg': self.argument['#new'] + self.func.withHelp('`arg` is a shorthand for `argument.new`'), '#arg': self.argument['#new'] + self.func.withHelp('`arg` is a shorthand for `argument.new`'),

View file

@ -203,9 +203,31 @@
for arg in self.doc.args for arg in self.doc.args
]), ]),
enums: std.join('', [
if arg.enums != null
then '\n\nAccepted values for `%s` are ' % arg.name
+ (
std.join(', ', [
std.toString(item)
for item in arg.enums
])
)
else ''
for arg in self.doc.args
]),
linkName: '%(name)s(%(args)s)' % self, linkName: '%(name)s(%(args)s)' % self,
content: '```ts\n%(name)s(%(args)s)\n```\n\n%(help)s' % self, content:
(|||
```ts
%(name)s(%(args)s)
```
||| % self)
+ '%(help)s' % self
+ '%(enums)s' % self,
// odd concatenation to prevent unintential newline changes
}, },