我正在尝试构建一个使用 oclif 使用 Rest API 的 CLI。
我的代码如下:
import Command from '@oclif/command'
import axios from 'axios'
export class AggregatedCommand extends Command {
static args = [
{name: 'Area', required: true},
{name: 'AreaName', required : true},
{name: 'timeres', required: true},
{name: 'Resolution', required: true},
{name: 'ProdType',required: true},
{name: 'ProductionType',required:true},
{name: 'dateformat', required: true},
{name: 'dateinput', required: true},
{name: 'beforeformat', required: false},
{name: 'format' , required: false}
]
async run() {
const axios = require('axios');
const {args} = this.parse(AggregatedCommand);
//console.log(`${args.format}`);
if (`${args.dateformat}`=='--date'){
var splitted = `${args.dateinput}`.split("-", 3);
// console.log('http://localhost:8765/energy/api/AggregatedGenerationPerType/' +`${args.AreaName}` +'/' + `${args.ProductionType}` +'/' + `${args.Resolution}` +'/date/' + splitted[0] +'-' + splitted[1] + '-' + splitted[2]);
if (`${args.format}`== "undefined" || `${args.format}`=="json"){
const data= await axios.get('http://localhost:8765/energy/api/AggregatedGenerationPerType/' +`${args.AreaName}` +'/' + `${args.ProductionType}` +'/' + `${args.Resolution}` +'/date/' + splitted[0] +'-' + splitted[1] + '-' + splitted[2]);
console.log(data.data);
}
else if (`${args.format}`=="csv") {
const data= await axios.get('http://localhost:8765/energy/api/AggregatedGenerationPerType/' +`${args.AreaName}` +'/' + `${args.ProductionType}` +'/' + `${args.Resolution}` +'/date/' + splitted[0] +'-' + splitted[1] + '-' + splitted[2]+ "/format=" + `${args.format}`);
console.log(data.data);
}
else console.log("Error 400: Bad Request");
}
else if (`${args.dateformat}`=='--month'){
var splitted = `${args.dateinput}`.split("-", 2);
if (`${args.format}`== "undefined" || `${args.format}`=="json"){
const data= await axios.get('http://localhost:8765/energy/api/AggregatedGenerationPerType/' +`${args.AreaName}` +'/' + `${args.ProductionType}` +'/' + `${args.Resolution}` +'/month/' + splitted[0] +'-' + splitted[1]);
console.log(data.data);
}
else if (`${args.format}`=="csv") {
const data= await axios.get('http://localhost:8765/energy/api/AggregatedGenerationPerType/' +`${args.AreaName}` +'/' + `${args.ProductionType}` +'/' + `${args.Resolution}` +'/month/' + splitted[0] +'-' + splitted[1] + "/format=" + `${args.format}`);
console.log(data.data);
}
else console.log("Error 400: Bad Request");
}
我正在尝试输入如下命令:
energy AggregatedGenerationPerType --area United Kingdom --timeres PT15M --productionType AllTypes --year 2018
但是 oclif 和 typescript 不承认英国是一个参数,而是两个“United”和“Kingdom”。我该如何解决这个问题?