@aminnairi/natural

0.1.1 • Public • Published

node-natural

Parse options using a natural language

Tests Publish

Requirements

  • Node

Usage

$ npm install @aminnairi/natural
$ touch hello
#!/usr/bin/env node

import {createNaturalOptionParser} from "@aminnairi/natural";

const parse = createNaturalOptionParser([
  {name: "help", option: "help", isBoolean: true, isMultiple: false},
  {name: "name", option: "name", isBoolean: false, isMultiple: false},
  {name: "countries", option: "country", isBoolean: false, isMultiple: true}
]);

const [parsed, unknown, lone] = parse(process.argv.slice(2));

if (lone.length > 0) {
  console.error(`Lone argument: ${lone[0]}`);
  process.exit(1);
}

if (unknown.length > 0) {
  console.error(`Unknown argument: ${unknown[0]}`);
  process.exit(2);
}

if (parsed.name === undefined) {
  console.error(`Missing argument: name`);
  process.exit(3);
}

if (parsed.help) {
  console.log("Usage:");
  console.log("  hello name You");
  console.log("  hello name You country France");
  console.log("  hello name You country France country Spain");
  process.exit(0);
}

if ((parsed.countries ?? []).length > 0) {
  console.log(`Hello ${parsed.name} from ${parsed.countries.join(", ")}!`);
  process.exit(0);
}

console.log(`Hello ${parsed.name}!`);
$ chmod +x hello

$ ./hello
Missing argument: name

$ ./hello name
Lone argument: name

$ ./hello name You hello
Unknown argument: hello

$ ./hello name You
Hello You!

$ ./hello name You country France
Hello You from France!

$ ./hello name You country France country Spain
Hello You from France, Spain!

$ ./hello name You country France country Spain help
Usage:
  hello name You
  hello name You country France
  hello name You country France country Spain

Package Sidebar

Install

npm i @aminnairi/natural

Weekly Downloads

0

Version

0.1.1

License

GPL-3.0-or-later

Unpacked Size

38.8 kB

Total Files

5

Last publish

Collaborators

  • aminnairi