patrick-steele-idem / argly Goto Github PK
View Code? Open in Web Editor NEWA flexible command line arguments parser that is easy to configure and offers robust type handling.
A flexible command line arguments parser that is easy to configure and offers robust type handling.
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();
var parser = require('../')
.createParser({
'--foo -f': {
type: 'boolean',
defaultValue: true
}
});
{
'--minify -m': 'boolean',
'--minify -t': 'boolean'
}
{
'--minify -m': 'boolean',
'--test -m': 'boolean'
}
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>)
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'
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.