0.1.27 • Published 11 months ago

@fnet/args v0.1.27

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
11 months ago

@fnet/args

Introduction

@fnet/args is a command-line interface (CLI) utility designed to assist in parsing and validating command-line arguments. It leverages the capabilities of JSON Schema for validation, providing a robust method to ensure the correctness of user inputs. This tool is ideal for developers who need a straightforward way to manage command-line arguments and ensure they adhere to expected schemas.

How It Works

@fnet/args operates by receiving an array of command-line arguments, a JSON Schema for validation, and optional configurations like initial arguments or a package callback. The utility parses these arguments using the minimist library and then validates them against the provided schema, ensuring they are correctly formatted and contain the required data types and structures.

Key Features

  • Argument Parsing: Uses the minimist library to parse command-line arguments, handling various input formats including flags and key-value pairs.
  • JSON Schema Validation: Validates the parsed arguments against a JSON Schema, leveraging the AJV library for dynamic schema compilation and validation.
  • Dot Notation Support: Allows the use of dot notation for nested parameter specification, making it easier to define complex configuration structures.
  • Schema Display in YAML: Converts and displays the JSON Schema in a readable YAML format with highlighted keys and values for easy understanding of expected arguments.
  • Extensibility: Offers options to provide initial arguments and callbacks for package information, supporting flexible integration into various CLI tools.

Conclusion

In essence, @fnet/args provides a reliable and efficient solution for parsing and validating command-line inputs, ensuring they meet expected schema requirements. It automates error checking in CLI applications, reducing potential bugs or unexpected behavior from user inputs.

Developer Guide for @fnet/args

Overview

The @fnet/args library is a command-line utility designed to simplify the process of parsing and validating command-line arguments against a JSON Schema. Its core functionality includes:

  • Parsing command-line arguments using dot notation.
  • Validating arguments according to a provided JSON Schema.
  • Providing helpful error messages for invalid arguments.
  • Displaying usage information in YAML format upon request.

This tool is particularly useful for Node.js developers who need to handle complex command-line inputs in a structured and validated manner.

Installation

To add @fnet/args to your project, you can use either npm or yarn:

npm install @fnet/args

or

yarn add @fnet/args

Usage

Here's a basic setup to get you started with using @fnet/args in your Node.js project:

import parseArgs from '@fnet/args';

const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "number" }
  },
  required: ["name", "age"]
};

async function main() {
  const args = await parseArgs({ schema });

  if (args.errors) {
    console.error("Argument validation failed:", args.errors);
  } else {
    console.log("Parsed arguments:", args);
  }
}

main();

Examples

Here are a few practical examples demonstrating the core functionality of @fnet/args.

Example 1: Basic Argument Parsing

This example shows how to parse a simple set of command-line arguments typical in Node.js applications.

const schema = {
  type: "object",
  properties: {
    input: { type: "string" },
    output: { type: "string" }
  },
  required: ["input", "output"]
};

parseArgs({ schema })
  .then(args => {
    console.log("Input File:", args.input);
    console.log("Output File:", args.output);
  });

Example 2: Displaying Help Information

Enable users to access help information when the --help flag is used.

const schema = {
  type: "object",
  properties: {
    help: { type: "boolean" },
  },
  required: []
};

parseArgs({ schema })
  .then(args => {
    if (args.help) {
      console.log("Usage: node your-script.js --input <file> --output <file>");
    }
  });

Example 3: Initial Argument Values and Merging

This shows how to merge command-line arguments with initial values.

const initialArgs = {
  input: "default_input.txt"
};

const schema = {
  type: "object",
  properties: {
    input: { type: "string" },
    output: { type: "string" }
  },
  required: ["output"]
};

parseArgs({ schema, initial: initialArgs })
  .then(args => {
    console.log("Merged Input File:", args.input); // Uses provided or default input
    console.log("Output File:", args.output);
  });

Acknowledgement

We acknowledge the usage of libraries like minimist for argument parsing and ajv for JSON Schema validation, which help in enhancing the capabilities of @fnet/args.

Input Schema

$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
  schema:
    type: object
    description: A valid JSON Schema for validating the arguments.
  argv:
    type: array
    items:
      type: string
    description: An array of command-line arguments to be parsed and validated.
      Defaults to process.argv.
  validate:
    type: object
    description: A precompiled validation function. If not provided, AJV will be
      dynamically imported to compile the schema.
  initial:
    type: object
    description: Initial arguments to merge with parsed arguments.
  exitOnError:
    type: boolean
    description: Whether to exit the process on validation failure. Defaults to true.
  packageCallback:
    type: object
    description: A callback function to retrieve the package information.
required:
  - schema
0.1.27

11 months ago

0.1.26

11 months ago

0.1.21

1 year ago

0.1.22

1 year ago

0.1.23

1 year ago

0.1.24

1 year ago

0.1.25

1 year ago

0.1.20

1 year ago

0.1.12

1 year ago

0.1.13

1 year ago

0.1.14

1 year ago

0.1.15

1 year ago

0.1.16

1 year ago

0.1.17

1 year ago

0.1.18

1 year ago

0.1.19

1 year ago

0.1.11

1 year ago

0.1.10

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago