@fnet/yargs-options-from-schema

0.1.9 • Public • Published

@fnet/yargs-options-from-schema

This project provides a utility for converting JSON schema definitions into yargs-compatible options objects. It facilitates command-line argument parsing by automatically mapping schema attributes, such as types, descriptions, and default values, to yargs configuration options.

How It Works

The tool takes a JSON schema as input and processes its properties to generate an object suitable for use with yargs' .options() method. It supports various schema attributes, including type conversion and description, and handles nested properties and oneOf constructs to define mutually exclusive options in yargs.

Key Features

  • Converts JSON schema to yargs options format.
  • Supports nested properties and complex schema constructs.
  • Handles type conversion for yargs compatibility.
  • Supports oneOf schemas by establishing conflict relationships.
  • Includes descriptions and default values when present in the schema.
  • Allows for predefined choices and aliases in yargs options.

Conclusion

This utility is particularly useful for developers looking to leverage JSON schemas to streamline the setup of command-line interfaces using yargs. It automates the tedious task of manually defining options by interpreting schema attributes and applying them directly to yargs configuration.

Developer Guide for @fnet/yargs-options-from-schema

Overview

The @fnet/yargs-options-from-schema library converts a JSON schema into an options object compatible with yargs. This allows developers to define command-line argument parsers with comprehensive mappings from JSON schema attributes. The library supports conversion for type definitions, default values, descriptions, choices (enums), and aliases, among other features. It also processes nested properties and oneOf structures to offer flexible and conflict-managed command-line option setups.

Installation

To install the @fnet/yargs-options-from-schema library, use either npm or yarn:

npm install @fnet/yargs-options-from-schema

or

yarn add @fnet/yargs-options-from-schema

Usage

The library's primary functionality is encapsulated in a default exported function that takes a JSON schema and returns a yargs-compatible options object. Here's how to use it in a typical scenario:

import optionsFromSchema from '@fnet/yargs-options-from-schema';

// Sample JSON schema
const schema = {
  properties: {
    username: {
      type: 'string',
      description: 'The user name',
      default: 'guest',
    },
    port: {
      type: 'number',
      description: 'Port number for the server',
      default: 8080,
    },
    verbose: {
      type: 'boolean',
      description: 'Enable verbose logging',
    },
  },
  required: ['username'],
};

// Conversion to yargs options
const yargsOptions = await optionsFromSchema({ schema });

// Use with yargs
import yargs from 'yargs';

yargs
  .options(yargsOptions)
  .help()
  .argv;

Examples

Basic Usage

// JSON schema with basic data types
const basicSchema = {
  properties: {
    name: {
      type: 'string',
    },
    age: {
      type: 'number',
    },
    isAdmin: {
      type: 'boolean',
      default: false,
    }
  }
};

// Convert schema to yargs options
const options = await optionsFromSchema({ schema: basicSchema });

Nested Properties and oneOf Usage

// JSON schema with nested properties and oneOf
const complexSchema = {
  properties: {
    server: {
      type: 'object',
      properties: {
        host: {
          type: 'string',
          default: 'localhost',
        },
        port: {
          type: 'number',
          default: 3000,
        },
      },
    },
    database: {
      oneOf: [
        {
          properties: {
            type: {
              type: 'string',
              enum: ['sql', 'nosql'],
            },
            connection: {
              type: 'string',
            },
          },
          required: ['type'],
        },
      ],
    },
  },
};

// Convert schema to yargs options
const complexOptions = await optionsFromSchema({ schema: complexSchema });

These code examples demonstrate converting JSON schema into yargs-compatible option configurations, including complex setups with nested properties and conditional options.

Acknowledgement

This library utilizes @fnet/yaml for schema parsing from YAML files. The contributors to this library have aimed to streamline command-line configuration using schema-driven approaches.

Input Schema

$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
  schema:
    type: object
    description: The JSON schema object to convert

Readme

Keywords

none

Package Sidebar

Install

npm i @fnet/yargs-options-from-schema

Weekly Downloads

43

Version

0.1.9

License

MIT

Unpacked Size

21.3 kB

Total Files

6

Last publish

Collaborators

  • serdark
  • serdar986
  • gboyraz