UNPKG

1.74 kBtext/coffeescriptView Raw
1_ = require('lodash')
2Command = require('./command')
3Option = require('./option')
4Signature = require('./signature')
5
6exports.parse = require('./parse').parse
7exports.state = require('./state')
8exports.defaults = require('./settings')
9exports.utils = require('./utils')
10
11exports.command = (options) ->
12 options.options = _.map options.options, (option) ->
13
14 # Necessary to prevent modifying the signature
15 # of the original object, and causing issues
16 # if the same object is used in another command
17 result = _.clone(option)
18
19 result.signature = new Signature(option.signature)
20 return new Option(result)
21
22 options.signature = new Signature(options.signature)
23 command = new Command(options)
24 exports.state.commands.push(command)
25
26exports.globalOption = (options) ->
27 options.signature = new Signature(options.signature)
28 option = new Option(options)
29 exports.state.globalOptions.push(option)
30
31exports.permission = (name, permissionFunction) ->
32 if not name?
33 throw new Error('Missing permission name')
34
35 if not _.isString(name)
36 throw new Error('Invalid permission name')
37
38 if not permissionFunction?
39 throw new Error('Missing permission function')
40
41 if not _.isFunction(permissionFunction)
42 throw new Error('Invalid permission function')
43
44 exports.state.permissions[name] = permissionFunction
45
46exports.execute = (args, callback) ->
47 exports.state.getMatchCommand args.command, (error, command) ->
48 return callback?(error) if error?
49
50 if not command?
51 return exports.defaults.actions.commandNotFound(args.command)
52
53 try
54 command.execute(args, callback)
55 catch error
56 return callback?(error)
57
58# Handy shortcut
59exports.run = (argv, callback) ->
60 parsedArgs = exports.parse(argv)
61 exports.execute(parsedArgs, callback)