vamsiampolu
vamsiampolu

Reputation: 6622

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) {
  program
    .version('0.0.1')
    .usage('redbubble-demo')
    .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)
    .parse(process.argv)

  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 program, i get no values.

I call my app without any arguments using:

babel-node src/index.js

UPDATE:

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 webpack to bundle it.

Upvotes: 1

Views: 3830

Answers (1)

Nick Tomlin
Nick Tomlin

Reputation: 29221

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. (Today I learned that this is a JSPM/babel-node alternative.)

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.

Upvotes: 2

Related Questions