capitano
Version:
Powerful, non opitionated command line parser for serious applications
62 lines (45 loc) • 1.5 kB
text/coffeescript
_ = require('lodash')
parse = require('./parse')
Signature = require('./signature')
isValidAlias = (alias) ->
return _.isString(alias) or _.isArray(alias)
module.exports = class Option
constructor: (options = {}) ->
if options.signature not instanceof Signature
throw new Error('Missing or invalid option signature')
if options.signature.hasParameters()
throw new Error('Use the parameter option attribute')
if options.alias? and not isValidAlias(options.alias)
throw new Error('Invalid alias')
if options.parameter? and not _.isString(options.parameter)
throw new Error('Invalid parameter')
if options.boolean and options.parameter?
throw new Error('Boolean options can\'t have parameters')
if not options.boolean and not options.parameter?
throw new Error('Missing parameter')
_.defaults options,
boolean: false
alias: []
_.extend(this, options)
getOptionsValue: (options) ->
value = options[]
if not value?
value = _.chain(options)
.pick()
.values()
.first()
.value()
return value
matches: (value) ->
return false if not value?
return not _.some [
and not _.isBoolean(value)
not and _.isBoolean(value)
]
toString: ->
signatures = _.map [ .toString() ].concat(), (signature) ->
return "-#{signature}" if signature.length <= 1
return "--#{signature}"
result = signatures.join(', ')
result += " <#{@parameter}>" if ?
return result