Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aminnairi/node-natural
https://github.com/aminnairi/node-natural
Last synced: 11 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/aminnairi/node-natural
- Owner: aminnairi
- License: gpl-3.0
- Created: 2021-03-28T09:15:29.000Z (over 3 years ago)
- Default Branch: next
- Last Pushed: 2021-03-28T09:33:08.000Z (over 3 years ago)
- Last Synced: 2024-10-11T18:11:41.464Z (about 1 month ago)
- Language: JavaScript
- Size: 68.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# node-natural
Parse options using a natural language
[![Tests](https://github.com/aminnairi/node-natural/actions/workflows/test.yaml/badge.svg?branch=next)](https://github.com/aminnairi/node-natural/actions/workflows/test.yaml) [![Publish](https://github.com/aminnairi/node-natural/actions/workflows/publish.yaml/badge.svg?branch=latest)](https://github.com/aminnairi/node-natural/actions/workflows/publish.yaml)
## Requirements
- Node
## Usage
```console
$ npm install @aminnairi/natural
$ touch hello
``````javascript
#!/usr/bin/env nodeimport {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}!`);
``````console
$ 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
```