Skip to content

Commit dbfa5ee

Browse files
feat(wrangler): Add support for examples in --help
This commit enables yargs `example` support in wrangler (see https://yargs.js.org/docs/#api-reference-examplecmd1-desc1-cmd2-desc2), and adds one for `vectorize query`.
1 parent a9b4f25 commit dbfa5ee

3 files changed

Lines changed: 70 additions & 8 deletions

File tree

packages/wrangler/src/__tests__/vectorize/vectorize.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,58 @@ describe("vectorize help", () => {
142142
--------------------"
143143
`);
144144
});
145+
146+
it("should show help when the query command is passed without an argument", async () => {
147+
await expect(() => runWrangler("vectorize query")).rejects.toThrow(
148+
"Not enough non-option arguments: got 0, need at least 1"
149+
);
150+
151+
expect(std.err).toMatchInlineSnapshot(`
152+
"X [ERROR] Not enough non-option arguments: got 0, need at least 1
153+
154+
"
155+
`);
156+
expect(std.out).toMatchInlineSnapshot(`
157+
"
158+
wrangler vectorize query <name>
159+
160+
Query a Vectorize index
161+
162+
POSITIONALS
163+
name The name of the Vectorize index [string] [required]
164+
165+
GLOBAL FLAGS
166+
-j, --experimental-json-config Experimental: support wrangler.json [boolean]
167+
-c, --config Path to .toml configuration file [string]
168+
-e, --env Environment to use for operations and .env files [string]
169+
-h, --help Show help [boolean]
170+
-v, --version Show version number [boolean]
171+
172+
OPTIONS
173+
--vector Vector to query the Vectorize Index [array] [required]
174+
--top-k The number of results (nearest neighbors) to return [number] [default: 5]
175+
--return-values Specify if the vector values should be included in the results [boolean] [default: false]
176+
--return-metadata Specify if the vector metadata should be included in the results [string] [choices: \\"all\\", \\"indexed\\", \\"none\\"] [default: \\"none\\"]
177+
--namespace Filter the query results based on this namespace [string]
178+
--filter Filter the query results based on this metadata filter. [string]
179+
180+
EXAMPLES
181+
❯❯ wrangler vectorize query --vector 1 2 3 0.5 1.25 6
182+
Query the Vectorize Index by vector. To read from a json file that contains data in the format [1, 2, 3], you could use a command like
183+
\`wrangler vectorize query --vector $(jq -r '.[]' data.json | xargs)\`
184+
185+
❯❯ wrangler vectorize query --filter '{ 'p1': 'abc', 'p2': { '$ne': true }, 'p3': 10, 'p4': false, 'nested.p5': 'abcd' }'
186+
Filter the query results.
187+
188+
--------------------
189+
📣 Vectorize is currently in open beta
190+
📣 Please use the '--deprecated-v1' flag to create, get, list, delete and insert vectors into legacy Vectorize indexes
191+
📣 See the Vectorize docs for how to get started and known issues: https://developers.cloudflare.com/vectorize
192+
📣 Please report any bugs to https://github.com/cloudflare/workers-sdk/issues/new/choose
193+
📣 To give feedback, visit https://discord.cloudflare.com/
194+
--------------------"
195+
`);
196+
});
145197
});
146198

147199
describe("vectorize commands", () => {

packages/wrangler/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ export function createCLIParser(argv: string[]) {
277277
"Commands:": `${chalk.bold("COMMANDS")}`,
278278
"Options:": `${chalk.bold("OPTIONS")}`,
279279
"Positionals:": `${chalk.bold("POSITIONALS")}`,
280+
"Examples:": `${chalk.bold("EXAMPLES")}`,
280281
});
281282
wrangler.group(
282283
["experimental-json-config", "config", "env", "help", "version"],

packages/wrangler/src/vectorize/query.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ export function options(yargs: CommonYargsArgv) {
2121
.positional("name", {
2222
type: "string",
2323
demandOption: true,
24-
description: "The name of the Vectorize index.",
24+
description: "The name of the Vectorize index",
2525
})
2626
.options({
2727
vector: {
2828
type: "array",
2929
demandOption: true,
30-
describe:
31-
"Vector to query the Vectorize Index. Example: `--vector 1 2 3 0.5 1.25 6`. To read from a json file that contains data in the format [1, 2, 3], you could use a command like `--vector $(jq -r '.[]' data.json | xargs)`",
30+
describe: "Vector to query the Vectorize Index",
3231
coerce: (arg: unknown[]) =>
3332
arg
3433
.map((value) =>
@@ -42,29 +41,28 @@ export function options(yargs: CommonYargsArgv) {
4241
"top-k": {
4342
type: "number",
4443
default: 5,
45-
describe: "The number of results (nearest neighbors) to return.",
44+
describe: "The number of results (nearest neighbors) to return",
4645
},
4746
"return-values": {
4847
type: "boolean",
4948
default: false,
5049
describe:
51-
"Specify if the vector values should be included in the results.",
50+
"Specify if the vector values should be included in the results",
5251
},
5352
"return-metadata": {
5453
type: "string",
5554
choices: ["all", "indexed", "none"],
5655
default: "none",
5756
describe:
58-
"Specify if the vector metadata should be included in the results. Should be either 'all', 'indexed' or 'none'",
57+
"Specify if the vector metadata should be included in the results",
5958
},
6059
namespace: {
6160
type: "string",
6261
describe: "Filter the query results based on this namespace",
6362
},
6463
filter: {
6564
type: "string",
66-
describe:
67-
"Filter the query results based on this metadata filter. Example: `--filter '{ 'p1': 'abc', 'p2': { '$ne': true }, 'p3': 10, 'p4': false, 'nested.p5': 'abcd' }'`",
65+
describe: "Filter the query results based on this metadata filter.",
6866
coerce: (jsonStr: string): VectorizeQueryOptions["filter"] => {
6967
try {
7068
return JSON.parse(jsonStr);
@@ -76,6 +74,17 @@ export function options(yargs: CommonYargsArgv) {
7674
},
7775
},
7876
})
77+
.example([
78+
[
79+
`❯❯ wrangler vectorize query --vector 1 2 3 0.5 1.25 6\n` +
80+
" Query the Vectorize Index by vector. To read from a json file that contains data in the format [1, 2, 3], you could use a command like\n" +
81+
" `wrangler vectorize query --vector $(jq -r '.[]' data.json | xargs)`\n",
82+
],
83+
[
84+
"❯❯ wrangler vectorize query --filter '{ 'p1': 'abc', 'p2': { '$ne': true }, 'p3': 10, 'p4': false, 'nested.p5': 'abcd' }'\n" +
85+
" Filter the query results.",
86+
],
87+
])
7988
.epilogue(vectorizeBetaWarning);
8089
}
8190

0 commit comments

Comments
 (0)