# Parsing command options in Javascript

Poll:
What's your favourite option parser library?

* Oclif
* Commander.js
* Ink
* Inquirer.js

Javascript is a language that was developed for the web, but its usefulness has turned out to go far beyond just the Internet.
Today, thanks to projects like Node.js and Electron, Javascript is as much a general-purpose scripting language as it is a browser component.
In fact, there are Javascript libraries especially designed to build command-line interfaces.
Yes, you can run Javascript in your terminal.

Now, when you enter a command into your terminal, there are generally LINK-TO-UNDERSTANDING-LINUX-TERMINAL[options], also called _switches_ or _flags_, that you can use to modify how the command runs.
This is a useful convention defined by the https://opensource.com/article/19/7/what-posix-richard-stallman-explains[POSIX specification], so as a programmer it's useful to know how to detect and parse options.
To get this functionality from Javascript, it's useful to use a library designed to make it easy to build command-line interfaces.
My favourite is [Commander.js](https://github.com/tj/commander.js).
It's easy, it's flexible, and it's intuitive.

## Installing node

To use the Commander.js library, you must have Node.js installed.
On Linux, you cane install Node using your package manager.
For example, on Fedora, CentOS, Mageia, and others:

```bash
$ sudo dnf install nodejs
```

On Windows and macOS, [download installers from the nodejs.org website](https://nodejs.org/en/download).

## Installing Commander.js

To install Commander.js, use the `npm` command:

```bash
$ npm install commander
```

## Adding a library to your Javascript code

In Javascript, you can use the `require` key word to include (or import, if you're used to Python) a library into your code.
Create a file called `example.js` and open it in your favourite text editor.
Add this line to the top to include the Commander.js library:

```javascript
const { program } = require('commander');
```

## Option parsing in Javascript

The first thing you must do to parse options is to define the valid options your application can accept.
The Commander.js library lets you define both short and long options, along with a helpful message to clarify the purpose of each.

```javascript
program
 .description('A sample application to parse options')
 .option('-a, --alpha', 'Alpha')
 .option('-b, --beta <VALUE>', 'Specify a VALUE', 'Foo');
```

The first option, which I've called `--alpha` (`-a` for short) is a Boolean switch: it either exists or it doesn't.
It takes no arguments.
The second option, which I've called `--beta` (`-b` for short) accepts an argument, and even specifies a default value when nothing has been provided.

## Accessing the command-line data

Once you've defined valid options, you can reference the values using the long option name:

```javascript
program.parse();

const options = program.opts();
console.log('Options detected:');

if (options.alpha) console.log('alpha');

const beta = !options.beta ? 'no' : options.beta;
console.log('beta is: %s', beta);
```

## Run the application

Try running it with the `node` command, first with no options:

```bash
$ node ./example.js
Options detected:
beta is: Foo
```

The default value for `beta` is used in absence of an override from the user.

Run it again, this time using the options:

```bash
$ node ./example.js --beta hello --alpha
Options detected:
alpha
beta is: hello
```

This time, the test script successfully detected the option `--alpha`, and the `--beta` option with the value provided by the user.


== Option parsing

Here's the full demonstration code for your reference:

```javascript
const { program } = require('commander');

program
 .description('A sample application to parse options')
 .option('-a, --alpha', 'Alpha')
   .option('-b, --beta <VALUE>', 'Specify a VALUE', 'Foo');

program.parse();

const options = program.opts();
console.log('Options detected:');

console.log(typeof options);

if (options.alpha) console.log(' * alpha');
const beta = !options.beta ? 'no' : options.beta;
console.log(' * beta is: %s', beta);
```

There are further examples in the project's [Git repository](https://github.com/tj/commander.js).

Including options for your users is an important feature for any application, and Commander.js makes it easy to do.
There are other libraries aside from Commander.js, but I find this one easy and quick to use.
What's your favourite Javascript command-line builder?