1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
|
optionator = require '..'
{strict-equal: equal} = require 'assert'
q = (expected, options, args) ->
{generate-help} = optionator options
help-text = generate-help args
try
equal help-text, expected
catch
console.log '# Result:'
console.log help-text
console.log '# Expected:'
console.log expected
throw e
qo = (expected, option-name, options) ->
{generate-help-for-option} = optionator options
help-text = generate-help-for-option option-name
try
equal help-text, expected
catch
console.log '# Result:'
console.log help-text
console.log '# Expected:'
console.log expected
throw e
suite 'help' ->
help-option =
option: 'help'
type: 'Boolean'
description: 'recieve help - print this info'
count-option =
option: 'count'
type: 'Number'
description: 'count of stuff that is to be counted'
obj-option =
option: 'obj'
type: '{x: Number, y: Boolean, z: Object}'
description: 'an object full of things and stuff'
test 'single basic option' ->
q ' --help recieve help - print this info', options: [help-option]
test 'prepend/append' ->
q '''
cmd
--help recieve help - print this info
version 0.1.0
''', {
prepend: 'cmd'
append: 'version 0.1.0'
options: [help-option]
}
test 'heading' ->
q '''
Options:
--help recieve help - print this info
''', {
options:
* heading: 'Options'
* help-option
}
test 'heading with prepend' ->
q '''
cmd
Options:
--help recieve help - print this info
''', {
prepend: 'cmd'
options:
* heading: 'Options'
* help-option
}
test 'two options' ->
q ' --help recieve help - print this info
\n --count Number count of stuff that is to be counted', {
options: [help-option, count-option]
}
test 'headings' ->
q '''
Options:
--help recieve help - print this info
More Options:
--count Number count of stuff that is to be counted
''', {
options:
* heading: 'Options'
* help-option
* heading: 'More Options'
* count-option
}
test 'greatly differnt lengths' ->
q '''
cmd
--help recieve help - print this info
--count Number count of stuff that is to be counted
--obj {x: Number, y: Boolean, z: Object} an object full of things and stuff
''', {
prepend: 'cmd'
options: [help-option, count-option, obj-option]
}
test 'short main name' ->
q ' -h help me', options: [{
option: 'h'
type: 'Boolean'
description: 'help me'
}]
test 'one alias' ->
q ' -h, -H, --help help me', options: [{
option: 'help'
alias: ['h' 'H']
type: 'Boolean'
description: 'help me'
}]
test 'enum type' ->
q ' --size String shirt size - either: small, medium, or large', options: [{
option: 'size'
type: 'String'
enum: <[ small medium large ]>
description: 'shirt size'
}]
test 'enum type, just two' ->
q ' --size String shirt size - either: small or large', options: [{
option: 'size'
type: 'String'
enum: <[ small large ]>
description: 'shirt size'
}]
test 'default' ->
q ' --count Number count of stuff that is to be counted - default: 2', options: [{
option: 'count'
type: 'Number'
description: 'count of stuff that is to be counted'
default: '2'
}]
test 'default with no description' ->
q ' --count Number default: 2', options: [{
option: 'count'
type: 'Number'
default: '2'
}]
test 'default - boolean with true when no short alias' ->
q ' --no-colour', options: [{
option: 'colour'
type: 'Boolean'
default: 'true'
}]
test 'default - boolean with true when no short alias but long aliases' ->
q ' --no-colour, --no-color', options: [{
option: 'colour'
type: 'Boolean'
alias: 'color'
default: 'true'
}]
test 'default - boolean with true with short alias' ->
q ' -c, --colour default: true', options: [{
option: 'colour'
alias: 'c'
type: 'Boolean'
default: 'true'
}]
test 'many aliases' ->
q ' -h, -H, --halp, --help halp me', options: [{
option: 'halp'
alias: ['help' 'h' 'H']
type: 'Boolean'
description: 'halp me'
}]
test 'aliases prop predefined' ->
q ' -h, -H, --halp, --help halp me', options: [{
option: 'halp'
aliases: ['help' 'h' 'H']
type: 'Boolean'
description: 'halp me'
}]
test 'NUM' ->
q ' -NUM::Int the number', options: [{
option: 'NUM'
type: 'Int'
description: 'the number'
}]
test 'show hidden' ->
opts =
options:
* option: 'hidden'
type: 'Boolean'
description: 'magic'
hidden: true
* option: 'visible'
type: 'Boolean'
description: 'boring'
q ' --visible boring', opts
q ' --hidden magic\n --visible boring', opts, {+show-hidden}
suite 'interpolation' ->
opts =
prepend: 'usage {{x}}'
options: [{heading: 'Options'}]
append: 'version {{version}}'
test 'none' ->
q '''
usage {{x}}
Options:
version {{version}}
''', opts
test 'partial' ->
q '''
usage cmd
Options:
version {{version}}
''', opts, {interpolate: {x: 'cmd'}}
test 'basic' ->
q '''
usage cmd
Options:
version 2
''', opts, {interpolate: {x: 'cmd', version: 2}}
test 'with empty string' ->
q '''
usage
Options:
version
''', opts, {interpolate: {x: '', version: ''}}
test 'more than once, with number' ->
opts =
prepend: 'usage {{$0}}, {{$0}}'
options: [{heading: 'Options'}]
append: '{{$0}} and {{$0}}'
q '''
usage xx, xx
Options:
xx and xx
''', opts, {interpolate: {$0: 'xx'}}
test 'no stdout' ->
q '''
cmd
--obj {x: Number, y: Boolean, z: Object} an object full of things and stuff
''', {
prepend: 'cmd'
options: [obj-option]
stdout: null
}
test 'no description' ->
q '''
cmd
--help
''', {
prepend: 'cmd'
options: [{
option: 'help'
type: 'Boolean'
}]
}
suite 'wrapping' ->
test 'basic with max-width' ->
q '''
cmd
--help recieve help - print this info
''', {
prepend: 'cmd'
options: [help-option]
stdout: {isTTY: true, columns: 250}
}
test 'partial single' ->
q '''
cmd
--obj {x: Number, y: Boolean, z: Object} an object full of
things and stuff
''', {
prepend: 'cmd'
options: [obj-option]
stdout: {isTTY: true, columns: 68}
}
test 'full single' ->
q '''
cmd
--obj {x: Number, y: Boolean, z: Object}
an object full of things and stuff
''', {
prepend: 'cmd'
options: [obj-option]
stdout: {isTTY: true, columns: 50}
}
test 'partial several' ->
q '''
cmd
Options:
--help recieve help - print this info
--count Number count of stuff that is to be counted
--obj {x: Number, y: Boolean, z: Object} an object full of things
and stuff
''', {
prepend: 'cmd'
options:
* heading: 'Options'
* help-option
* count-option
* obj-option
stdout: {isTTY: true, columns: 70}
}
test 'full several' ->
q '''
cmd
Options:
--help recieve help - print this info
--count Number count of stuff that is to be counted
--obj {x: Number, y: Boolean, z: Object}
an object full of things and stuff
''', {
prepend: 'cmd'
options:
* heading: 'Options'
* help-option
* count-option
* obj-option
stdout: {isTTY: true, columns: 55}
}
test 'partial all' ->
q '''
cmd
--help recieve help - print this
info
--count Number count of stuff that is to
be counted
''', {
prepend: 'cmd'
options:
* help-option
* count-option
stdout: {isTTY: true, columns: 46}
}
test 'full all' ->
q '''
cmd
--help
recieve help -
print this info
--count Number
count of stuff
that is to be
counted
''', {
prepend: 'cmd'
options:
* help-option
* count-option
stdout: {isTTY: true, columns: 26}
}
test 'type' ->
q '''
cmd
--obj {x: Number, y:
Boolean, z: Object}
an object full of things
and stuff
''', {
prepend: 'cmd'
options: [obj-option]
stdout: {isTTY: true, columns: 32}
}
suite 'for option' ->
opts =
options:
* option: 'times-num'
type: 'Number'
description: 'times to do something.'
example: '--times-num 23'
* option: 'input'
alias: 'i'
type: 'OBJ::Object'
description: 'the input that you want'
example: '--input "x: 52, y: [1,2,3]"'
default: '{a: 1}'
* option: 'nope'
type: 'Boolean'
description: 'nothing at all'
long-description: 'really nothing at all'
* option: 'nope2'
type: 'Boolean'
test 'times' ->
qo '''
--times-num Number
==================
description: Times to do something.
example: --times-num 23
''', 'times-num', opts
test 'input' ->
qo '''
-i, --input OBJ::Object
=======================
default: {a: 1}
description: The input that you want.
example: --input "x: 52, y: [1,2,3]"
''', 'input', opts
qo '''
-i, --input OBJ::Object
=======================
default: {a: 1}
description: The input that you want.
example: --input "x: 52, y: [1,2,3]"
''', 'i', opts
test 'no example - long description' ->
qo '''
--nope
======
description: really nothing at all
''', 'nope', opts
test 'long description text with max width' ->
opts =
options: [
option: 'long'
type: 'String'
description: 'it goes on and on my friends, some people started singing it not knowing what it was'
]
stdout: {isTTY: true, columns: 50}
qo '''
--long String
=============
description:
It goes on and on my friends, some people
started singing it not knowing what it was.
''', 'long', opts
opts.stdout = null
qo '''
--long String
=============
description: It goes on and on my friends, some people started singing it not knowing what it was.
''', 'long', opts
test 'multiple examples' ->
qo '''
--op
====
description: The thing.
examples:
cmd --op
cmd --no-op
''', 'op', {options: [{
option: 'op'
type: 'Boolean'
description: 'the thing'
example:
'cmd --op'
'cmd --no-op'
}]}
test 'rest positional' ->
opts =
options: [{
option: 'rest'
type: 'Boolean'
description: 'The rest'
rest-positional: true
}]
stdout: {isTTY: false}
qo '''
--rest
======
description: The rest. Everything after this option is considered a positional argument, even if it looks like an option.
''', 'rest', opts
# no description
delete opts.options.0.description
qo '''
--rest
======
description: Everything after this option is considered a positional argument, even if it looks like an option.
''', 'rest', opts
test 'no description or rest positional' ->
qo '--nope2', 'nope2', opts
test 'invalid option' ->
qo "Invalid option '--FAKE' - perhaps you meant '-i'?", 'FAKE', opts
suite 'help style settings' ->
test 'all different' ->
opts =
help-style:
alias-separator: '|'
type-separator: ': '
description-separator: ' > '
initial-indent: 1
secondary-indent: 2
max-pad-factor: 10
prepend: 'cmd'
options:
* option: 'help'
alias: 'h'
type: 'Boolean'
description: 'recieve help - print this info'
* count-option
* obj-option
q '''
cmd
-h|--help > recieve help - print this info
--count: Number > count of stuff that is to be counted
--obj: {x: Number, y: Boolean, z: Object} > an object full of things and stuff
''', opts
|