node.js - yargs 仅采用命令行输入字符串的第一个单词

标签 node.js yargs

我正在教程中开发一个node.js命令行天气应用程序,我意识到当我输入一个字符串作为输入时,仅采用第一个单词,该字符串被拆分为一个单词数组,并且仅第一个单词返回

app.js

const yargs = require('yargs');
const geocode = require('./geocode/geocode.js');
const argv = yargs
.options({
	a: {
		demand: true,//this argument is require
		alias: 'address',
		describe: 'Address to fetch weather for',
		string: true//always parse the address argument as a string
	}
})
.help()
.alias('help', 'h')
.argv;
geocode.geocodeAddress(argv.address, (errorMessage, results) => {
	if(errorMessage){
		console.log(errorMessage);
	}else{
		console.log(JSON.stringify(results, undefined, 4));
	}
});

地理编码.js

const request = require('request');


let geocodeAddress = (address, callback)=>{
	let encodedAddress = encodeURIComponent(address);
	request({
		url:`https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}`,
		json:true
	}, (err, response, body)=>{
		if(err){
			callback('unable to connect to service');
		}else if(body.status === 'ZERO_RESULTS'){
			callback('unable to find address');
		}else if(body.status === 'OK'){
			callback(undefined, {
				address: body.results[0].formatted_address,
				latitude: body.results[0].geometry.location.lat,
				longitude: body.results[0].geometry.location.lng

			});
		}
		
	});
}

module.exports.geocodeAddress = geocodeAddress;

here is the output when i run the code

最佳答案

你的代码没有问题,这是Windows命令行的行为。 执行命令时请使用双“”而不是“”。在第一个空格之后,所有参数都将在 Windows 上丢失。

所以运行:

node app.js -a "lombard street"

而不是

node app.js -a 'lombard street'

关于node.js - yargs 仅采用命令行输入字符串的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47915586/

相关文章:

javascript - Next.js 警告 : Warning: Each child in a list should have a unique "key" prop

javascript - TypeError : Cannot read property 'create' of undefined. 使用 sequelize 添加和登录用户

javascript - 无法读取未定义的sequelize Express JS 的属性 'findAll'

node.js - 面对这个错误 : TypeError: yargs. 命令不是函数

javascript - npm 脚本使用 yargs 将参数/参数传递给节点脚本

javascript - SyntaxError :/Users/3x7r3m157/Development/Javascript/db. json:JSON 输入意外结束

node.js - 如何在我的网络中的开发服务器上使用 (Node) Livereload

Node.js Sequelize 无法读取 nulll 的属性

javascript - 我将如何配置 yargs 以获取单个输入文件和可选输出文件?

javascript - 使用 yargs 和 gulp-if 分离/组合 gulp 任务源