hac_k

from @abdfnx

CLI app messenger & helper

Features

Install

❯ npm i @abdfnx/hac_k

Usage

❯ ./foo-app.js xbox --xbox

CommonJS

#!/usr/bin/env node
"use strict";
const hac_k = require("hac_k");
const foo = require(".");

const cli = hac_k(
	`
	Usage
	  ❯ foo <input>

	Options
	  --xbox, -x  Include a xbox

	Examples
	  ❯ foo consoles --xbox
	  🎮 consoles 🎮
`,
	{
		flags: {
			xbox: {
				type: "boolean",
				alias: "x",
			},
		},
	}
);
/*
{
	input: ['consoles'],
	flags: {xbox: true},
	...
}
*/

foo(cli.input[0], cli.flags);

ES Modules

#!/usr/bin/env node
import { createRequire } from "module";
import foo from "./lib/index.js";

const hac_k = createRequire(import.meta.url)("hac_k");

const cli = hac_k(
	`
	Usage
	  ❯ foo <input>

	Options
	  --xbox, -x  Include a xbox

	Examples
	  ❯ foo consoles --xbox
	  🎮 consoles 🎮
`,
	{
		flags: {
			xbox: {
				type: "boolean",
				alias: "x",
			},
		},
	}
);
/*
{
	input: ['consoles'],
	flags: {xbox: true},
	...
}
*/

foo(cli.input[0], cli.flags);

API

hac_k(helpText, options?)

hac_k(options)

Returns an object with:

helpText

Type: string

Shortcut for the help option.

options

Type: object

flags

Type: object

Define argument flags.

The key is the flag name and the value is an object with any of:

Example:

flags: {
	xbox_ninja_cat: {
		type: 'string',
		alias: 'xnc',
		default: ['xbox', 'ninja_cat'],
		isMultiple: true,
		isRequired: (flags, input) => {
			if (flags.otherFlag) {
				return true;
			}

			return false;
		}
	}
}

Caution: Explicitly specifying undefined for booleanDefault has different meaning from omitting key itself.

Example:

const hac_k = require("hac_k");

const cli = hac_k(
	`
	Usage
	  ❯ foo

	Options
	  --xbox, -x  Include xbox
	  --ninja_cat, -nc  Include a ninja_cat

	Examples
	  ❯ foo
	  🎮 consoles 🎮
`,
	{
		booleanDefault: undefined,
		flags: {
			xbox: {
				type: "boolean",
				default: true,
				alias: "x",
			},
			ninja_cat: {
				type: "boolean",
				default: false,
				alias: "nc",
			},
			football: {
				type: "boolean",
				alias: "f",
			},
		},
	}
);
/*
{
	flags: {
		xbox: true,
		ninja_cat: true,
		footbal: true
	},
	unnormalizedFlags: {
		xbox: true,
		x: true,
		ninja_cat: true,
		nc: true,
		f: true
	},
	…
}
*/

Promises

hac_k will make unhandled rejected promises fail hard instead of the default silent fail. Meaning you don’t have to manually .catch() promises used in your CLI.