parser icon indicating copy to clipboard operation
parser copied to clipboard

Is there a way to import flags from a Json File?

Open ShrutheeshIR opened this issue 5 years ago • 4 comments

I have a Json file with a properly value pairs. Can I import flags for my CLI from the JSON file or do they necessarily need to be hardcoded?

ShrutheeshIR avatar Mar 09 '19 18:03 ShrutheeshIR

yes, you could import the json and perform a data transformation into flags

jdx avatar Mar 09 '19 18:03 jdx

Can you please give a tiny example? @jdxcode

ShrutheeshIR avatar Mar 09 '19 18:03 ShrutheeshIR

that's outside the scope of what we're able to provide support for

jdx avatar Mar 09 '19 18:03 jdx

Hello @ShrutheeshIR,

Something like this should work (absolutely not tested):

flags.json

{
  "stringFlag": {
    ...
  },
  "numberFlag": {
    ...
  },
  "whateverFlag": {
    ...
  },
  ...
}

src/commands/doit.ts

import { Command } from '@oclif/command'
import * as Parser from '@oclif/parser'

import flagsJson from './flags.json'
 
type IFlag<T = any> = Parser.flags.IFlag<T>

const { stringFlag, numberFlag, whateverFlag, ...otherFlags } = flagsJson

class DoIt extends Command {
  static flags: typeof Command.flags = {
    stringFlag: stringFlag as IFlag<string>,
    numberFlag: numberFlag as IFlag<number>,
    whateverFlag: whateverFlag as IFlag,
    ...(otherFlags as Record<string, IFlag<any>>)
  }
}

MunifTanjim avatar May 25 '20 22:05 MunifTanjim