npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

commander-parser

v0.1.2

Published

node.js command-line parser

Downloads

6

Readme

Commander-Parser.js

Build Status

A command-line parser for node.js like python's argparser, inspired by commander.js.

Command

var command = parser().command(<command>, <help>)

Argument

parser().argument(<name>, <help>, {
    "narg": <narg> ,
    "required": <boolean>,
    "dest": <dest>,
    "const": <conse>,
    "type": <type>
})

Option

parser().option(<flag>, <help>, {
    "action": <action> ,
    "required": <boolean>,
    "dest": <dest>,
    "const": <conse>,
    "type": <type>
})

Command

parser().command(<name>, <help>);

Methods

parser(name)

Return an instance of parser.Parser.

  • name: The name of the program(default: process.argv[1])

class parser.Parser(name)

Create a new Parser object.

  • name: The name of the program(default: process.argv[1])

action(name, handler, needInput)

  • name: The name of the Action.

  • handler: The handler of the Action.

    arguments: handler arguments.

    • value: raw value.
    • memo: dest memo.
    • options: options.

type(name, handler)

  • name: The name of the Type.

  • handler: The handler of the Type.

    arguments: handler arguments.

    • value: raw value.
    • error: error handler.
    • options: options.

Parser() Methods

usage(usage)

Return parser.

  • usage: The string describing the program usage(default: generated from arguments added to parser)

description(description)

Return parser.

  • description: The string describing the program.

addition(addition)

Return parser.

  • addition: Additional messages of the program.

argument(name, help, options={})

Return parser.

  • name: The name of the argument.
  • help: The string describing the argument.
  • options: Other options.
    • narg: How many arguments of type type should be consumed when this option is seen. If > 1, parser will store a array of values to dest.(default: 1)
    • required: The value is required.
    • default: The value to use for this argument’s destination if the argument is not seen on the command line.
    • dest: This tells parser where to write it.
    • type: The argument type expected (e.g., "string" or "int"); the available types are ["int", "float", "string", "choice", <function>].You can registry custom types by parser.type.
    • choices: For options of type "choice", the list of strings the user may choose from.

option(flag, help, options={})

Return parser.

  • flag: The flags of the option.
  • help: The string describing the option.
  • options: Other options.
    • action: Determines parser‘s behaviour when this option is seen on the command line; the available options are ["store", "store_const", "store_true", "store_false", "append", "append_const", "count"].(default "store") You can registry custom actions by parser.action.
    • required: The value is required.
    • default: The value to use for this argument’s destination if the argument is not seen on the command line.
    • dest: This tells parser where to write it.
    • type: The argument type expected (e.g., "string" or "int"); the available types are ["int", "float", "string", "choice", <function>].You can registry custom types by parser.type.
    • const: For actions that store a constant value, the constant value to store.
    • choices: For options of type "choice", the list of strings the user may choose from.

command(name, help)

Return command parser.

  • name: The name of the command.
  • help: The string describing the command.

help()

Print parser's usage.

exec(function)

Return parser.

  • function: This function will be called when calling parser.parse.

  • arguments:

    • options: The parsed command-line options.

parse(argv, withCommand=false)

parse command-line options

  • argv: command-line argvs with ["node", <script>].(default: process.argv)

  • withCommand: If this is a string.Parsed options'key "command" will be command name.

parseArgv(argv, withCommand=false)

like parse, but argv without ["node", <script>]

action(name, handler, needInput)

  • name: The name of the Action.

  • handler: The handler of the Action.

    arguments: handler arguments.

    • value: raw value.
    • memo: dest memo.
    • options: options.

type(name, handler)

  • name: The name of the Type.

  • handler: The handler of the Type.

    arguments: handler arguments.

    • value: raw value.
    • error: error handler.
    • options: options.

Examples

var parser = require("commander-parser");

parser()
    .option(['-f', '--file'], 'write file names', {
        'action': 'store_append',
        'required': true
    })
    .option('-a', 'append file', {
        'dest': 'mode',
        'action': 'store_const',
        'const': 'a'
    })
    .option('-w', 'write file', {
        'dest': 'mode',
        'action': 'store_const',
        'const': 'w'
    });