npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fnet/yargs-options-from-schema

v0.1.9

Published

A package that is used to generate yargs options from a JSON schema.

Downloads

172

Readme

@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