File: help_spec.lua

package info (click to toggle)
lua-argparse 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 356 kB
  • sloc: python: 38; makefile: 15
file content (687 lines) | stat: -rw-r--r-- 20,540 bytes parent folder | download
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
local Parser = require "argparse"
getmetatable(Parser()).error = function(_, msg) error(msg) end

describe("tests related to help message generation", function()
   it("creates correct help message for empty parser", function()
      local parser = Parser "foo"
      assert.equal([[
Usage: foo [-h]

Options:
   -h, --help            Show this help message and exit.]], parser:get_help())
   end)

   it("uses custom help option ", function()
      local parser = Parser "foo"
         :add_help "/?"
      assert.equal([[
Usage: foo [/?]

Options:
   /?                    Show this help message and exit.]], parser:get_help())
   end)

   it("uses description and epilog", function()
      local parser = Parser("foo", "A description.", "An epilog.")

      assert.equal([[
Usage: foo [-h]

A description.

Options:
   -h, --help            Show this help message and exit.

An epilog.]], parser:get_help())
   end)

   it("creates correct help message for arguments", function()
      local parser = Parser "foo"
      parser:argument "first"
      parser:argument "second-and-third"
         :args "2"
      parser:argument "maybe-fourth"
         :args "?"
      parser:argument("others", "Optional.")
         :args "*"

      assert.equal([[
Usage: foo [-h] <first> <second-and-third> <second-and-third>
       [<maybe-fourth>] [<others>] ...

Arguments:
   first
   second-and-third
   maybe-fourth
   others                Optional.

Options:
   -h, --help            Show this help message and exit.]], parser:get_help())
   end)

   it("creates correct help message for options", function()
      local parser = Parser "foo"
      parser:flag "-q" "--quiet"
      parser:option "--from"
         :count "1"
         :target "server"
      parser:option "--config"

      assert.equal([[
Usage: foo [-h] [-q] --from <from> [--config <config>]

Options:
   -h, --help            Show this help message and exit.
   -q, --quiet
   --from <from>
   --config <config>]], parser:get_help())
   end)

   it("creates correct help message for arguments with choices", function()
      local parser = Parser "foo"
      parser:argument "move"
         :choices {"rock", "paper", "scissors"}

      assert.equal([[
Usage: foo [-h] {rock,paper,scissors}

Arguments:
   {rock,paper,scissors}

Options:
   -h, --help            Show this help message and exit.]], parser:get_help())
   end)

   it("creates correct help message for options with argument choices", function()
      local parser = Parser "foo"
      parser:option "--format"
         :choices {"short", "medium", "full"}

      assert.equal([[
Usage: foo [-h] [--format {short,medium,full}]

Options:
   -h, --help            Show this help message and exit.
   --format {short,medium,full}]], parser:get_help())
   end)

   it("adds margin for multiline descriptions", function()
      local parser = Parser "foo"
      parser:flag "-v"
         :count "0-2"
         :target "verbosity"
         :description [[
Sets verbosity level.
-v: Report all warnings.
-vv: Report all debugging information.]]

      assert.equal([[
Usage: foo [-h] [-v]

Options:
   -h, --help            Show this help message and exit.
   -v                    Sets verbosity level.
                         -v: Report all warnings.
                         -vv: Report all debugging information.]], parser:get_help())
   end)

   it("puts different aliases on different lines if there are arguments", function()
      local parser = Parser "foo"

      parser:option "-o --output"

      assert.equal([[
Usage: foo [-h] [-o <output>]

Options:
   -h, --help            Show this help message and exit.
         -o <output>,
   --output <output>]], parser:get_help())
   end)

   it("handles description with more lines than usage", function()
      local parser = Parser "foo"

      parser:option "-o --output"
         :description [[
Sets output file.
If missing, 'a.out' is used by default.
If '-' is passed, output to stdount.
]]

      assert.equal([[
Usage: foo [-h] [-o <output>]

Options:
   -h, --help            Show this help message and exit.
         -o <output>,    Sets output file.
   --output <output>     If missing, 'a.out' is used by default.
                         If '-' is passed, output to stdount.]], parser:get_help())
   end)

   it("handles description with less lines than usage", function()
      local parser = Parser "foo"

      parser:option "-o --output"
         :description "Sets output file."

      assert.equal([[
Usage: foo [-h] [-o <output>]

Options:
   -h, --help            Show this help message and exit.
         -o <output>,    Sets output file.
   --output <output>]], parser:get_help())
   end)

   it("handles very long argument lists", function()
      local parser = Parser "foo"

      parser:option "-t --at-least-three"
         :args("3+")
         :argname {"<foo>", "<bar>", "<baz>"}
         :description "Sometimes argument lists are really long."

      assert.equal([[
Usage: foo [-h] [-t <foo> <bar> <baz> ...]

Options:
   -h, --help            Show this help message and exit.
                 -t <foo> <bar> <baz> ...,
   --at-least-three <foo> <bar> <baz> ...
                         Sometimes argument lists are really long.]], parser:get_help())
   end)

   it("shows default values", function()
      local parser = Parser "foo"
      parser:option "-o"
         :default "a.out"
      parser:option "-p"
         :default "8080"
         :description "Port."

      assert.equal([[
Usage: foo [-h] [-o <o>] [-p <p>]

Options:
   -h, --help            Show this help message and exit.
   -o <o>                default: a.out
   -p <p>                Port. (default: 8080)]], parser:get_help())
   end)

   it("does not show default value when show_default == false", function()
      local parser = Parser "foo"
      parser:option "-o"
         :default "a.out"
         :show_default(false)
      parser:option "-p"
         :default "8080"
         :show_default(false)
         :description "Port."

      assert.equal([[
Usage: foo [-h] [-o <o>] [-p <p>]

Options:
   -h, --help            Show this help message and exit.
   -o <o>
   -p <p>                Port.]], parser:get_help())
   end)

   it("creates correct help message for commands", function()
      local parser = Parser "foo"
      parser:flag "-q --quiet"
      local run = parser:command "run"
         :description "Run! "
      run:option "--where"

      assert.equal([[
Usage: foo [-h] [-q] <command> ...

Options:
   -h, --help            Show this help message and exit.
   -q, --quiet

Commands:
   run                   Run! ]], parser:get_help())
   end)

   it("creates correct help message for subcommands", function()
      local parser = Parser "foo"
      parser:flag "-q" "--quiet"
      local run = parser:command "run"
      run:option "--where"

      assert.equal([[
Usage: foo run [-h] [--where <where>]

Options:
   -h, --help            Show this help message and exit.
   --where <where>]], run:get_help())
   end)

   it("uses message provided by user", function()
      local parser = Parser "foo"
         :help "I don't like your format of help messages"
      parser:flag "-q" "--quiet"

      assert.equal([[
I don't like your format of help messages]], parser:get_help())
   end)

   it("does not mention hidden arguments, options, and commands", function()
      local parser = Parser "foo"
      parser:argument "normal"
      parser:argument "deprecated"
         :args "?"
         :hidden(true)
      parser:flag "--feature"
      parser:flag "--misfeature"
         :hidden(true)
      parser:command "good"
      parser:command "okay"
      parser:command "never-use-this-one"
         :hidden(true)

      assert.equal([[
Usage: foo [-h] [--feature] <normal> <command> ...

Arguments:
   normal

Options:
   -h, --help            Show this help message and exit.
   --feature

Commands:
   good
   okay]], parser:get_help())
   end)

   it("omits categories if all elements are hidden", function()
      local parser = Parser "foo"
         :add_help(false)
      parser:argument "deprecated"
         :args "?"
         :hidden(true)
      parser:flag "--misfeature"
         :hidden(true)

      assert.equal([[
Usage: foo]], parser:get_help())
   end)

   it("does not mention hidden option and command aliases", function()
      local parser = Parser "foo"
      parser:option "--server"
         :hidden_name "--from"
      parser:command "newname"
         :hidden_name "oldname"

      assert.equal([[
Usage: foo [-h] [--server <server>] <command> ...

Options:
   -h, --help            Show this help message and exit.
   --server <server>

Commands:
   newname]], parser:get_help())
   end)

   it("supports grouping options", function()
      local parser = Parser "foo"
         :add_help(false)
      parser:argument "thing"

      parser:group("Options for setting position",
         parser:option "--coords"
            :args(2)
            :argname {"<x>", "<y>"}
            :description "Set coordinates.",
         parser:option "--polar"
            :args(2)
            :argname {"<rad>", "<ang>"}
            :description "Set polar coordinates."
      )

      parser:group("Options for setting style",
         parser:flag "--dotted"
            :description "More dots.",
         parser:option "--width"
            :argname "<px>"
            :description "Set width."
      )

      assert.equal([[
Usage: foo [--coords <x> <y>] [--polar <rad> <ang>] [--dotted]
       [--width <px>] <thing>

Arguments:
   thing

Options for setting position:
   --coords <x> <y>      Set coordinates.
   --polar <rad> <ang>   Set polar coordinates.

Options for setting style:
   --dotted              More dots.
   --width <px>          Set width.]], parser:get_help())
   end)

   it("adds default group with 'other' prefix if not all elements of a type are grouped", function()
      local parser = Parser "foo"

      parser:group("Main arguments",
         parser:argument "foo",
         parser:argument "bar",
         parser:flag "--use-default-args"
      )

      parser:argument "optional"
         :args "?"

      parser:group("Main options",
         parser:flag "--something",
         parser:option "--test"
      )

      parser:flag "--version"

      parser:group("Some commands",
         parser:command "foo",
         parser:command "bar"
      )

      parser:command "another-command"

      assert.equal([[
Usage: foo [-h] [--use-default-args] [--something] [--test <test>]
       [--version] <foo> <bar> [<optional>] <command> ...

Main arguments:
   foo
   bar
   --use-default-args

Other arguments:
   optional

Main options:
   --something
   --test <test>

Other options:
   -h, --help            Show this help message and exit.
   --version

Some commands:
   foo
   bar

Other commands:
   another-command]], parser:get_help())
   end)

   it("allows spacing out element help blocks more with help_vertical_space", function()
      local parser = Parser "foo"
         :help_vertical_space(1)

      parser:argument "arg1"
         :description "Argument number one."
      parser:argument "arg2"
         :description "Argument number two."

      parser:flag "-p"
         :description "This is a thing."
      parser:option "-f --foo"
         :description [[
And this things uses many lines.
Because it has lots of complex behaviour.
That needs documenting.]]

      assert.equal([[
Usage: foo [-h] [-p] [-f <foo>] <arg1> <arg2>

Arguments:

   arg1                  Argument number one.

   arg2                  Argument number two.

Options:

   -h, --help            Show this help message and exit.

   -p                    This is a thing.

      -f <foo>,          And this things uses many lines.
   --foo <foo>           Because it has lots of complex behaviour.
                         That needs documenting.]], parser:get_help())
   end)

   it("inherits help_vertical_space in commands", function()
      local parser = Parser "foo"
         :help_vertical_space(1)

      local cmd1 = parser:command "cmd1"
         :help_vertical_space(2)

      cmd1:flag("-a", "Do a thing.")
      cmd1:flag("-b", "Do b thing.")

      local cmd2 = parser:command "cmd2"

      cmd2:flag("-c", "Do c thing.")
      cmd2:flag("-d", "Do d thing.")

      assert.equal([[
Usage: foo cmd1 [-h] [-a] [-b]

Options:


   -h, --help            Show this help message and exit.


   -a                    Do a thing.


   -b                    Do b thing.]], cmd1:get_help())

      assert.equal([[
Usage: foo cmd2 [-h] [-c] [-d]

Options:

   -h, --help            Show this help message and exit.

   -c                    Do c thing.

   -d                    Do d thing.]], cmd2:get_help())
   end)

   it("allows configuring margins using help_usage_margin and help_description_margin", function()
      local parser = Parser "foo"
         :help_usage_margin(2)
         :help_description_margin(15)

      parser:argument "arg1"
         :description "Argument number one."
      parser:argument "arg2"
         :description "Argument number two."

      parser:flag "-p"
         :description "This is a thing."
      parser:option "-f --foo"
         :description [[
And this things uses many lines.
Because it has lots of complex behaviour.
That needs documenting.]]

      assert.equal([[
Usage: foo [-h] [-p] [-f <foo>] <arg1> <arg2>

Arguments:
  arg1         Argument number one.
  arg2         Argument number two.

Options:
  -h, --help   Show this help message and exit.
  -p           This is a thing.
     -f <foo>, And this things uses many lines.
  --foo <foo>  Because it has lots of complex behaviour.
               That needs documenting.]], parser:get_help())
   end)

   describe("autowrap", function()
      it("automatically wraps descriptions to match given max width", function()
         local parser = Parser "foo"
            :help_max_width(80)

         parser:option "-f --foo"
            :description("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " ..
               "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " ..
               "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " ..
               "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " ..
               "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
         parser:option "-b --bar"
            :description "See above."

         assert.equal([[
Usage: foo [-h] [-f <foo>] [-b <bar>]

Options:
   -h, --help            Show this help message and exit.
      -f <foo>,          Lorem ipsum dolor sit amet, consectetur adipiscing
   --foo <foo>           elit, sed do eiusmod tempor incididunt ut labore et
                         dolore magna aliqua. Ut enim ad minim veniam, quis
                         nostrud exercitation ullamco laboris nisi ut aliquip ex
                         ea commodo consequat. Duis aute irure dolor in
                         reprehenderit in voluptate velit esse cillum dolore eu
                         fugiat nulla pariatur. Excepteur sint occaecat
                         cupidatat non proident, sunt in culpa qui officia
                         deserunt mollit anim id est laborum.
      -b <bar>,          See above.
   --bar <bar>]], parser:get_help())
      end)

      it("preserves existing line breaks", function()
         local parser = Parser "foo"
            :help_max_width(80)

         parser:option "-f --foo"
            :description("This is a long line, it should be broken down into several lines. " .. [[
It just keeps going and going.
This should always be a new line.
Another one.
]])
         parser:option "-b --bar"

         assert.equal([[
Usage: foo [-h] [-f <foo>] [-b <bar>]

Options:
   -h, --help            Show this help message and exit.
      -f <foo>,          This is a long line, it should be broken down into
   --foo <foo>           several lines. It just keeps going and going.
                         This should always be a new line.
                         Another one.
      -b <bar>,
   --bar <bar>]], parser:get_help())
      end)

      it("preserves indentation", function()
         local parser = Parser "foo"
            :help_max_width(80)

         parser:option "-f --foo"
            :description("This is a long line, it should be broken down into several lines.\n" ..
               "   This paragraph is indented with three spaces, so when it gets broken down into several lines, " ..
               "they will be, too.\n\n" ..
               "  That was an empty line there, preserve it.")

         assert.equal([[
Usage: foo [-h] [-f <foo>]

Options:
   -h, --help            Show this help message and exit.
      -f <foo>,          This is a long line, it should be broken down into
   --foo <foo>           several lines.
                            This paragraph is indented with three spaces, so
                            when it gets broken down into several lines, they
                            will be, too.

                           That was an empty line there, preserve it.]], parser:get_help())
      end)

      it("preserves indentation of list items", function()
         local parser = Parser "foo"
            :help_max_width(80)

         parser:option "-f --foo"
            :description("Let's start a list:\n\n" ..
               "* Here is a list item.\n" ..
               "* Here is another one, this one is very long so it needs several lines. More words. Word. Word.\n" ..
               "  + Here is a nested list item. Word. Word. Word. Word. Word. Bird. Word. Bird. Bird. Bird.\n" ..
               "*   Back to normal list, this one uses several spaces after the list item mark. Bird. Bird. Bird.")


      assert.equal([[
Usage: foo [-h] [-f <foo>]

Options:
   -h, --help            Show this help message and exit.
      -f <foo>,          Let's start a list:
   --foo <foo>
                         * Here is a list item.
                         * Here is another one, this one is very long so it
                           needs several lines. More words. Word. Word.
                           + Here is a nested list item. Word. Word. Word. Word.
                             Word. Bird. Word. Bird. Bird. Bird.
                         *   Back to normal list, this one uses several spaces
                             after the list item mark. Bird. Bird. Bird.]], parser:get_help())
      end)

      it("preserves multiple spaces between words", function()
         local parser = Parser "foo"
            :help_max_width(80)

         parser:option "-f --foo"
            :description("This  is  a  long  line  with  two  spaces  between  words,  it  should  be  broken  down.")

         assert.equal([[
Usage: foo [-h] [-f <foo>]

Options:
   -h, --help            Show this help message and exit.
      -f <foo>,          This  is  a  long  line  with  two  spaces  between
   --foo <foo>           words,  it  should  be  broken  down.]], parser:get_help())
      end)

      it("autowraps description and epilog", function()
         local parser = Parser "foo"
            :help_max_width(80)
            :description("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " ..
               "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " ..
               "ullamco laboris nisi ut aliquip ex ea commodo consequat.")
            :epilog("Duis aute irure dolor in reprehenderit " ..
               "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " ..
               "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")

         assert.equal([[
Usage: foo [-h]

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Options:
   -h, --help            Show this help message and exit.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.]], parser:get_help())
      end)
   end)
end)