Reputation:
I am trying to build a CLI that consumes a Rest API using oclif.
My code is the following:
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");
}
I am trying to input a command like the following:
energy AggregatedGenerationPerType --area United Kingdom --timeres PT15M --productionType AllTypes --year 2018
But oclif and typescript do not recognize the United Kingdom as one parameter but as two "United" and "Kingdom". How do I solve this?
Upvotes: 1
Views: 898
Reputation: 70
As you already using flags in your example call you should switch to them instead of args. (see https://oclif.io/docs/flags).
Arguments are positional arguments passed to the command
Flag options are non-positional arguments passed to the command
Example:
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')
}
}
Then you can pass
energy AggregatedGenerationPerType --area "United Kingdom" --timeres PT15M --productionType AllTypes --year 2018
Upvotes: 1