/
Holixus
/
node-getopt
Обзор
Документация
Войти
/
Holixus
/
node-getopt
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
test/test.toffee
184 строки
3 KB
Miao Jiang
Add test case.
28 янв 2013, 16:34
28 янв 2013, 16:34
de482d5
Код
Авторство
О чём код?
try Getopt = require './..' assert = require 'assert' eq = assert.deepEqual throws = assert.throws # has argument getopt = new Getopt([ ['a', 'has-argument='], ]) eq( getopt.parse(['-a', 'a-value']) { argv: [], options: { 'has-argument': 'a-value', } }, 'has-argument' ) eq( getopt.parse(['--has-argument', 'a-value']) { argv: [], options: { 'has-argument': 'a-value', } }, 'has-argument' ) # short option spaced value parsing bug # https://github.com/jiangmiao/node-getopt/issues/1 getopt = new Getopt([ ['a', 'has-argument='], ['b', 'no-argument'] ]) eq( getopt.parse(['-aone two three']) { argv: [], options: { 'has-argument': 'one two three', } }, 'has-argument' ) eq( getopt.parse(['-baone two three']) { argv: [], options: { 'has-argument': 'one two three', 'no-argument': true } }, 'has-argument' ) eq( getopt.parse(['-ba<a >']) { argv: [], options: { 'has-argument': '<a >', 'no-argument': true } }, 'has-argument' ) # no argument getopt = new Getopt([ ['A', 'A'] ['B', 'B'] ['C', 'C'] ]) eq( getopt.parse(['-ABC']) { argv: [], options: { 'A': true, 'B': true, 'C': true } }, 'no-argument' ) # combined has argument and no argument getopt = new Getopt([ ['a', 'a='] ['A', 'A'] ['B', 'B'] ['C', 'C'] ]) eq( getopt.parse(['-ABCa', 'foo']) { argv: [], options: { 'A': true, 'B': true, 'C': true, 'a': 'foo' } }, 'no-argument' ) # invalid option check getopt = new Getopt([ ]).error((e) -> throw e ) throws( -> getopt.parse(['-A']) (err) -> err.message == 'invalid option A' ) # long option getopt = new Getopt([ ['h', 'help'] ]) eq( getopt.parse(['--help']), { argv: [], options: { 'help': true } }, 'long option' ) # keep data after -- getopt = new Getopt([ ]) eq( getopt.parse('-- hello world'.split(' ')), { argv: ['hello', 'world'], options: {} } ) # mixed getopt = new Getopt([ ['h', 'help'], ['m', 'multi=+'], ['s', 'short'] ]) eq( getopt.parse('foo --help --multi a -m b -sm c -- --help'.split(' ')), { argv: ['foo', '--help'], options: { 'help': true, 'multi': ['a', 'b', 'c'], 'short': true } } ) console.info "\x1b[32mTest passed.\x1b[0m" catch e console.info e console.info '\n--- STACK TRACE ---\n' console.info e.stack # vim: sw=2 ts=2 sts=2 expandtab :