首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Oclif CLI中包含空间分离的参数

在Oclif CLI中包含空间分离的参数
EN

Stack Overflow用户
提问于 2020-01-17 12:16:46
回答 1查看 528关注 0票数 1

我试图构建一个使用oclif消耗Rest的CLI。

我的代码如下:

代码语言:javascript
复制
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");
    }

我试图输入如下命令:

代码语言:javascript
复制
energy AggregatedGenerationPerType --area United Kingdom  --timeres PT15M --productionType AllTypes --year 2018 

但oclif和打字本不承认联合王国是一个参数,而是两个“统一”和“王国”。我该怎么解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-17 13:53:54

由于您已经在示例调用中使用了标志,所以您应该切换到它们,而不是args。(见https://oclif.io/docs/flags)。

参数是传递给命令的位置参数

标志选项是传递给命令的非位置参数。

示例:

代码语言:javascript
复制
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')
  }
}

然后你可以通过

代码语言:javascript
复制
energy AggregatedGenerationPerType --area "United Kingdom"  --timeres PT15M --productionType AllTypes --year 2018
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59787188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档