Skip to content

Instantly share code, notes, and snippets.

@kenanchristian
Last active September 21, 2020 06:46
Show Gist options
  • Save kenanchristian/ddcbc6beb1472b2ed85347011a8c9fda to your computer and use it in GitHub Desktop.
Save kenanchristian/ddcbc6beb1472b2ed85347011a8c9fda to your computer and use it in GitHub Desktop.
[[TypeScript CLI] Making the CLI Interactive] #cli #oclif
import {Command, flags} from '@oclif/command'
import {prompt} from 'inquirer'
export default class Create extends Command {
static description = 'Create a new Pizza'
static examples = [
`$ pizza create
Your pizza is ready!
`,
]
static flags = {
help: flags.help({char: 'h'}),
crust: flags.string({char: 'c', description: 'Type of Crust (Thin/Thick)'}),
toppings: flags.string({char: 't', description: 'Toppings to add', options: ['pepperoni', 'mushroom', 'bacon', 'pineapple'], multiple: true}),
extraSauce: flags.boolean({char: 'x', description: 'Do you want extra sauce?'}),
}
static args = [
{
name: 'count',
required: false,
description: 'How many pizza you want to create',
parse: (input: string) => parseInt(input, 10) || null,
default: null,
},
]
async getInteractiveArgs() {
const answer = await prompt([
{
type: 'number',
name: 'count',
message: 'How many pizza you want to create',
default: 1,
validate(value) {
if (isNaN(parseInt(value, 10))) {
return 'Pizza count should be a number'
}
return true
},
},
{
type: 'list',
name: 'crust',
message: 'How do you want you pizza crust be?',
default: 'Thin',
choices: ['Thin', 'Thick'],
},
{
type: 'checkbox',
name: 'toppings',
message: 'What do you want to add as toppings?',
default: '',
choices: ['Pepperoni', 'Mushroom', 'Bacon', 'Pineapple'],
validate(value) {
if (value.length === 0) {
return 'You should add at least 1 topping'
}
return true
},
},
{
type: 'confirm',
name: 'extraSauce',
message: 'Do you want extra sauce?',
default: false,
},
])
return answer
}
async run() {
// const {args, flags} = this.parse(Create)
const interactiveArgs = await this.getInteractiveArgs()
this.log(JSON.stringify(interactiveArgs))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment