Skip to content

Commit 340490d

Browse files
committed
feat(core): add support for variadic values
1 parent 522264f commit 340490d

19 files changed

Lines changed: 558 additions & 269 deletions

README.md

Lines changed: 238 additions & 170 deletions
Large diffs are not rendered by default.

example/calc.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Container } from 'inversify';
2+
import * as path from 'path';
3+
import * as cli from '../src';
4+
5+
import './services/calculator';
6+
7+
const container = new Container({ autoBindInjectable: true });
8+
9+
async function run() {
10+
await cli
11+
.versionFromPackage(__dirname)
12+
.ioc(container)
13+
.commandsFromDirectory(path.join(__dirname, '/commands'));
14+
15+
cli.execute();
16+
}
17+
18+
// tslint:disable-next-line:no-console
19+
run().catch(console.error);

example/commands/add.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { injectable } from 'inversify';
2+
import { Command, command, option, value } from '../../src';
3+
import { Calculator } from '../services/calculator';
4+
5+
export class AddCommandParams {
6+
@value({ variadic: { type: Number } })
7+
values: number[] = [];
8+
9+
@option({ shortName: 't' })
10+
thousandSeparators: boolean = false;
11+
12+
@option({ shortName: 'd', valueName: 'count' })
13+
decimalPlaces: number = 0;
14+
}
15+
16+
@command('add', AddCommandParams)
17+
@injectable()
18+
export class AddCommand implements Command<AddCommandParams> {
19+
constructor(private calculator: Calculator) {
20+
}
21+
22+
// tslint:disable:no-console
23+
async execute(params: AddCommandParams) {
24+
const { values, thousandSeparators, decimalPlaces } = params;
25+
26+
const result = this.calculator.add(...values);
27+
28+
const format = (val: number) => val.toLocaleString(undefined, {
29+
useGrouping: thousandSeparators,
30+
maximumFractionDigits: decimalPlaces
31+
});
32+
33+
console.log(`${values.map((val) => format(val)).join(' + ')} = ${format(result)}`);
34+
}
35+
// tslint:enable:no-console
36+
37+
}

example/commands/greet.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

example/commands/login.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

example/commands/substract.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { injectable } from 'inversify';
2+
import { Command, command, option, value } from '../../src';
3+
import { Calculator } from '../services/calculator';
4+
5+
export class SubtractCommandParams {
6+
@value({ variadic: { type: Number } })
7+
values: number[] = [];
8+
9+
@option({ shortName: 't' })
10+
thousandSeparators: boolean = false;
11+
12+
@option({ shortName: 'd', valueName: 'count' })
13+
decimalPlaces: number = 0;
14+
}
15+
16+
@command('subtract', SubtractCommandParams)
17+
@injectable()
18+
export class SubtractCommand implements Command<SubtractCommandParams> {
19+
constructor(private calculator: Calculator) {
20+
}
21+
22+
// tslint:disable:no-console
23+
async execute(params: SubtractCommandParams) {
24+
const { values, thousandSeparators, decimalPlaces } = params;
25+
26+
const result = this.calculator.subtract(...values);
27+
28+
const format = (val: number) => val.toLocaleString(undefined, {
29+
useGrouping: thousandSeparators,
30+
maximumFractionDigits: decimalPlaces
31+
});
32+
33+
console.log(`${values.map((val) => format(val)).join(' + ')} = ${format(result)}`);
34+
}
35+
// tslint:enable:no-console
36+
37+
}

example/index.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

example/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "calc",
3+
"version": "1.2.3"
4+
}

example/services/calculator.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { injectable } from 'inversify';
2+
3+
@injectable()
4+
export class Calculator {
5+
add(...amounts: number[]) {
6+
return amounts.reduce((total, amount) => total + amount, 0);
7+
}
8+
9+
subtract(...amounts: number[]) {
10+
return amounts.reduce((total, amount) => total - amount, amounts.splice(0, 1)[0]);
11+
}
12+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "tsc -p ./tsconfig.core.json",
88
"clean": "rimraf ./dist",
99
"commit": "commit",
10-
"example": "ts-node ./example",
10+
"example": "ts-node ./example/calc",
1111
"lint": "tslint -p ./tsconfig.json",
1212
"lint:fix": "npm run lint -- --fix",
1313
"precommit": "npm run lint && npm test && npm run package",

0 commit comments

Comments
 (0)