mirror of
https://github.com/flynx/argv.js.git
synced 2025-12-20 18:41:39 +00:00
cleanup...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
28711d7462
commit
c59f57ecd7
12
README.md
12
README.md
@ -189,6 +189,9 @@ var parser = argv.Parser({
|
|||||||
arg: '| required_option_given',
|
arg: '| required_option_given',
|
||||||
|
|
||||||
required: true,
|
required: true,
|
||||||
|
|
||||||
|
// keep this near the top of the options list in -help...
|
||||||
|
priority: 80,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
@ -422,6 +425,12 @@ Ordering is as follows:
|
|||||||
|
|
||||||
Note that options and commands are grouped separately.
|
Note that options and commands are grouped separately.
|
||||||
|
|
||||||
|
The built-in options `-help`, `-version` and `-quiet` have a priority
|
||||||
|
of `99` so that they appear the the top of the `-help` list.
|
||||||
|
|
||||||
|
Any option defining `.required` and not defining an explicit `.priority`
|
||||||
|
will be sorted via `<parser>.requiredOptionPriority` (`80` by default).
|
||||||
|
|
||||||
|
|
||||||
#### `<option>.arg`
|
#### `<option>.arg`
|
||||||
|
|
||||||
@ -558,6 +567,9 @@ of weather the option was given by the user or not.
|
|||||||
Sets weather the _parser_ should complain/err if option/command is
|
Sets weather the _parser_ should complain/err if option/command is
|
||||||
not given.
|
not given.
|
||||||
|
|
||||||
|
Note that this also _implicitly_ prioritizes the option, for more info see:
|
||||||
|
[`<option>.priority`](#optionpriority).
|
||||||
|
|
||||||
|
|
||||||
#### `<option>.valueRequired`
|
#### `<option>.valueRequired`
|
||||||
|
|
||||||
|
|||||||
43
argv.js
43
argv.js
@ -25,6 +25,13 @@ var object = require('ig-object')
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
var OPTION_PREFIX = '-'
|
||||||
|
var COMMAND_PREFIX = '@'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
module.STOP =
|
module.STOP =
|
||||||
@ -146,6 +153,8 @@ function(name, pre, post){
|
|||||||
//
|
//
|
||||||
// default: 123,
|
// default: 123,
|
||||||
//
|
//
|
||||||
|
// priority: 50,
|
||||||
|
//
|
||||||
// required: false,
|
// required: false,
|
||||||
//
|
//
|
||||||
// valueRequired: false,
|
// valueRequired: false,
|
||||||
@ -261,8 +270,6 @@ object.Constructor('Parser', {
|
|||||||
}, {
|
}, {
|
||||||
// config...
|
// config...
|
||||||
//
|
//
|
||||||
optionPrefix: '-',
|
|
||||||
commandPrefix: '@',
|
|
||||||
// NOTE: this must contain two goups the first is the prefix and the
|
// NOTE: this must contain two goups the first is the prefix and the
|
||||||
// second must contain the option name...
|
// second must contain the option name...
|
||||||
// NOTE: we only care about differentiating an option from a command
|
// NOTE: we only care about differentiating an option from a command
|
||||||
@ -272,6 +279,9 @@ object.Constructor('Parser', {
|
|||||||
|
|
||||||
splitOptions: true,
|
splitOptions: true,
|
||||||
|
|
||||||
|
requiredOptionPriority: 80,
|
||||||
|
|
||||||
|
|
||||||
// instance stuff...
|
// instance stuff...
|
||||||
// XXX do we need all three???
|
// XXX do we need all three???
|
||||||
script: null,
|
script: null,
|
||||||
@ -294,8 +304,11 @@ object.Constructor('Parser', {
|
|||||||
//
|
//
|
||||||
options: function(...prefix){
|
options: function(...prefix){
|
||||||
var that = this
|
var that = this
|
||||||
|
var req_prio = this.requiredOptionPriority != null ?
|
||||||
|
this.requiredOptionPriority
|
||||||
|
: 80
|
||||||
prefix = prefix.length == 0 ?
|
prefix = prefix.length == 0 ?
|
||||||
[this.optionPrefix]
|
[OPTION_PREFIX]
|
||||||
: prefix
|
: prefix
|
||||||
return prefix
|
return prefix
|
||||||
.map(function(prefix){
|
.map(function(prefix){
|
||||||
@ -323,8 +336,12 @@ object.Constructor('Parser', {
|
|||||||
.flat(1)
|
.flat(1)
|
||||||
.map(function(e, i){ return [e, i] })
|
.map(function(e, i){ return [e, i] })
|
||||||
.sort(function([a, ai], [b, bi]){
|
.sort(function([a, ai], [b, bi]){
|
||||||
a = a[3].priority
|
a = a[3].priority !== undefined ?
|
||||||
b = b[3].priority
|
a[3].priority
|
||||||
|
: (a[3].required && req_prio)
|
||||||
|
b = b[3].priority !== undefined ?
|
||||||
|
b[3].priority
|
||||||
|
: (b[3].required && req_prio)
|
||||||
return a != null && b != null ?
|
return a != null && b != null ?
|
||||||
b - a
|
b - a
|
||||||
// positive priority above order, negative below...
|
// positive priority above order, negative below...
|
||||||
@ -344,7 +361,7 @@ object.Constructor('Parser', {
|
|||||||
.filter(function([k, a, d, handler]){
|
.filter(function([k, a, d, handler]){
|
||||||
return handler.required }) },
|
return handler.required }) },
|
||||||
commands: function(){
|
commands: function(){
|
||||||
return this.options(this.commandPrefix) },
|
return this.options(COMMAND_PREFIX) },
|
||||||
|
|
||||||
// Get handler...
|
// Get handler...
|
||||||
//
|
//
|
||||||
@ -359,9 +376,9 @@ object.Constructor('Parser', {
|
|||||||
key = key.split(/=/).shift()
|
key = key.split(/=/).shift()
|
||||||
// normalize option/command name...
|
// normalize option/command name...
|
||||||
key = this.optionInputPattern.test(key) ?
|
key = this.optionInputPattern.test(key) ?
|
||||||
key.replace(this.optionInputPattern, this.optionPrefix+'$2')
|
key.replace(this.optionInputPattern, OPTION_PREFIX+'$2')
|
||||||
: !key.startsWith(this.commandPrefix) ?
|
: !key.startsWith(COMMAND_PREFIX) ?
|
||||||
key.replace(this.commandInputPattern, this.commandPrefix+'$1')
|
key.replace(this.commandInputPattern, COMMAND_PREFIX+'$1')
|
||||||
: key
|
: key
|
||||||
var seen = new Set()
|
var seen = new Set()
|
||||||
while(key in this
|
while(key in this
|
||||||
@ -424,7 +441,7 @@ object.Constructor('Parser', {
|
|||||||
// common tests...
|
// common tests...
|
||||||
isCommand: function(str){
|
isCommand: function(str){
|
||||||
return this.commandInputPattern.test(str)
|
return this.commandInputPattern.test(str)
|
||||||
&& ((this.commandPrefix + str) in this
|
&& ((COMMAND_PREFIX + str) in this
|
||||||
|| this['@*']) },
|
|| this['@*']) },
|
||||||
hasArgument: function(handler){
|
hasArgument: function(handler){
|
||||||
handler = typeof(handler) == typeof('str') ?
|
handler = typeof(handler) == typeof('str') ?
|
||||||
@ -932,7 +949,7 @@ object.Constructor('Parser', {
|
|||||||
var [arg, value] = arg.split(/=/)
|
var [arg, value] = arg.split(/=/)
|
||||||
// skip single letter unknown or '--' options...
|
// skip single letter unknown or '--' options...
|
||||||
if(arg.length <= 2
|
if(arg.length <= 2
|
||||||
|| arg.startsWith(parsed.optionPrefix.repeat(2))){
|
|| arg.startsWith(OPTION_PREFIX.repeat(2))){
|
||||||
return [arg, undefined] }
|
return [arg, undefined] }
|
||||||
// split and normalize...
|
// split and normalize...
|
||||||
var [a, ...r] =
|
var [a, ...r] =
|
||||||
@ -959,9 +976,9 @@ object.Constructor('Parser', {
|
|||||||
// because options if unidentified need to be split into
|
// because options if unidentified need to be split into
|
||||||
// single letter options and commands to not...
|
// single letter options and commands to not...
|
||||||
var [type, dfl] = opt_pattern.test(arg) ?
|
var [type, dfl] = opt_pattern.test(arg) ?
|
||||||
['opt', parsed.optionPrefix +'*']
|
['opt', OPTION_PREFIX +'*']
|
||||||
: parsed.isCommand(arg) ?
|
: parsed.isCommand(arg) ?
|
||||||
['cmd', parsed.commandPrefix +'*']
|
['cmd', COMMAND_PREFIX +'*']
|
||||||
: ['unhandled']
|
: ['unhandled']
|
||||||
// options / commands...
|
// options / commands...
|
||||||
if(type != 'unhandled'){
|
if(type != 'unhandled'){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user