Git Product home page Git Product logo

deez-argv's Introduction

deez-argv

A left-pad inspired package for deserializing an argument list to a Record

Install via the plethora of available package managers!!!!

$ npm i deez-argv
$ yarn add deez-argv
$ pnpm add deez-argv
$ bun i deez-argv

By default process.argv is passed if no argument is given.

const args = deserializeArgumentList();
// ^? Record<string, string>

You can also pass a custom argument list with a custom starting point

const args = deserializeArgumentList(['--foo', 'bar', '-baz=qar'], 0);
// ^? Record<string, string>

The starting point otherwise defaults to 2 for compatibility with process.argv.

You can then pass that object to whatever validation you desire.

const argSchema = z.object({foo: z.string(), baz: z.string()});
const args = argSchema.parse(deserializeArgumentList());
// ^? {foo:string, baz:string}

- and -- denotes an argument that should be parsed. If an argument is followed by another argument, it is assumed to be a boolean flag. Otherwise values can be passed either with a = or with a space. You can use commas to pass multiple values into an array.

$ node index.js --foo bar -biz --baz=qar --qux --shows interstellar,bum, pirates
const args = deserializeArgumentList();
// ^? Record<string, string>
assert.deepStrictEqual(args, {
  foo: 'bar',
  biz: true,
  baz: 'qar',
  qux: true,
  shows: ['interstellar', 'bum', 'pirates'],
});

Obviously this library could handle validation and mapping for you, kind of like the Rust Clap-crate, but for now it's just a simple deserializer.

deez-argv's People

Contributors

remirth avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.