1 | _ = require('lodash')
|
2 | Command = require('./command')
|
3 | Option = require('./option')
|
4 | Signature = require('./signature')
|
5 |
|
6 | exports.parse = require('./parse').parse
|
7 | exports.state = require('./state')
|
8 | exports.defaults = require('./settings')
|
9 | exports.utils = require('./utils')
|
10 |
|
11 | exports.command = (options) ->
|
12 | options.options = _.map options.options, (option) ->
|
13 |
|
14 |
|
15 |
|
16 |
|
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 |
|
26 | exports.globalOption = (options) ->
|
27 | options.signature = new Signature(options.signature)
|
28 | option = new Option(options)
|
29 | exports.state.globalOptions.push(option)
|
30 |
|
31 | exports.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 |
|
46 | exports.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 |
|
59 | exports.run = (argv, callback) ->
|
60 | parsedArgs = exports.parse(argv)
|
61 | exports.execute(parsedArgs, callback)
|