yargs只占用命令行输入字符串的第一个单词

问题描述 投票:1回答:1

我正在从一个教程中的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));
	}
});

geocode.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

node.js yargs
1个回答
3
投票

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

所以运行:

node app.js -a "lombard street"

代替

node app.js -a 'lombard street'
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.