Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Commit b359b35

Browse files
fsargentjaebradley
authored andcommitted
Expose errors, better requirements checking on CLI arguments (#43)
* Added checking for addresses being provided. Commander doesnt yet provide the ability to set required arguments. * Add checking for required arguments for time command. * Reformat uber-time to use Command better * Update to how the commander CLI library is used. Will output help by default if no arguments given. Added descriptions.
1 parent a3f1b23 commit b359b35

File tree

5 files changed

+52
-34
lines changed

5 files changed

+52
-34
lines changed

src/executables/uber-price.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,25 @@ const symbolService = new SymbolService();
2525
program
2626
.option('-s, --start <start>', 'specify start address')
2727
.option('-e, --end <end>', 'specify end address')
28-
.option('-u, --unit <unit>', 'specify distance unit')
28+
.option('-u, --unit [unit]', 'specify distance unit')
2929
.parse(process.argv);
3030

31-
try {
32-
service.executePriceEstimates(program.start, program.end, program.unit)
33-
.then(table => console.log(table))
34-
.catch((e) => {
35-
if (isDistanceExceededError(e)) {
36-
console.log(`Maximum distance of ${symbolService.getMaximumDistanceSymbol()} miles exceeded between start address: ${program.start} and end address: ${program.end}`);
37-
} else {
38-
console.error('Could not get price estimates');
39-
}
40-
});
41-
} catch (e) {
42-
console.error('Could not get price estimates');
31+
32+
if (typeof program.start !== 'string' && typeof program.end !== 'string') {
33+
// Output help if there are no arguments or flags.
34+
program.outputHelp();
35+
} else {
36+
try {
37+
service.executePriceEstimates(program.start, program.end, program.unit)
38+
.then(table => console.log(table))
39+
.catch((e) => {
40+
if (isDistanceExceededError(e)) {
41+
console.log(`Maximum distance of ${symbolService.getMaximumDistanceSymbol()} miles exceeded between start address: ${program.start} and end address: ${program.end}`);
42+
} else {
43+
console.error('Could not get price estimates:\n', e.message);
44+
}
45+
});
46+
} catch (e) {
47+
console.error('Could not get price estimates:\n', e.message);
48+
}
4349
}

src/executables/uber-time.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@ import CommandExecutionService from '../services/CommandExecutionService';
99
const service = new CommandExecutionService();
1010

1111
program
12-
.arguments('<address>')
13-
.action((address) => {
14-
try {
15-
service.executeTimeEstimates(address).then(table => console.log(table));
16-
} catch (Error) {
17-
console.error('Could not get time estimates');
18-
}
19-
})
12+
.usage('<address>')
13+
.description('Get Time-To-Pickup Estimates')
2014
.parse(process.argv);
15+
16+
const address = program.args.toString();
17+
18+
if (!address) {
19+
program.outputHelp();
20+
} else {
21+
try {
22+
service.executeTimeEstimates(address)
23+
.then(table => console.log(table))
24+
.catch((e) => {
25+
console.error('Could not get time estimates:\n', e.message);
26+
});
27+
} catch (e) {
28+
console.error('Could not get time estimates:\n', e.message);
29+
}
30+
}

src/executables/uber.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import program from 'commander';
55
import pkg from '../../package.json';
66

77
program.version(pkg.version)
8+
.description('Figure out if you should order a car to pick you up and drive you to where you want to go')
89
.command('price', 'get price estimate')
9-
.command('time', 'get time to pickup estimate')
10+
.command('time [address]', 'get time to pickup estimate')
1011
.parse(process.argv);
12+

src/services/CommandExecutionService.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ export default class CommandExecutionService {
2424
}
2525

2626
executePriceEstimates(startAddress, endAddress, distanceUnitName) {
27-
if (typeof startAddress !== 'string') {
28-
throw new TypeError('start address should be a string');
29-
}
30-
31-
if (typeof endAddress !== 'string') {
32-
throw new TypeError('end address should be a string');
27+
// commander.js doesn't support required arguments, and will always
28+
// interpolate arguments into strings.
29+
if (typeof startAddress !== 'string' || typeof endAddress !== 'string') {
30+
throw new TypeError(
31+
'Start and End addresses (-s \'<address>\' -e \'<address>\') are required.',
32+
);
3333
}
3434

3535
let distanceUnit = DistanceUnit.MILE;
@@ -52,7 +52,7 @@ export default class CommandExecutionService {
5252

5353
executeTimeEstimates(address) {
5454
if (typeof address !== 'string') {
55-
throw new TypeError('address should be a string');
55+
throw new TypeError('Address should be a string');
5656
}
5757

5858
return this.uberService.getTimeEstimates(address)

test/services/CommandExecutionServiceTest.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ describe('Command Execution Service', () => {
1717
const service = new CommandExecutionService();
1818

1919
describe('Price estimates', () => {
20-
it('throws for invalid start address type', () => {
21-
expect(() => service.executePriceEstimates(1, '', DistanceUnit.MILE)).to.throw(TypeError, 'start address should be a string');
20+
it('throws for empty start address type', () => {
21+
expect(() => service.executePriceEstimates(1, '', DistanceUnit.MILE)).to.throw(TypeError, 'Start and End addresses (-s \'<address>\' -e \'<address>\') are required.');
2222
});
2323

24-
it('throws for invalid end address type', () => {
25-
expect(() => service.executePriceEstimates('foo', 1, DistanceUnit.MILE)).to.throw(TypeError, 'end address should be a string');
24+
it('throws for empty end address type', () => {
25+
expect(() => service.executePriceEstimates('foo', null, DistanceUnit.MILE)).to.throw(TypeError, 'Start and End addresses (-s \'<address>\' -e \'<address>\') are required.');
2626
});
2727

2828
it('throws for undefined distance unit name', () => {
@@ -41,7 +41,7 @@ describe('Command Execution Service', () => {
4141

4242
describe('Time estimates', () => {
4343
it('throws for invalid address type', () => {
44-
expect(() => service.executeTimeEstimates(1)).to.throw(TypeError, 'address should be a string');
44+
expect(() => service.executeTimeEstimates(1)).to.throw(TypeError, 'Address should be a string');
4545
});
4646

4747
it('returns a value', () => {

0 commit comments

Comments
 (0)