Today, in the natural way in nodejs, our CLI arguments are passed into something called process.argv
as a string array.
The problem with that, is that it's not really 'extremely easy to use' in your code.
You'd have to do something like the following code (each time):
const arg = process.argv.find((argument: string) => argument.indexOf('--arg=') !== -1);
const value = arg.split('=')[1];
The code above looks for an item in the array that will contain part of your argument, for this example your argument is --arg
.
Not only that, afterwards you need to only take the value in order to use it.
Maybe the code is not complex, but for me it was quite annoying to always handle in my personal/work projects.
A Better solution:
In order to solve my issue, I brought to life a NPM package called cli-argument-parser
, this package gives you to ability to address a JS object that will contain only the relevant arguments and their values.
How does it work?
After running npm install cli-argument-parser
to install it,
You can try the following code snippet:
import { cliArguments } from 'cli-argument-parser';
const arg1 = cliArguments.arg1;
const arg2 = cliArguments.arg2;
The package, works based on arguments prefixes (--
as default) and separators (=
as default).
According to the code snippet above, if I pass the arguments --arg1=value1 arg2=value2
, the variable arg1 will hold the correct value, but variable arg2 will hold the value of undefined, because it was filtered out based on the default prefix of --
,
If we passed --arg2=value2
, the arg2 variable will hold the correct value of value2.
Command line configuration:
Even though I honestly favor using arguments this way --arg=value
, people are different so I made sure we can configure unique styles of argument filtering.
Using a configuration file
A way to achieve this is to create a file called cli.config.json
in the root directory of your project. You will need to set both prefix and separator like the following:
{
prefix: '-',
separator: '~'
}
This configuration means, that you can now pass argument of -arg~value
and it will know to filter the correct arguments and build it in the cliArguments
JS object.
Using CLI arguments
If additional files are a mess in your opinion, it is also possible to pass the CLI configuration via CLI arguments.
--cli-prefix
to configurate the CLI prefix, ie: --cli-prefix=--
--cli-separator
to configurate the CLI separator, ie: --cli-separator==
Using the code
Instead of using the existing cliArguments
, you are able to also create a custom one, using the following code snippet:
import { filterArguments } from 'cli-argument-parser';
const arguments = filterArguments('--', '=');
The arguments
variable will hold a JS object with arguments (just like cliArguments
) filtered by defined prefix and separator.
In a few words:
Maybe the problem is not the biggest, but definitely makes it a bit easier to read CLI arguments using this package.
Feel free to share your thoughts and take a peek here and don't be shy to ⭐ it ;)
Top comments (2)
It seems straightforward and simple to use. I'll give it a shot.
Thanks! very useful!