项目作者: ollikekalainen

项目描述 :
Command Line Parser
高级语言: JavaScript
项目地址: git://github.com/ollikekalainen/clparser.git
创建时间: 2019-03-01T15:12:49Z
项目社区:https://github.com/ollikekalainen/clparser

开源协议:MIT License

下载


clparser

Simple command line parser for node

Installation

  1. npm install clparser

Usage

Class interface

Initialization

  1. const clp = require("clparser");
  2. const parser = new clp.ClParser({
  3. offset: <number>, // default: 2
  4. optionPrefix: <string>, // "/", "-", or "--", default: "/"
  5. caseSensitive: <boolean> // default: false
  6. });

Properties

  1. parser.offset Number, default: 2
  2. parser.optionPrefix String, "/", "-", or "--", default: "/"
  3. parser.parameterCount Number of parameters (readonly, no options or switches)
  4. parser.options Array of objects [{ option<1>: value }, ..., { option<n>: value }] (readonly).
  5. Does not follow the caseSensitive option.
  6. parser.parameters Array (readonly, no options or switches)
  7. parser.switches Array of strings [ switch<1>, ..., switch<n> ] (readonly).
  8. Does not follow the caseSensitive option.

Methods

  1. parser.option(name) Get the value of the named option
  2. parser.parameter(index) Get the command line parameter indicated by index
  3. parser.switch(name) Returns boolean value that indicates whether the named switch exist

Function interface

Initialization

  1. const clp = require("clparser");
  2. clp.setPreferences({
  3. offset: <number>, // default: 2
  4. optionPrefix: <string>, // "/", "-", or "--", default: "/"
  5. caseSensitive: <boolean> // default: false
  6. });

Functions

  1. parser.getOption(name) Returns the value of the named option.
  2. parser.getOptions() Returns an array of option objects
  3. [{ option<1>: value }, ..., { option<n>: value }] (readonly).
  4. Does not follow the caseSensitive option.
  5. parser.getParameter(index) Returns the command line parameter indicated by index.
  6. parser.getParameterCount() Returns the number of parameters (no options or switches).
  7. parser.getParameters() Returns parameters as array (no options or switches).
  8. parser.isSwitch(name) Returns a boolean value that indicates whether the named switch exist.
  9. parser.getSwitches() Return an array of strings [ switch<1>, ..., switch<n> ] (readonly).
  10. Does not follow the caseSensitive option.

Example

  1. >
  2. >node test.js /command:copy source.txt "target 42.txt" /silent /i /tries:3
  3. >
  4. const clp = require("clparser");
  5. const parser = new clp.ClParser({ offset: 2 });
  6. console.log(parser.option("command")) -> "copy"
  7. console.log(parser.options) -> [{ command: "copy", tries:"3" }]
  8. console.log(parser.parameterCount) -> 2
  9. console.log(parser.parameters) -> ["source.txt","target.txt"]
  10. console.log(parser.parameter(0)) -> "source.txt"
  11. console.log(parser.parameter(1)) -> "target 42.txt"
  12. console.log(parser.switch("silent")) -> true
  13. console.log(parser.switches) -> ["silent","i"]