Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

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

Open
ShrutheeshIR opened this issue Mar 9, 2019 · 4 comments
Open

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

ShrutheeshIR opened this issue Mar 9, 2019 · 4 comments

Comments

@ShrutheeshIR
Copy link

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?

@jdx
Copy link
Contributor

jdx commented Mar 9, 2019

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

@ShrutheeshIR
Copy link
Author

Can you please give a tiny example? @jdxcode

@jdx
Copy link
Contributor

jdx commented Mar 9, 2019

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

@MunifTanjim
Copy link

MunifTanjim commented May 25, 2020

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>>)
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants