simple-args-parser

A simple parser for command line arguments (process.argv[])

Usage no npm install needed!

<script type="module">
  import simpleArgsParser from 'https://cdn.skypack.dev/simple-args-parser';
</script>

README

simple-args-parser

Parse command line arguments (process.argv[]) in one function

Syntax:

args = parser.parse(process.argv, config, fallback?)

Config:

{
    short: [],
    long: [],
    stupid: [],
    errOnDisallowed: true
}

short[] is an array of short arguments (eg. -a)

long[] is an array of long arguments (eg. --long)

stupid[] is an array of stupid arguments (Long arguments with one -, eg -stupid)

If you want to get the argument parameter (--argument 'parameter'), add a : after the argument in the array

// Without argument
short: ['a']

// With argument
short: ['a:']

As you may have noticed, dont put the - with the argument into the array.

Fallback: Fallback is a function called when an error is recieved, called by fallback(err). If you dont specify a custon one, the error will be thrown upon encounter.

Example:

const parser = require('simple-args-parser')

args = parser.parse(process.argv, {
  short: ['a:', 'b:', 'c'],
  long: ['hello', 'world:'],
  stupid: ['powershell', 'bill:'],
  errOnDisallowed: true
}, (err) => {
  console.log(err)
})

console.log(args)

Try running this script like node test.js -a word -b 'this is a string' -c --hello --world indeed -powershell -bill 'gates'. It should output

{
  a: 'word',
  b: 'this is a string',
  c: true,
  hello: true,
  world: 'indeed',
  powershell: true,
  bill: 'gates'
}