Skip to content

clinjs/clargs

Repository files navigation

Clargs

Clargs is a simple light parser for building cli with node.js.

Installation

using npm npm i @clinjs/clargs

using yarn yarn add @clinjs/clargs

Setup

use clargs.setup(options) to configure your cli.

api type description required
programName String The name used to run your cli. Yes
usage String Explain how to use your cli. Yes
options Option Define options the user can use. No
commands Command Define the commands the user can use. No
clargs.setup({
  programName: 'commity',
  usage: 'commity <command> <options>',
  options: [
    {
      name: '--push',
      alias: '-p',
      description: 'push changes to current remote branch after commiting',
    },
    {
      name: '--addAll',
      alias: '-a',
      description: 'add all staged changes before commiting',
    },
  ],
  commands: [
    {
      name: 'init',
      description: 'inititialize Commity',
      options: [
        {
          name: '--overwrite',
          alias: '-o',
          description: 'overwrite existing config (if exist)',
        },
      ],
    },
  ],
});

Commands

Api Return type Description
Function commandUsed(command: string) Boolean Allow you to know if a command is used.
const clargs = require('@clinjs/clargs');

clargs.setup({
  // ...
});

if (clargs.commandUsed('init')) {
  console.log('Command "init" used');
};

Help

Clargs includes help command that output cli usage, commands and options.

Options

Api Return type Description
Function hasOption(option: string, alias: string) Boolean Allow you to know if an option is used.
const clargs = require('@clinjs/clargs');

clargs.setup({
  // ...
});

if (clargs.hasOption('foo', 'f')) {
  console.log('--foo option passed');
}

Interfaces

SetupOptions

interface SetupOptions {
  allowUnknown: boolean;
  usage: string;
  options: Options;
  commands: Commands;
}

Option

interface Option {
    name: string;
    alias: string;
    description: string;
}

Command

interface Command {
    name: string;
    description: string;
    options: Option[];
}