@tslib/argv
TypeScript icon, indicating that this package has built-in type declarations

1.0.26 • Public • Published

@tslib/argv

中文说明

@tslib/argv

minimalist TypeScript command line parsing library

  • Complete code hinting and type resolution
  • Supports standard command line syntax: program run -p 9000 -h 127.0.0.1
  • Supports long and short parameters, such as: "-p" or "--port"
  • The command and all parameters support default values
  • Automatically generate help information
  • Clean and very simple code with no more than 200 lines of comments
  • Independent code, no third-party dependent libraries?
  • Convenient reference
  • Example ts project in /example directory

Install

  yarn add @tslib/argv

using

  • see directory: '/src/test'

Instructions

1. Introducing argv library

  1. Add the command line definition file: "cmdline.ts"
import argv from '@tslib/argv';
// Automatically import version information, it can also be manually defined
import pkg = require('../package.json');

/**
 * Export the parsed command line
 */
export default argv(
  pkg.name,
  pkg.version,
  ` Example using for Test ts argv library
  You can using: "${pkg.name} [cmd] params ..." to test it
  `,
  {
    cmd: {
      short: null, // ​​Define short is null, set cmd to command parameter parsing mode
      desc: 'default cmd is "run",other:"test","build"',
      value: 'run', // default value
    },
    port: {
      short: 'p',
      desc: 'service listen port,default:3000',
      value: 3000, // default value
    },
    boo: {
      short: 'b',
      desc: 'service listen port,default:3000',
      value: true, // default value
    }
  }
);

2. Create a .json import file definition [optional].

jsonImport.ts added to the project source file directory, without using import import

declare module "*.json" {
  const value: any;
  export = value;
}

3. Using Command Line Parameters

import cmdline from './cmdline';

console.log('cmd', cmdline.cmd);
console.log('port', cmdline.port);
console.log('boo', cmdline.boo);

Other notes

  • import cmdline is a read-only object that prevents accidental changes to variables
  • boolean type parameter receives 'null', '0', 'false', as false, others are true

@tslib

TypeScript full stack development library, dedicated to all types of front and back end component development

Readme

Keywords

none

Package Sidebar

Install

npm i @tslib/argv

Weekly Downloads

2

Version

1.0.26

License

MIT

Unpacked Size

41.7 kB

Total Files

22

Last publish

Collaborators

  • magicmind2013
  • smallpotato
  • zhfjyq