Включение параметров, разделенных пробелом, в Oclif CLI
Я пытаюсь создать CLI, который использует Rest API, используя oclif.
Мой код следующий:
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 и машинописный текст распознают Соединенное Королевство не как один параметр, а как два "United" и "Kingdom". Как мне решить эту проблему?
1 ответ
Решение
Поскольку вы уже используете флаги в своем примере вызова, вам следует переключиться на них вместо args. (см. https://oclif.io/docs/flags).
Аргументы - это позиционные аргументы, передаваемые команде
Параметры флага - это непозиционные аргументы, передаваемые команде
Пример:
export class AggregatedCommand extends Command {
static flags = {
area: flags.string(),
timeres: flags.string(),
...
}
async run() {
const {flags} = this.parse(AggregatedCommand)
if (flags.area) console.log('--area is set')
if (flags.timeres) console.log('--timeres is set')
}
}
Тогда вы можете пройти
energy AggregatedGenerationPerType --area "United Kingdom" --timeres PT15M --productionType AllTypes --year 2018