student student - 1 year ago 103
Node.js Question

How is it possible for a string to be typed as Number in npm package commandLineArgs

In the below code from MongoDB's course Week 3's Query Operators in the Node.js Driver chapter :

var MongoClient = require('mongodb').MongoClient,
commandLineArgs = require('command-line-args'),
assert = require('assert');

var options = commandLineOptions();

MongoClient.connect('mongodb://localhost:27017/crunchbase', function(err, db) {

assert.equal(err, null);
console.log("Successfully connected to MongoDB.");

var query = queryDocument(options);
var projection = {
"_id": 1,
"name": 1,
"founded_year": 1,
"number_of_employees": 1,
"crunchbase_url": 1

var cursor = db.collection('companies').find(query, projection);
var numMatches = 0;

function(doc) {
numMatches = numMatches + 1;
function(err) {
assert.equal(err, null);
console.log("Our query was:" + JSON.stringify(query));
console.log("Matching documents: " + numMatches);
return db.close();


function queryDocument(options) {


var query = {
"founded_year": {
"$gte": options.firstYear,
"$lte": options.lastYear

if ("employees" in options) {
query.number_of_employees = {
"$gte": options.employees

return query;


function commandLineOptions() {

var cli = commandLineArgs([{
name: "firstYear",
alias: "f",
type: Number
}, {
name: "lastYear",
alias: "l",
type: Number
}, {
name: "employees",
alias: "e",
type: Number

var options = cli.parse()
if (!(("firstYear" in options) && ("lastYear" in options))) {
title: "Usage",
description: "The first two options below are required. The rest are optional."

return options;


I'm requiring
package, which has a method
. All good and fine...

Now, I see that the
of the objects passed to this method is set to
. We can clearly see that they're

How is it possible?

Answer Source

From the command-line-args GitHub page:

The type value is a setter function (you receive the output from this), enabling you to be specific about the type and value received.

In other words, passing Number as type allows you to parse the arguments as numbers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download