typescript - 在 Oclif CLI 中包含以空格分隔的参数
问题描述
我正在尝试构建一个使用 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”。我该如何解决这个问题?
解决方案
由于您已经在示例调用中使用了标志,因此您应该切换到它们而不是 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
推荐阅读
- gradle - Gradle 4 升级复杂 task.execute() 替换
- python - 我正在尝试更改 Dash 应用程序的布局
- python - 使用 xml.etree.ElementTree 内容从我的 XML 中消失
- angular - 未根据 ngFor 循环中的状态检查复选框
- c++ - 嵌套命名空间c ++中的大数组
- tensorflow - 为什么机器学习训练在一个 gpu 上失败但在另一个 gpu 上运行?
- python - 在 sklearn 模型上调用 .fit() 时的 SettingWithCopyWarning
- mongodb - 如何在 spring-data-mongodb 中编写 UpdateMany mongo 查询
- javascript - jquery 类型在外部函数中不可用
- jsp - 无法让 JSTL 标记与 Tomcat 9 一起使用,尝试了所有解决方案均无效