Giter Club home page Giter Club logo

argly's People

Contributors

austinkelleher avatar patrick-steele-idem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

argly's Issues

Automatically handle `--help`/`-h` arguments

I really like the general direction of this library, but it sure would be nice if I didn’t need to write a custom validate() function in order to print usage in response to --help. The readme suggests this is far easier than it really is—this example doesn’t actually print any help messages:

var result = require('argly')
    .createParser({
        '--help':   { type: 'string', description: 'Show this help message' },
        '--foo -f': { type: 'string', description: 'Some helpful description for "foo"' },
        '--bar -b': { type: 'string', description: 'Some helpful description for "bar"' }
    })
    .parse();

To be honest, the main reason I use a more complicated arg parser than minimist is to get nice help messages and, while printUsage() is helpful here, it seems like the sort of thing I should rarely need to invoke explicitly.

Maybe something like this, that automatically prints usage for -h or --help:

var result = require('argly')
    .createParser({
        '--foo -f': { type: 'string', description: 'Some helpful description for "foo"' },
        '--bar -b': { type: 'string', description: 'Some helpful description for "bar"' }
    })
    .addHelp()
    .parse();

// Above is the same as:
var result = require('argly')
    .createParser({
        '--help':   { type: 'string', description: 'Show this help message' },
        '--foo -f': { type: 'string', description: 'Some helpful description for "foo"' },
        '--bar -b': { type: 'string', description: 'Some helpful description for "bar"' }
    })
    // NOTE: calling `addHelp()` shouldn't preclude someone from also adding
    // their own validation/onError routines that run in addition to these ones
    .validate(function(result) {
        if (result.help) {
            this.printUsage();
            process.exit(0);
        }
    })
    .onError(function(error) {
        this.printUsage();
        console.error(error);
        process.exit(1);
    })
    .parse();

And it could be customized like so:

var result = require('argly')
    .createParser({
        '--foo -f': { type: 'string', description: 'Some helpful description for "foo"' },
        '--bar -b': { type: 'string', description: 'Some helpful description for "bar"' }
    })
    .addHelp('--really-help -?', 'Print this awesome, in-your-face help message')
    .parse();

Support passing a negative integer for an argument that accepts integers

When creating a parser that accepts integers as an argument, allow it to accept negative integer values too. Currently when passing a negative integer value, it parses it as an argument and errors out because none of the options match the negative integer value.

For e.g. set option that accepts negative integer in the parser

var args = require('argly').createParser({
        '--max-len': {
            type: 'int',
            description: 'The maximum line length. Defaults to 80'
        }).parse();

and then if I run the command

node index.js --max-len -1

but this errors out with

Illegal argument: "-1" (allowed:<list of allowed commands>)

Allow `defaultValue` to be a function that returns the value

Example:

var parser = require('../')
            .createParser({
                '--foo -f': {
                    type: 'string',
                    defaultValue: function() {
                        return 'foo' + 'bar';
                    }
                },
                '--bar -b': {
                    type: 'string'
                }
            });

        var parsed = parser.parse('--bar test'.split(/\s/));

        expect(parsed).to.deep.equal({
            foo: 'foo bar',
            bar: 'test'
        });

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.