parser
parser copied to clipboard
Is there a way to import flags from a Json File?
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?
yes, you could import the json and perform a data transformation into flags
Can you please give a tiny example? @jdxcode
that's outside the scope of what we're able to provide support for
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>>)
}
}