Giter VIP home page Giter VIP logo

p-args's Introduction

p-args

Zero dependencies TypeScript wrapper around Node.js util.parseArgs() API

Features

Note
This project is currently in development.

  • Zero dependencies
  • Simple to use API
  • First-class TypeScript support
  • Automatic help generation for commands

Requirements

Minimum requirements:

  • Node.js >=18.12.0
  • TypeScript >=5.3

Installation

npm install -D p-args

Usage

Simple Parsing

import { build } from 'p-args'

const program = build({
  name: 'my-cli',
  description: 'A command-line tool.',
  version: '0.1.0',
})

const parsed = await program.parse()

console.log(JSON.stringify(parsed, null, 2))
Show output
❯ npx tsx examples/simple-parsing.ts baz wow --type=bar
{
  "command": "baz",
  "args": [
    "wow"
  ],
  "options": {
    "type": "bar"
  }
}

Simple Command with Options

import { build } from 'p-args'

const program = build({
  name: 'web-server-cli',
  description: 'launch web server',
  version: '0.1.0',
  options: {
    ip: {
      type: 'string',
      short: 'i',
      default: '127.0.0.1',
      description: 'web ip',
    },
    port: {
      type: 'string',
      short: 'p',
      default: '3000',
      description: 'web port',
    },
  },
  handler: (_args, opts) => {
    console.log(`server listening: ${opts.ip}:${opts.port}`)
  },
})

await program.parse()
Show output
❯ npx tsx examples/simple-options.ts --ip=192.168.178.70 --port=4000

# todo

License

This project is under MIT license.

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.