Skip to content

Commit cf4ff4b

Browse files
authored
feat: add enums to args (#45)
1 parent 5e45c19 commit cf4ff4b

3 files changed

Lines changed: 61 additions & 8 deletions

File tree

doc-util/README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ local d = import "github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet"
1818

1919
## Index
2020

21-
* [`fn arg(name, type, default)`](#fn-arg)
21+
* [`fn arg(name, type, default, enums)`](#fn-arg)
2222
* [`fn fn(help, args)`](#fn-fn)
2323
* [`fn obj(help, fields)`](#fn-obj)
2424
* [`fn pkg(name, url, help, filename='', version='master')`](#fn-pkg)
2525
* [`fn render(obj)`](#fn-render)
2626
* [`fn val(type, help, default)`](#fn-val)
2727
* [`obj argument`](#obj-argument)
28-
* [`fn new(name, type, default)`](#fn-argumentnew)
28+
* [`fn new(name, type, default, enums)`](#fn-argumentnew)
2929
* [`obj func`](#obj-func)
3030
* [`fn new(help, args)`](#fn-funcnew)
3131
* [`fn withArgs(args)`](#fn-funcwithargs)
@@ -45,7 +45,7 @@ local d = import "github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet"
4545
### fn arg
4646

4747
```ts
48-
arg(name, type, default)
48+
arg(name, type, default, enums)
4949
```
5050

5151
`arg` is a shorthand for `argument.new`
@@ -107,10 +107,22 @@ Utilities for creating function arguments
107107
#### fn argument.new
108108

109109
```ts
110-
new(name, type, default)
110+
new(name, type, default, enums)
111+
```
112+
113+
`new` creates a new function argument, taking the `name`, the `type`. Optionally it
114+
can take a `default` value and `enum`-erate potential values.
115+
116+
Examples:
117+
118+
```jsonnet
119+
[
120+
d.argument.new('foo', d.T.string),
121+
d.argument.new('bar', d.T.string, default='loo'),
122+
d.argument.new('baz', d.T.number, enums=[1,2,3]),
123+
]
111124
```
112125

113-
new creates a new function argument, taking the name, the type and optionally a default value
114126

115127
### obj func
116128

doc-util/main.libsonnet

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,30 @@
119119

120120
'#argument': d.obj('Utilities for creating function arguments'),
121121
argument:: {
122-
'#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)]),
123-
new(name, type, default=null): {
122+
'#new': d.fn(|||
123+
`new` creates a new function argument, taking the `name`, the `type`. Optionally it
124+
can take a `default` value and `enum`-erate potential values.
125+
126+
Examples:
127+
128+
```jsonnet
129+
[
130+
d.argument.new('foo', d.T.string),
131+
d.argument.new('bar', d.T.string, default='loo'),
132+
d.argument.new('baz', d.T.number, enums=[1,2,3]),
133+
]
134+
```
135+
|||, [
136+
d.arg('name', d.T.string),
137+
d.arg('type', d.T.string),
138+
d.arg('default', d.T.any),
139+
d.arg('enums', d.T.array),
140+
]),
141+
new(name, type, default=null, enums=null): {
124142
name: name,
125143
type: type,
126144
default: default,
145+
enums: enums,
127146
},
128147
},
129148
'#arg': self.argument['#new'] + self.func.withHelp('`arg` is a shorthand for `argument.new`'),

doc-util/render.libsonnet

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,31 @@
203203
for arg in self.doc.args
204204
]),
205205

206+
enums: std.join('', [
207+
if arg.enums != null
208+
then '\n\nAccepted values for `%s` are ' % arg.name
209+
+ (
210+
std.join(', ', [
211+
std.toString(item)
212+
for item in arg.enums
213+
])
214+
)
215+
else ''
216+
for arg in self.doc.args
217+
]),
218+
206219
linkName: '%(name)s(%(args)s)' % self,
207220

208-
content: '```ts\n%(name)s(%(args)s)\n```\n\n%(help)s' % self,
221+
content:
222+
(|||
223+
```ts
224+
%(name)s(%(args)s)
225+
```
226+
227+
||| % self)
228+
+ '%(help)s' % self
229+
+ '%(enums)s' % self,
230+
// odd concatenation to prevent unintential newline changes
209231
210232
},
211233

0 commit comments

Comments
 (0)