Socket
Book a DemoInstallSign in
Socket

kitten-cli

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kitten-cli

The purrfect Command Line parsing library for Node.js

1.2.3
latest
Source
npmnpm
Version published
Weekly downloads
1
-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

DEPRECATION NOTICE

This library has been depreciated. Use classy-commander or commander instead!


kitten-cli

The purrfect Command Line parsing library for Node.js console apps!

Description

Kitten CLI enables rapid development of command line apps / tasks in node.

Installation

npm install kitten-cli --save

Usage

Simple Hello World Example

index.js

var cli = require('kitten-cli');

cli
    .command('greet')
    .value('name')
    .action(greet);

cli.run();

function greet(command) {
    console.log('Hello ' + command.value('name'));
}

Running node index.js greet World would print Hello World to the console

Importing

var cli = require('kitten-cli');

Registering Commands

There are 2 ways to register commands:

  • You can use the fluent API:
cli
    .command('login')
    .value('user-name')
    .value('password')
    .option('-r', '--remember-me')
    .action(login)
    
    .command('logout')
    .action(logout);
  • Or declare your commands in a file using the intuitive DSL:

commands.txt

login <user-name> <password> -r|--remember-me
logout

Then load your commands from the file:

index.js

cli.commandsFromFile('commands.txt');

Note that if you use the DSL and you want to associate actions with your commands you still need to wire up your actions via the fluent API afterwards.

cli
    .command('login').action(login)
    .command('logout').action(logout);

Using options

You can declare options for a command. Note that you can specify multiple names for an option.

index.js

cli
    .command('greet')
    .value('name')
    .option('-s', '--shout')
    .action(showGreeting);

cli.run();

...

Then check them in your action handler:

...

function showGreeting(command) {
    var message = 'Hi ' + command.value('name');
    
    if (command.option('shout')) {
        message = message.toUpperCase();
    }
    
    console.log(message);
}

Running node index.js greet Zoidberg will print Hi Zoidberg

Running node index.js greet Zoidberg -s will print HI ZOIDBERG

Options with values

If you declare a value right after declaring an option, the value will be associated with the option (not the command).

cli
    .command('bio')
    .value('first-name')
    .option('-l', '--last-name').value('last-name')
    .option('-a', '--age').value('age')
    .action(showBio);

function showBio(command) {
    var message = 'First Name: ' + command.value('first-name');
    
    if (command.option('last-name')) {
        message += '\nLast Name' + command
            .option('last-name')
            .value('last-name');
    }

    if (command.option('age')) {
        message += '\nAge:' + command
            .option('age')
            .value('age');
    }
    
    console.log(message);
}

Running node index.js bio Rick --last-name Sanchez --age 60 would print:

First Name: Rick
Last Name: Sanchez
Age: 60

Got an Issue / Feature Request?

Then please add an issue on GitHub! 😺

Keywords

cli

FAQs

Package last updated on 29 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.