<a href="https://www.npmjs.org/package/opsh"><img src="https://img.shields.io/npm/v/opsh.svg?style=flat-square&labelColor=5085c0&color=dedcdb" alt="npm version"></a> <a href="https://bundlephobia.com/result?p=opsh"><img src="https://img.shields.io/bundlep
npm install opshAn argument processor for your Node.js command-line apps that gives you a helping hand in adhering to the POSIX guidelines, while supporting GNU-style long options.
Install the opsh package with npm:
``bash`
npm install opsh
Then use it in your executable file.
example.js:
`jsoption: ${option} = ${value}
#! /usr/bin/env node
let opsh = require('opsh');
opsh(process.argv.slice(2), {
option(option, value) {
console.log();operand or option-argument of ${opt}: ${item}
},
operand(operand, opt) {
if (opt) {
console.log();operand: ${item}
} else {
console.log();`
}
}
});
> Note: Before running your program for the first time, you need to run chmod +x example.js to make your file executable.
Running example.js produces this output:
`bash
./example.js -i input.txt --format=terse -n output.txt
option: i = undefined
operand or option-argument of i: input.txt
option: format = terse
option: n = undefined
operand or option-argument of n: output.txt
`
Let's unpack what's going on.
The way opsh works is it identifies options, option-arguments (that's an option's value), and operands based on the POSIX / GNU conventions, and not much more. Any further semantics is left to the library user.
In the command above, the meaning of input.txt is ambiguous. Without further information, opsh can't tell whether it is the option-argument of the -i option immediately preceding it, or an operand.
functionThe library exports a single opsh() function that can be invoked in two ways. The first argument _args_ is common to both styles, and represents the array of arguments to parse. You'll usually want to pass in process.argv.slice(2).
opsh(_args_: Array, _booleanOptions_: Array)
One way to sort out ambiguous input is to declare upfront which options are meant to be _boolean_, meaning they don't accept an option-argument. Providing a booleanOptions array as the second argument lets opsh sort out operands from option-arguments and return an object with these keys:
- options — options and their option-argument are present here in key-value pairs;operands
- — an array of operands.
This style of invoking opsh() will throw an error whenever a boolean option receives an option-operand, or a non-boolean option doesn't receive one.
When _booleanOptions_ is omitted, it defaults to the empty array [].
Let's take our previous invocation:
`bash`
./example.js -i input.txt --format=terse -n output.txt
And this time let's specify that -n is a boolean option and that, conversely, -i and --format accept values.
`js`
const args = '-i input.txt --format=terse -n output.txt'.split(' ');
opsh(args, ['n']);
This gives us the following result:
`js`
{
options: {
i: 'input.txt',
format: 'terse',
n: true
},
operands: ['output.txt']
}
opsh(_args_: Array, _walker_: Object)
Provide a _walker object_ as the second argument for full control on how the user input is interpreted. A walker object contains these keys (all optional):
`jsoption
opsh(process.argv.slice(2), {
/*
Invoked when visiting an option (),value
with or without an explicit option-argument ().
There might be a previous option left unsaturated
(unsaturated_option).
*/
option(option, value, unsaturated_option) {
// ...
},
/*
Invoked when visiting an operand (operand) or unsaturated_option
possible option-argument when the previous option
was left unsaturated ().
*/
operand(operand, unsaturated_option) {
// ...
},
/*
Invoked when visiting the -- delimiter.unsaturated_option
There might be a previous option left unsaturated
().`
*/
delimiter(delimiter, unsturated_option) {
// ...
}
});
Returning false from any walker function stops further traversal.
Short options with their argument separated by , such as -n10`, are not supported.