capitano
Version:
Powerful, non opitionated command line parser for serious applications
85 lines (63 loc) • 1.95 kB
text/coffeescript
_ = require('lodash')
parse = require('./parse')
REGEX_REQUIRED = /^<(.*)>$/
REGEX_OPTIONAL = /^\[(.*)\]$/
REGEX_VARIADIC = /^[<\[](.*)[\.]{3}[>\]]$/
REGEX_MULTIWORD = /\s/
REGEX_STDIN = /^[<\[]\|(.*)[\]>]$/
STDIN_CHARACTER = '|'
module.exports = class Parameter
constructor: (parameter) ->
if _.isNumber(parameter)
parameter = String(parameter)
if not parameter? or not _.isString(parameter)
throw new Error("Missing or invalid parameter: #{parameter}")
= parameter
if and
throw new Error('Parameter can\'t be variadic and allow stdin')
_testRegex: (regex) ->
return regex.test()
isRequired: ->
return
isOptional: ->
return
isVariadic: ->
return
isWord: ->
return not _.some [
]
isMultiWord: ->
return
allowsStdin: ->
return [1] is STDIN_CHARACTER
getValue: ->
return if
regex = REGEX_REQUIRED if
regex = REGEX_OPTIONAL if
regex = REGEX_VARIADIC if
regex = REGEX_STDIN if
result = .match(regex)
return result[1]
getType: ->
return 'word' if
return 'parameter'
matches: (parameter) ->
return parameter is if
parameterWordsLength = parse.split(parameter).length
if
return true if
return false if parameterWordsLength < 1
else
if
return false if parameterWordsLength < 1
return true
toString: ->
# Preserve quotes when joining the command.
# If a command word used to have quotes (e.g: had whitespace),
# we explicitly quote it back.
# https://github.com/resin-io/capitano/issues/4
if and
return '"' + + '"'
return