我像这样使用 defaultArgs 设置指挥官:
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
}
我在这里设置了默认值并将它们传递给函数,如下所示:
const defaultValues = {
url: 'someuRL',
size: 'medium',
outputDir: './www'
}
const program = initializeCommander(defaultValues)
console.log(program) //no value in console
当我尝试从中访问选项时program
,我没有得到任何值。
我调用我的应用程序没有任何参数使用:
babel-node src/index.js
更新:
我现在可以将应用程序作为二进制文件运行,npm link
但出现此错误:
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)
中的行号stack trace
是关闭的,因为我正在使用webpack
它来捆绑它。