1.1.3 • Published 8 years ago

kickass v1.1.3

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

kickass

A commander to parse arguments and manage sub commands for NodeJS.

Install

npm i -S kickass

Example

import { Command } from 'kickass'

const { name, version, desc } = require('../package.json')

const action = (args: any, cmd: Command) => console.log(
  `Executing <${cmd.name}> with arguments: ${JSON.stringify(args, void 0, '  ')}`
)
const passby = (args: any, cmd: Command) => console.warn(
  `Passby <${cmd.name}> with arguments: ${JSON.stringify(args, void 0, '  ')}`
)

new Command(name, process.argv.slice(2), action)
  .version(version)
  .desc(desc)
  .option('-g, --global [value] global optional value')
  .passby(passby)

  .command('option', action)
  .option('-s, --string ?<string> shortcut, value is optional, value, option is'
          + ' required, description')
  .option('--no-short ?<no shortcut> **no shortcut**, value is optional, value, option'
          + ' is required, description')
  .option('-rv, --required-value <require> shortcut, **value is required**, value, option'
          + ' is required, description')
  .option('-oo, --optional-option ?[optional] shortcut, value is optional, value,'
          + ' **option is optional**, description')
  .option('-rvoo, --required-value-optional-option [optional] shortcut, **value is'
          + ' required, value, **option is optional**, description')
  .option('-nv, --no-value shortcut, value is optional, **no value**, **option is'
          + ' optional**, description')
  .option('-nd, --no-description ?<no desc>')
  .option('-hv, --handled-value ?<handled> handled value by handler',
    (value: string) => value === void 0 ? 'default value' : value)
  .option('-dv, --default-value ?<default> default value to required option with'
          + ' optional value', '.tsx')
  .option('-dvo, --default-value-optional ?[default 2] default value to optional option'
          + ' and optional value, if passed the argument name without value, well invoke'
          + ' default value to handler(if specified), else if not passed the argument,'
          + ' well use default value directly without invoke handler',
    (value: string) => value + value,
    '[one]')
  .boolean('-sb, --shortcut-boolean boolean shortcut method', true)
  .number('-sn, --shortcut-number number shortcut method', 0)
  .json('-sj, --shortcut-json json parsed', null)
  .array('-sa, --shortcut-array split by ,', [])
  .rest('rest', false, (args: string[]) => args.map(parseInt))

  .parent()
  .command('child-command', action)
  .command('global-required', action)
  .option('-g, --global ?<required> no default, will pass undefined if no value passed')
  .command('action', action)
  .rest('actions')
  .parent()
  .parent()
  .command('passby', action)
  .option('-p, --pass [value] passby value for parent', false)
  .passby(passby)
  .command('action', action)
  .option('-a,--action action without value')
  .exec()

This code is in src/Command.example.ts. you can get it by this get the generated help information.

Run this code in terminal result:

ts-node src/Command.example.ts option -h

kickass
Version: 1.1.2

Usage: kickass [options] option [options] [...rest]

Commands:
    kickass [options] 
    kickass [options] option [options] [...rest] 
    kickass [options] child-command 
    kickass [options] child-command global-required [options] 
    kickass [options] child-command global-required [options] action [...actions] 
    kickass [options] child-command passby [options] 
    kickass [options] child-command passby [options] action [options] 

Options:
        -v,--version: optional
                output version of the command
        -h,--help: optional
                output help information
        -s,--string [string]: required
                shortcut, value is optional, value, option is required, 
                description
        --no-short [no shortcut]: required
                **no shortcut**, value is optional, value, option is required, 
                description
        -rv,--required-value <require>: required
                shortcut, **value is required**, value, option is required, 
                description
        -oo,--optional-option [optional]: optional
                shortcut, value is optional, value, **option is optional**, 
                description
        -rvoo,--required-value-optional-option <optional>: optional
                shortcut, **value is required, value, **option is optional**, 
                description
        -nv,--no-value: optional
                shortcut, value is optional, **no value**, **option is 
                optional**, description
        -nd,--no-description [no desc]: required
        -hv,--handled-value [handled]: required
                handled value by handler
        -dv,--default-value [default]: required
                default value to required option with optional value
        -dvo,--default-value-optional [default 2]: optional
                default value to optional option and optional value, if passed 
                the argument name without value, well invoke default value to 
                handler(if specified), else if not passed the argument, well 
                use default value directly without invoke handler
        -sb,--shortcut-boolean: optional
                boolean shortcut method
        -sn,--shortcut-number: optional
                number shortcut method
        -sj,--shortcut-json: optional
                json parsed
        -sa,--shortcut-array: optional
                split by ,
git clone https://github.com/acrazing/kickass.git

cd kickass

npm i --production

npm i -g typescript typings

typings i

tsc

node ./lib/Command.example.js --help

######### OR #########

cd a/npm/project/path
# OR npm init -y

npm i kickass

node node_modules/lib/Command.example.js -h

API

API

motivation

There are so many command managers for NodeJS, just like minimist, commander, etc, and I use commander very frequently. But that has some problem about sub command and optional options. see https://github.com/tj/commander.js/issues/44

just so, I write this.

  • The api is almost same to commander, but, in this package, option is optional or required, and option's value is optional or required is explicit. They are non-interference in each other.
  • A complete support for child commands, the registered commands looks like a tree, each one has its required or optional options, and has it executor. The options were separated by command name between commands. And additionally, a active command's parent could be invoked passby.

Enjoy It~

License

MIT

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago