Giter Site home page Giter Site logo

genisysram / argv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from codenothing/argv

0.0 0.0 0.0 208 KB

Node based command line argument parser

Home Page: http://codenothing.github.com/argv/

License: MIT License

Shell 2.98% JavaScript 97.02%

argv's Introduction

argv

argv is a nodejs module that does command line argument parsing.

NPM version Build Status Code Climate

Installation

$ npm install argv

Usage

var argv = require( 'argv' );
var args = argv.option( options ).run();
-> { targets: [], options: {} }

Run

Runs the argument parser on the global arguments. Custom arguments array can be used by passing into this method

// Parses default arguments 'process.argv.slice( 2 )'
argv.run();

// Parses array instead
argv.run([ '--option=123', '-o', '123' ]);

Options

argv is a strict argument parser, which means all options must be defined before parsing starts.

argv.option({
	name: 'option',
	short: 'o',
	type: 'string',
	description: 'Defines an option for your script',
	example: "'script --opiton=value' or 'script -o value'"
});

Modules

Modules are nested commands for more complicated scripts. Each module has it's own set of options that have to be defined independently of the root options.

argv.mod({
	mod: 'module',
	description: 'Description of what the module is used for',
	options: [ list of options ]
});

Types

Types convert option values to useful js objects. They are defined along with each option.

  • string: Ensure values are strings
  • path: Converts value into a fully resolved path.
  • integer | int: Converts value into an integer
  • float: Converts value into a float number
  • boolean | bool: Converts value into a boolean object. 'true' and '1' are converted to true, everything else is false.
  • csv: Converts value into an array by splitting on comma's.
  • list: Allows for option to be defined multiple times, and each value added to an array
  • [list|csv],[type]: Combo type that allows you to create a list or csv and convert each individual value into a type.
argv.option([
	{
		name: 'option',
		type: 'csv,int'
	},
	{
		name: 'path',
		short: 'p',
		type: 'list,path'
	}
]);

// csv and int combo
$ script --option=123,456.001,789.01
-> option: [ 123, 456, 789 ]

// list and path combo
$ script -p /path/to/file1 -p /path/to/file2
-> option: [ '/path/to/file1', '/path/to/file2' ]

You can also create your own custom type for special conversions.

argv.type( 'squared', function( value ) {
	value = parseFloat( value );
	return value * value;
});

argv.option({
	name: 'square',
	short: 's',
	type: 'squared'
});

$ script -s 2
-> 4

Version

Defining the scripts version number will add the version option and print it out when asked.

argv.version( 'v1.0' );

$ script --version
v1.0

Info

Custom information can be displayed at the top of the help printout using this method

argv.info( 'Special script info' );

$ script --help

Special script info

... Rest of Help Doc ...

Clear

If you have competing scripts accessing the argv object, you can clear out any previous options that may have been set.

argv.clear().option( [new options] );

Help

argv injects a default help option initially and on clears. The help() method triggers the help printout.

argv.help();

License

The MIT License

Copyright (c) 2012-2013 Corey Hart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

argv's People

Contributors

codenothing 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.