0.5.1 • Published 5 years ago

@aminnairi/command-parser v0.5.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

command-parser

npm.io npm.io npm.io npm.io npm.io npm.io

Command Line Arguments Parser

$ code my-cli
'use strict'

const { CommandParser } = require('@aminnairi/command-parser')

const name = 'my-cli'
const version = '0.1.0'
const synopsis = 'My Command Line Interface'
const parser = new CommandParser(name, version, synopsis)

const command = 'name'
const description = 'Your name'
parser.option(command, description)

const { name } = parser.parser()

console.log(`Hello, ${name}!`)
$ node my-cli --name you
Hello, you!

Installation

Using npm

$ npm install --save @aminnairi/command-parser

Or using yarn

$ yarn add @aminnairi/command-parser

Include

Using CommonJS

'use strict'

const { CommandParser, NO_VALUE_EXPECTED } = require('@aminnairi/command-parser')

Using ECMAScript Modules

'use strict'

import { CommandParser, NO_VALUE_EXPECTED } from '@aminnairi/command-parser'

Usage

Instanciation

const name = 'my-cli'
const version = '0.1.0'
const synopsis = 'My Command Line Interface'
const parser = new CommandParser(name, version, synopsis)

Arguments

Configuration

parser
  .option('name', 'Your name')
  .option('mood', 'Your mood')
  .option('robot', 'If you are a robot', NO_VALUE_EXPECTED)

Parsing

const { name, mood, robot } = parser.parse()

if (name) {
  console.log(`Hello, ${name}!`)
}

if (mood) {
  console.log(`Are you really ${mood}?`)
}

if (robot) {
  console.log(`01101100 01101111 01101100 right?`)
}

Command Line

Using node

Double-Dash Syntax

$ node my-cli --name John --mood happy --robot
Hello, John!
Are you really happy?
01101100 01101111 01101100 right?

Single-Dash Syntax

$ node my-cli -n John -m happy -r
Hello, John!
Are you really happy?
01101100 01101111 01101100 right?

Short Single-Dash Syntax

$ node my-cli -rn John -m happy
Hello, John!
Are you really happy?
01101100 01101111 01101100 right?

Version

$ node my-cli --version
my-cli version 0.1.0

Help

$ node my-cli --help
SYNOPSIS

    My Command Line Interface

OPTIONS

    -h, --help
        Display this message

    -v, --version
        Display the name and version of this command

    -n, --name [NAME]
        Your name

    -m, --mood [MOOD]
        Your mood

    -r, --robot
        If you are a robot

TypeScript

$ npm install --save typescript ts-node @aminnairi/command-parser
$ ./node_modules/.bin/tsc --init
$ code my-cli
'use strict'

import { CommandParser, ICommandParserOptions, NO_VALUE_EXPECTED } from '@aminnairi/command-parser'

const name: string = 'my-cli'
const version: string = '0.1.0'
const synopsis: string = 'My Command Line Interface'
const parser: CommandParser = new CommandParser(name, version, synopsis)

parser
  .option('name', 'Your name')
  .option('mood', 'Your mood')
  .option('robot', 'If you are a robot')

const { name, mood, robot }: ICommandParserOptions =  parser.parse()

if (name) {
  console.log(`Hello, ${name}!`)
}

if (mood) {
  console.log(`Are you really ${mood}?`)
}

if (robot) {
  console.log(`01101100 01101111 01101100 right?`)
}
$ ./node_modules/.bin/ts-node my-cli -rn John -m happy
Hello, John!
Are you really happy?
01101100 01101111 01101100 right?

Contributing

See CONTRIBUTING.md.

0.5.1

5 years ago

0.5.0

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago