Skip to content

Commit

Permalink
feat: support varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 7, 2022
1 parent 1652088 commit 6a24106
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
8 changes: 8 additions & 0 deletions messages/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ Try this:
# warning.CommandInBeta

This command is currently in beta. Any aspect of this command can change without advanced notice. Don't use beta commands in your scripts.

# error.InvalidArgumentFormat

Set varargs with this format: key=value or key="value with spaces".

# error.DuplicateArgument

Found duplicate argument %s.
2 changes: 1 addition & 1 deletion src/exported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { Flags as OclifFlags } from '@oclif/core';

export { toHelpSection } from './util';
export { toHelpSection, parseVarArgs } from './util';
export { Deployable, Deployer, DeployerResult } from './deployer';
export { Deauthorizer } from './deauthorizer';
export { Progress, Prompter, generateTableChoices } from './ux';
Expand Down
36 changes: 36 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import {
SfdxPropertyKeys,
SFDX_ALLOWED_PROPERTIES,
SUPPORTED_ENV_VARS,
Messages,
} from '@salesforce/core';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');

export type HelpSection = {
header: string;
body: Array<{ name: string; description: string } | undefined>;
Expand Down Expand Up @@ -59,3 +63,35 @@ export function toHelpSection(
.filter((b) => b);
return { header, body };
}

export function parseVarArgs(args: Record<string, unknown>, argv: string[]): Record<string, unknown> {
const final: Record<string, unknown> = {};
const argVals = Object.values(args);

// Remove arguments from varargs
const varargs = argv.filter((val) => !argVals.includes(val));

// Support `config set key value`
if (varargs.length === 2 && !varargs[0].includes('=')) {
return { [varargs[0]]: varargs[1] };
}

// Ensure that all args are in the right format (e.g. key=value key1=value1)
varargs.forEach((arg) => {
const split = arg.split('=');

if (split.length !== 2) {
throw messages.createError('error.InvalidArgumentFormat', [arg]);
}

const [name, value] = split;

if (final[name]) {
throw messages.createError('error.DuplicateArgument', [name]);
}

final[name] = value || undefined;
});

return final;
}
31 changes: 30 additions & 1 deletion test/unit/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
SfdxPropertyKeys,
SFDX_ALLOWED_PROPERTIES,
} from '@salesforce/core';
import { toHelpSection } from '../../src/util';
import { parseVarArgs, toHelpSection } from '../../src/util';

describe('toHelpSection', () => {
it('should produce help section for env vars', () => {
Expand Down Expand Up @@ -86,3 +86,32 @@ describe('toHelpSection', () => {
});
});
});

describe('parseVarArgs', () => {
it('should parse varargs', () => {
const varargs = parseVarArgs({}, ['key1=value1']);
expect(varargs).to.deep.equal({ key1: 'value1' });
});

it('should parse varargs and not arguments', () => {
const varargs = parseVarArgs({ arg1: 'foobar' }, ['foobar', 'key1=value1']);
expect(varargs).to.deep.equal({ key1: 'value1' });
});

it('should parse single set of varargs', () => {
const varargs = parseVarArgs({ arg1: 'foobar' }, ['foobar', 'key1', 'value1']);
expect(varargs).to.deep.equal({ key1: 'value1' });
});

it('should throw if invalid format', () => {
expect(() => parseVarArgs({ arg1: 'foobar' }, ['foobar', 'key1=value1', 'key2:value2'])).to.throw(
'Set varargs with this format: key=value or key="value with spaces". key2:value2'
);
});

it('should throw if duplicates exist', () => {
expect(() => parseVarArgs({ arg1: 'foobar' }, ['foobar', 'key1=value1', 'key1=value1'])).to.throw(
'Found duplicate argument key1.'
);
});
});

0 comments on commit 6a24106

Please sign in to comment.