vamsiampolu vamsiampolu - 11 months ago 57
Node.js Question

Commander not using default values

I setup commander with defaultArgs like this:

import * as validations from './validation'
import program from 'commander'
import path from 'path'

export default function initializeCommander (defaults) {
.option('-u', '--url [url]', validations.isValidUrl, defaults.url)
.option('-o', '--output-dir [path]', validations.hasValidParentDirectory, path.resolve(defaults.path))
.option('-s', '--size [size]', validations.isValidSize, defaults.size)

return program

I have the default values set up here and pass them to the function like this:

const defaultValues = {
url: 'someuRL',
size: 'medium',
outputDir: './www'
const program = initializeCommander(defaultValues)
console.log(program) //no value in console

When I try and access the options from
, i get no values.

I call my app without any arguments using:

babel-node src/index.js


I am able to run the app as a binary now using
npm link
but i get this error:

TypeError: Path must be a string. Received undefined
at assertPath (path.js:7:11)
at Object.resolve (path.js:1146:7)
at initializeCommander (/home/vamsi/Do/redbubble-demo/build/index.js:9889:222)
at Object.<anonymous> (/home/vamsi/Do/redbubble-demo/build/index.js:87:34)
at __webpack_require__ (/home/vamsi/Do/redbubble-demo/build/index.js:22:30)
at /home/vamsi/Do/redbubble-demo/build/index.js:42:18
at Object.<anonymous> (/home/vamsi/Do/redbubble-demo/build/index.js:45:10)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)

The line numbers in the
stack trace
are off because I am using
to bundle it.


I'm curious as to why you are using webpack to bundle a Node cli. Traditionally bundlers like webpack or browserify are used to replicate the node module system for client side projects, not for server side ones.

Commander is throwing an exception because your default is called outputDir not path but you are passing path.resolve(defaults.path)) in your commander definition.