Skip to content

Commit aaabebf

Browse files
committed
Add option and arg parsers in JS cli
1 parent 922a0c4 commit aaabebf

1 file changed

Lines changed: 71 additions & 25 deletions

File tree

clients/js/src/cli.ts

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type GlobalOptions = {
5555
keypair?: string;
5656
payer?: string;
5757
rpc?: string;
58-
priorityFees?: string;
58+
priorityFees?: MicroLamports;
5959
};
6060
const program = new Command();
6161
program
@@ -71,10 +71,15 @@ program
7171
'Path to keypair file of transaction fee and storage payer. (default: keypair)'
7272
)
7373
.option('--rpc <string>', 'RPC URL. (default: solana config or localhost)')
74-
.option(
75-
'--priority-fees <number>',
76-
'Priority fees per compute unit for sending transactions',
77-
'100000'
74+
.addOption(
75+
new Option(
76+
'--priority-fees <number>',
77+
'Priority fees per compute unit for sending transactions'
78+
)
79+
.default('100000')
80+
.argParser((value: string | undefined) =>
81+
value !== undefined ? (BigInt(value) as MicroLamports) : undefined
82+
)
7883
);
7984

8085
// Upload metadata command.
@@ -91,7 +96,17 @@ type UploadOptions = GlobalOptions & {
9196
bufferOnly: boolean;
9297
};
9398
program
94-
.command('upload <seed> <program-id> [content]')
99+
.command('upload')
100+
.argument('<seed>', 'Seed for the metadata account')
101+
.argument(
102+
'<program>',
103+
'Program associated with the metadata account',
104+
address
105+
)
106+
.argument(
107+
'[content]',
108+
'Direct content to upload. See options for other sources such as --file, --url and --account.'
109+
)
95110
.description('Upload metadata')
96111
.option(
97112
'--third-party',
@@ -141,7 +156,7 @@ program
141156
.action(
142157
async (
143158
seed: string,
144-
programAddress: string,
159+
programAddress: Address,
145160
content: string | undefined,
146161
_,
147162
cmd: Command
@@ -169,9 +184,7 @@ program
169184
buffer: options.bufferOnly ? true : undefined,
170185
extractLastTransaction: options.bufferOnly,
171186
closeBuffer: true,
172-
priorityFees: options.priorityFees
173-
? (BigInt(options.priorityFees) as MicroLamports)
174-
: undefined,
187+
priorityFees: options.priorityFees,
175188
});
176189
if (lastTransaction) {
177190
const transactionBytes =
@@ -199,15 +212,21 @@ type DownloadOptions = GlobalOptions & {
199212
thirdParty?: string | true;
200213
};
201214
program
202-
.command('download <seed> <program-id>')
215+
.command('download')
203216
.description('Download IDL to file')
217+
.argument('<seed>', 'Seed for the metadata account')
218+
.argument(
219+
'<program>',
220+
'Program associated with the metadata account',
221+
address
222+
)
204223
.option('-o, --output <path>', 'Path to save the IDL file')
205224
.option(
206225
'--third-party [address]',
207226
'When provided, a non-canonical metadata account will be downloaded using the provided address or the active keypair as the authority.',
208227
false
209228
)
210-
.action(async (seed: string, programAddress: string, _, cmd: Command) => {
229+
.action(async (seed: string, program: Address, _, cmd: Command) => {
211230
const options = cmd.optsWithGlobals() as DownloadOptions;
212231
const client = getClient(options);
213232
const authority =
@@ -219,7 +238,7 @@ program
219238
try {
220239
const content = await downloadMetadata(
221240
client.rpc,
222-
address(programAddress),
241+
program,
223242
seed,
224243
authority
225244
);
@@ -237,22 +256,28 @@ program
237256
});
238257

239258
program
240-
.command('set-authority <seed> <program-id> <new-authority>')
259+
.command('set-authority')
241260
.description(
242261
'Set or update an additional authority on canonical metadata accounts'
243262
)
263+
.argument('<seed>', 'Seed for the metadata account')
264+
.argument(
265+
'<program>',
266+
'Program associated with the metadata account',
267+
address
268+
)
269+
.argument('<new-authority>', 'The new authority to set', address)
244270
.action(
245271
async (
246272
seed: string,
247-
programAddress: string,
273+
program: Address,
248274
newAuthority: string,
249275
_,
250276
cmd: Command
251277
) => {
252278
const options = cmd.optsWithGlobals() as GlobalOptions;
253279
const client = getClient(options);
254280
const [keypair, payer] = await getKeyPairSigners(options, client.configs);
255-
const program = address(programAddress);
256281
const { metadata, programData } = await getPdaDetails({
257282
rpc: client.rpc,
258283
program,
@@ -264,9 +289,7 @@ program
264289
kind: 'message',
265290
instructions: [
266291
...getComputeUnitInstructions({
267-
computeUnitPrice: options.priorityFees
268-
? (BigInt(options.priorityFees) as MicroLamports)
269-
: undefined,
292+
computeUnitPrice: options.priorityFees,
270293
computeUnitLimit: 'simulated',
271294
}),
272295
getSetAuthorityInstruction({
@@ -282,13 +305,18 @@ program
282305
);
283306

284307
program
285-
.command('remove-authority <seed> <program-id>')
308+
.command('remove-authority')
309+
.argument('<seed>', 'Seed for the metadata account')
310+
.argument(
311+
'<program>',
312+
'Program associated with the metadata account',
313+
address
314+
)
286315
.description('Remove the additional authority on canonical metadata accounts')
287-
.action(async (seed: string, programAddress: string, _, cmd: Command) => {
316+
.action(async (seed: string, program: Address, _, cmd: Command) => {
288317
const options = cmd.optsWithGlobals() as GlobalOptions;
289318
const client = getClient(options);
290319
const [keypair, payer] = await getKeyPairSigners(options, client.configs);
291-
const program = address(programAddress);
292320
const { metadata, programData } = await getPdaDetails({
293321
rpc: client.rpc,
294322
program,
@@ -317,26 +345,44 @@ program
317345
});
318346

319347
program
320-
.command('set-immutable <seed> <program-id>')
348+
.command('set-immutable')
321349
.description(
322350
'Make the metadata account immutable, preventing any further updates'
323351
)
352+
.argument('<seed>', 'Seed for the metadata account')
353+
.argument(
354+
'<program>',
355+
'Program associated with the metadata account',
356+
address
357+
)
324358
.action(async () => {
325359
// TODO
326360
});
327361

328362
program
329-
.command('trim <seed> <program-id>')
363+
.command('trim')
330364
.description(
331365
'Trim the metadata account data to the minimum required size and recover rent'
332366
)
367+
.argument('<seed>', 'Seed for the metadata account')
368+
.argument(
369+
'<program>',
370+
'Program associated with the metadata account',
371+
address
372+
)
333373
.action(async () => {
334374
// TODO
335375
});
336376

337377
program
338-
.command('close <seed> <program-id>')
378+
.command('close')
339379
.description('Close metadata account and recover rent')
380+
.argument('<seed>', 'Seed for the metadata account')
381+
.argument(
382+
'<program>',
383+
'Program associated with the metadata account',
384+
address
385+
)
340386
.action(async () => {
341387
// TODO
342388
});

0 commit comments

Comments
 (0)