0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cacとinquirerでコマンドを作ってみた

Last updated at Posted at 2022-05-20

cac

javascript製のCLI作成ライブラリ。cacはCommand And Conquerの頭文字を取ったものだそう。なぜConquerなのかは。。。

npmのcacのページリンク

import cac from 'cac'

const cli = cac()
cli.command('Scream <word>', 'Scream someting')
    .option('--color-red', 'font color red')
    .option('--color-blue', 'font color blue')
    .action((word, option) => {
        let fontColorEscape = ''
        if (option.colorRed) {
            fontColorEscape = '\x1b[31m'
        } else if (option.colorBlue) {
            fontColorEscape = '\x1b[34m'
        }

        console.log(`${fontColorEscape}${word}`)
    })

cli.help()
cli.parse()

inquirer

対話型CLIを簡単に作成できるjavascript製ライブラリ。

npmのinquirerのページリンク

import { prompt, QuestionCollection } from 'inquirer'

export default async (): Promise<void> => {
    // input
    const givenNameField: QuestionCollection = [
        {
            type: 'input',
            message: "What's your given name?",
            name: 'givenName'
        }
    ]
    const givenName = await prompt(givenNameField).then((givenName: any) => {
        return (givenName as { 'givenName': string })['givenName']
    })

    // required
    const familyNameField: QuestionCollection = [
        {
            type: 'input',
            message: "What's your family name?",
            name: 'familyName',
            validate: (answer: any): boolean | string => {
                if (answer.length < 1) {
                    return 'Required fields'
                }

                return true
            }
        }
    ]
    const familyName = await prompt(familyNameField).then((familyName: any) => {
        return (familyName as { 'familyName': string })['familyName']
    })


    // select
    const listQuestions: QuestionCollection = [
        {
            type: 'list',
            name: 'address',
            message: 'Where is your address?',
            choices: [
                'Shinjuku',
                'Shibuya',
                {
                    name: 'Yotsuya',
                    disabled: 'disabled'
                },
                'Nakano'
            ]
        }
    ]
    const address = await prompt(listQuestions).then((address: any) => {
        return (address as { 'address': string })['address']
    })

    // multiSelect
    const checkboxQuestions: QuestionCollection = [
        {
            type: 'checkbox',
            message: 'What is your favorite color?',
            name: 'color',
            choices: [
                { name: 'red' },
                { name: 'green' },
                { name: 'blue' }
            ]
        }
    ]
    const colors = await prompt(checkboxQuestions).then((answers: any) => {
        return (answers as { 'color': string[] })['color']
    })

    console.log(
        `Name: "${ givenName }  ${ familyName }"
        Address: "${ address }"
        Color: "${ colors.join(', ') }"`
    )
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?