summaryrefslogtreecommitdiffstats
path: root/vespaclient/src/perl/lib/Yahoo/Vespa/ArgParser.pm
blob: 5abcd0b1fbb0445853131281edd23572c88d1950 (plain) (blame)
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
688
689
# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#
# Argument parser.
#
# Intentions:
#   - Make it very easy for programs to get info from command line.
#   - Allow shared libraries to register own options, such that a program can
#     delegate command line options to libraries used. (For instance, verbosity
#     arguments will be automatically delegated to console output module without
#     program needing to care much.
#   - Create a unified looking syntax page for all command line tools.
#   - Be able to reuse input validation. For instance that an integer don't
#     have a decimal point and that a hostname can be resolved.
# 

package Yahoo::Vespa::ArgParser;

use strict;
use warnings;
use Yahoo::Vespa::ConsoleOutput;
use Yahoo::Vespa::Utils;

BEGIN { # - Define exports and dependency aliases for module.
    use base 'Exporter';
    our @EXPORT = qw(
        addArgParserValidator
        setProgramBinaryName setProgramDescription
        setArgument setOptionHeader
        setFlagOption setHostOption setPortOption setStringOption
        setIntegerOption setFloatOption setUpCountingOption setDownCountingOption
        handleCommandLineArguments
        OPTION_SECRET OPTION_INVERTEDFLAG OPTION_REQUIRED
    );
        # Alias so we can avoid writing the entire package name
    *ConsoleOutput:: = *Yahoo::Vespa::ConsoleOutput::
}

my @ARGUMENTS;
my $DESCRIPTION;
my $BINARY_NAME;
my @ARG_SPEC_ARRAY;
my %OPTION_SPEC;
my @OPTION_SPEC_ARRAY;
my $SYNTAX_PAGE;
my $SHOW_HIDDEN;
my @VALIDATORS;
use constant OPTION_SECRET => 1;
use constant OPTION_INVERTEDFLAG => 2;
use constant OPTION_ADDFIRST => 4;
use constant OPTION_REQUIRED => 8;

# These variables are properties needed by ConsoleOutput module. ArgParser
# handles that modules argument settings as it cannot possibly depend upon
# ArgParser itself.
my $VERBOSITY; # Default verbosity before parsing arguments
my $ANSI_COLORS; # Whether to use ansi colors or not.

&initialize();

return 1;

########################## Default exported functions ########################

sub handleCommandLineArguments { # () Parses and sets all values
    my ($args, $validate_args_sub) = @_;

    &registerInternalParameters();
    if (!&parseCommandLineArguments($args)) {
        &writeSyntaxPage();
        exitApplication(1);
    }
    if (defined $validate_args_sub && !&$validate_args_sub()) {
        &writeSyntaxPage();
        exitApplication(1);
    }
    if ($SYNTAX_PAGE) {
        &writeSyntaxPage();
        exitApplication(0);
    }
}

sub addArgParserValidator { # (Validator) Add callback to verify parsing
    # Using such callbacks you can verify more than is supported natively by
    # argument parser, such that you can fail argument parsing at same step as
    # internally supported checks are handled.
    scalar @_ == 1 or confess "Invalid number of arguments given.";
    push @VALIDATORS, $_[0];
}
sub setProgramBinaryName { # (Name)  Defaults to name used on command line
    scalar @_ == 1 or confess "Invalid number of arguments given.";
    ($BINARY_NAME) = @_;
}
sub setProgramDescription { # (Description)
    scalar @_ == 1 or confess "Invalid number of arguments given.";
    ($DESCRIPTION) = @_;
}

sub setOptionHeader { # (Description)
    my ($desc) = @_;
    push @OPTION_SPEC_ARRAY, $desc; 
}

sub setFlagOption { # (ids[], Result&, Description, Flags)
    scalar @_ >= 3 or confess "Invalid number of arguments given.";
    my ($ids, $result, $description, $flags) = @_;
    if (!defined $flags) { $flags = 0; }
    my %optionspec = (
        'result' => $result,
        'flags' => $flags,
        'ids' => $ids,
        'description' => $description,
        'arg_count' => 0,
        'initializer' => sub {
            $$result = (($flags & OPTION_INVERTEDFLAG) == 0 ? 0 : 1);
            return 1;
        },
        'result_evaluator' => sub {
            $$result = (($flags & OPTION_INVERTEDFLAG) == 0 ? 1 : 0);
            return 1;
        }
    );
    setGenericOption($ids, \%optionspec);
}
sub setHostOption { # (ids[], Result&, Description, Flags)
    my ($ids, $result, $description, $flags) = @_;
    my %optionspec = (
        'result' => $result,
        'flags' => $flags,
        'ids' => $ids,
        'description' => $description,
        'arg_count' => 1,
        'result_evaluator' => sub {
            my ($id, $args) = @_;
            scalar @$args == 1 or confess "Should have one arg here.";
            my $host = $$args[0]; 
            if (!&validHost($host)) {
                printError "Invalid host '$host' given to option '$id'. "
                         . "Not a valid host\n";
                return 0;
            }
            printSpam "Set value of '$id' to $host.\n";
            $$result = $host;
            return 1;
        }
    );
    setGenericOption($ids, \%optionspec);
}
sub setPortOption { # (ids[], Result&, Description, Flags)
    my ($ids, $result, $description, $flags) = @_;
    my %optionspec = (
        'result' => $result,
        'flags' => $flags,
        'ids' => $ids,
        'description' => $description,
        'arg_count' => 1,
        'result_evaluator' => sub {
            my ($id, $args) = @_;
            scalar @$args == 1 or confess "Should have one arg here.";
            my $val = $$args[0]; 
            if ($val !~ /^\d+$/ || $val < 0 || $val >= 65536)  {
                printError "Invalid value '$val' given to port option '$id'."
                         . " Must be an unsigned 16 bit integer.\n";
                return 0;
            }
            printSpam "Set value of '$id' to $val.\n";
            $$result = $val;
            return 1;
        }
    );
    setGenericOption($ids, \%optionspec);
}
sub setIntegerOption { # (ids[], Result&, Description, Flags)
    my ($ids, $result, $description, $flags) = @_;
    my %optionspec = (
        'result' => $result,
        'flags' => $flags,
        'ids' => $ids,
        'description' => $description,
        'arg_count' => 1,
        'result_evaluator' => sub {
            my ($id, $args) = @_;
            scalar @$args == 1 or confess "Should have one arg here.";
            my $val = $$args[0]; 
            if ($val !~ /^(?:[-\+])?\d+$/)  {
                printError "Invalid value '$val' given to integer option "
                         . "'$id'.\n";
                return 0;
            }
            printSpam "Set value of '$id' to $val.\n";
            $$result = $val;
            return 1;
        }
    );
    setGenericOption($ids, \%optionspec);
}
sub setFloatOption { # (ids[], Result&, Description, Flags)
    my ($ids, $result, $description, $flags) = @_;
    my %optionspec = (
        'result' => $result,
        'flags' => $flags,
        'ids' => $ids,
        'description' => $description,
        'arg_count' => 1,
        'result_evaluator' => sub {
            my ($id, $args) = @_;
            scalar @$args == 1 or confess "Should have one arg here.";
            my $val = $$args[0]; 
            if ($val !~ /^(?:[-\+])?\d+(?:\.\d+)?$/)  {
                printError "Invalid value '$val' given to float option "
                         . "'$id'.\n";
                return 0;
            }
            printSpam "Set value of '$id' to $val.\n";
            $$result = $val;
            return 1;
        }
    );
    setGenericOption($ids, \%optionspec);
}
sub setStringOption { # (ids[], Result&, Description, Flags)
    my ($ids, $result, $description, $flags) = @_;
    my %optionspec = (
        'result' => $result,
        'flags' => $flags,
        'ids' => $ids,
        'description' => $description,
        'arg_count' => 1,
        'result_evaluator' => sub {
            my ($id, $args) = @_;
            scalar @$args == 1 or confess "Should have one arg here.";
            my $val = $$args[0]; 
            printSpam "Set value of '$id' to $val.\n";
            $$result = $val;
            return 1;
        }
    );
    setGenericOption($ids, \%optionspec);
}
sub setUpCountingOption { # (ids[], Result&, Description, Flags)
    my ($ids, $result, $description, $flags) = @_;
    my $org = $$result;
    my %optionspec = (
        'result' => $result,
        'flags' => $flags,
        'ids' => $ids,
        'description' => $description,
        'arg_count' => 0,
        'initializer' => sub {
            $$result = $org;
            return 1;
        },
        'result_evaluator' => sub {
            if (!defined $$result) {
                $$result = 0;
            }
            ++$$result;
            return 1;
        }
    );
    setGenericOption($ids, \%optionspec);
}
sub setDownCountingOption { # (ids[], Result&, Description, Flags)
    my ($ids, $result, $description, $flags) = @_;
    my $org = $$result;
    my %optionspec = (
        'result' => $result,
        'flags' => $flags,
        'ids' => $ids,
        'description' => $description,
        'arg_count' => 0,
        'initializer' => sub {
            $$result = $org;
            return 1;
        },
        'result_evaluator' => sub {
            if (!defined $$result) {
                $$result = 0;
            }
            --$$result;
            return 1;
        }
    );
    setGenericOption($ids, \%optionspec);
}

sub setArgument { # (Result&, Name, Description)
    my ($result, $name, $description, $flags) = @_;
    if (!defined $flags) { $flags = 0; }
    if (scalar @ARG_SPEC_ARRAY > 0 && ($flags & OPTION_REQUIRED) != 0) {
        my $last = $ARG_SPEC_ARRAY[scalar @ARG_SPEC_ARRAY - 1];
        if (($$last{'flags'} & OPTION_REQUIRED) == 0) {
            confess "Cannot add required argument after optional argument";
        }
    }
    my %argspec = (
        'result' => $result,
        'flags' => $flags,
        'name' => $name,
        'description' => $description,
        'result_evaluator' => sub {
            my ($arg) = @_;
            $$result = $arg;
            return 1;
        }
    );
    push @ARG_SPEC_ARRAY, \%argspec;
}

######################## Externally usable functions #######################

sub registerInternalParameters { # ()
    # Register console output parameters too, as the output module can't depend
    # on this tool.
    setFlagOption(
            ['show-hidden'],
            \$SHOW_HIDDEN,
            'Also show hidden undocumented debug options.',
            OPTION_ADDFIRST);
    setDownCountingOption(
            ['s'],
            \$VERBOSITY,
            'Create less verbose output.',
            OPTION_ADDFIRST);
    setUpCountingOption(
            ['v'],
            \$VERBOSITY,
            'Create more verbose output.',
            OPTION_ADDFIRST);
    setFlagOption(
            ['h', 'help'],
            \$SYNTAX_PAGE,
            'Show this help page.',
            OPTION_ADDFIRST);

    # If color use is supported and turned on by default, give option to not use
    if ($ANSI_COLORS) {
        setOptionHeader('');
        setFlagOption(
                ['nocolors'],
                \$ANSI_COLORS,
                'Do not use ansi colors in print.',
                OPTION_SECRET | OPTION_INVERTEDFLAG);
    }
}
sub setShowHidden { # (Bool)
    $SHOW_HIDDEN = ($_[0] ? 1 : 0);
}

############## Utility functions - Not intended for external use #############

sub initialize { # ()
    $VERBOSITY = 3;
    $ANSI_COLORS = Yahoo::Vespa::ConsoleOutput::ansiColorsSupported();
    $DESCRIPTION = undef;
    $BINARY_NAME = $0;
    if ($BINARY_NAME =~ /\/([^\/]+)$/) {
        $BINARY_NAME = $1;
    }
    %OPTION_SPEC = ();
    @OPTION_SPEC_ARRAY = ();
    @ARG_SPEC_ARRAY = ();
    @VALIDATORS = ();
    $SYNTAX_PAGE = undef;
    $SHOW_HIDDEN = undef;
    @ARGUMENTS = undef;
}
sub parseCommandLineArguments { # (ArgumentListRef)
    printDebug "Parsing command line arguments\n";
    @ARGUMENTS = @{ $_[0] };
    foreach my $spec (@OPTION_SPEC_ARRAY) {
        if (ref($spec) && exists $$spec{'initializer'}) {
            my $initsub = $$spec{'initializer'};
            &$initsub();
        }
    }
    my %eaten_args;
    if (!&parseOptions(\%eaten_args)) {
        printDebug "Option parsing failed\n";
        return 0;
    }
    if (!&parseArguments(\%eaten_args)) {
        printDebug "Argument parsing failed\n";
        return 0;
    }
    ConsoleOutput::setVerbosity($VERBOSITY);
    ConsoleOutput::setUseAnsiColors($ANSI_COLORS);
    return 1;
}
sub writeSyntaxPage { # ()
    if (defined $DESCRIPTION) {
        printResult $DESCRIPTION . "\n\n";
    }
    printResult "Usage: " . $BINARY_NAME;
    if (scalar keys %OPTION_SPEC > 0) {
        printResult " [Options]";
    }
    foreach my $arg (@ARG_SPEC_ARRAY) {
        if (($$arg{'flags'} & OPTION_REQUIRED) != 0) {
            printResult " <" . $$arg{'name'} . ">";
        } else {
            printResult " [" . $$arg{'name'} . "]";
        }
    }
    printResult "\n";

    if (scalar @ARG_SPEC_ARRAY > 0) {
        &writeArgumentSyntax();
    }
    if (scalar keys %OPTION_SPEC > 0) {
        &writeOptionSyntax();
    }
}
sub setGenericOption { # (ids[], Optionspec)
    my ($ids, $spec) = @_;
    if (!defined $$spec{'flags'}) {
        $$spec{'flags'} = 0;
    }
    foreach my $id (@$ids) {
        if (length $id == 1 && $id =~ /[0-9]/) {
            confess "A short option can not be a digit. Reserved so we can parse "
                . "-4 as a negative number argument rather than an option 4";
        }
    }
    foreach my $id (@$ids) {
        $OPTION_SPEC{$id} = $spec;
    }
    if (($$spec{'flags'} & OPTION_ADDFIRST) == 0) {
        push @OPTION_SPEC_ARRAY, $spec;
    } else {
        unshift @OPTION_SPEC_ARRAY, $spec;
    }
}
sub parseArguments { # (EatenArgs)
    my ($eaten_args) = @_;
    my $stopIndex = 10000000;
    my $argIndex = 0;
    printSpam "Parsing arguments\n";
    for (my $i=0; $i<scalar @ARGUMENTS; ++$i) {
        printSpam "Processing arg '$ARGUMENTS[$i]'.\n";
        if ($i <= $stopIndex && $ARGUMENTS[$i] eq '--') {
            printSpam "Found --. Further dash prefixed args will be args\n";
            $stopIndex = $i;
        } elsif ($i <= $stopIndex && $ARGUMENTS[$i] =~ /^-/) {
            printSpam "Option declaration. Ignoring\n";
        } elsif (exists $$eaten_args{$i}) {
            printSpam "Already eaten argument. Ignoring\n";
        } elsif ($argIndex < scalar @ARG_SPEC_ARRAY) {
            my $spec = $ARG_SPEC_ARRAY[$argIndex];
            my $name = $$spec{'name'};
            if (!&{$$spec{'result_evaluator'}}($ARGUMENTS[$i])) {
                printDebug "Failed evaluate result of arg $name. Aborting\n";
                return 0;
            }
            printSpam "Successful parsing of argument '$name'.\n";
            $$eaten_args{$i} = 1;
            ++$argIndex;
        } else {
            printError "Unhandled argument '$ARGUMENTS[$i]'.\n";
            return 0;
        }
    }
    if ($SYNTAX_PAGE) { # Ignore required arg check if syntax page is to be shown
        return 1;
    }
    for (my $i=$argIndex; $i<scalar @ARG_SPEC_ARRAY; ++$i) {
        my $spec = $ARG_SPEC_ARRAY[$i];
        if (($$spec{'flags'} & OPTION_REQUIRED) != 0) {
            my $name = $$spec{'name'};
            printError "Argument $name is required but not specified.\n";
            return 0;
        }
    }
    return 1;
}
sub getOptionArguments { # (Count, MinIndex, EatenArgs)
    my ($count, $minIndex, $eaten_args) = @_;
    my $stopIndex = 10000000;
    my @result;
    if ($count == 0) { return \@result; }
    for (my $i=0; $i<scalar @ARGUMENTS; ++$i) {
        printSpam "Processing arg '$ARGUMENTS[$i]'.\n";
        if ($i <= $stopIndex && $ARGUMENTS[$i] eq '--') {
            printSpam "Found --. Further dash prefixed args will be args\n";
            $stopIndex = $i;
        } elsif ($i <= $stopIndex && $ARGUMENTS[$i] =~ /^-[^0-9]/) {
            printSpam "Option declaration. Ignoring\n";
        } elsif (exists $$eaten_args{$i}) {
            printSpam "Already eaten argument. Ignoring\n";
        } elsif ($i < $minIndex) {
            printSpam "Not eaten, but too low index to be option arg.\n";
        } else {
            printSpam "Using argument\n";
            push @result, $ARGUMENTS[$i];
            $$eaten_args{$i} = 1;
            if (scalar @result == $count) {
                return \@result;
            }
        }
    }
    printSpam "Too few option arguments found. Returning undef\n";
    return;
}
sub parseOption { # (Id, EatenArgs, Index)
    my ($id, $eaten_args, $index) = @_;
    if (!exists $OPTION_SPEC{$id}) {
        printError "Unknown option '$id'.\n";
        return 0;
    }
    my $spec = $OPTION_SPEC{$id};
    my $args = getOptionArguments($$spec{'arg_count'}, $index, $eaten_args);
    if (!defined $args) {
        printError "Too few arguments for option '$id'.\n";
        return 0;
    }
    printSpam, "Found " . (scalar @$args) . " args\n";
    if (!&{$$spec{'result_evaluator'}}($id, $args)) {
        printDebug "Failed evaluate result of option '$id'. Aborting\n";
        return 0;
    }
    printSpam "Successful parsing of option '$id'.\n";
    return 1;
}
sub parseOptions { # (EatenArgs)
    my ($eaten_args) = @_;
    for (my $i=0; $i<scalar @ARGUMENTS; ++$i) {
        if ($ARGUMENTS[$i] =~ /^--(.+)$/) {
            my $id = $1;
            printSpam "Parsing long option '$id'.\n";
            if (!&parseOption($id, $eaten_args, $i)) {
                return 0;
            }
        } elsif ($ARGUMENTS[$i] =~ /^-([^0-9].*)$/) {
            my $shortids = $1;
            while ($shortids =~ /^(.)(.*)$/) {
                my ($id, $rest) = ($1, $2);
                printSpam "Parsing short option '$id'.\n";
                if (!&parseOption($id, $eaten_args, $i)) {
                    return 0;
                }
                $shortids = $rest;
            }
        }
    }
    printSpam "Successful parsing of all options.\n";
    return 1;
}
sub writeArgumentSyntax { # ()
    printResult "\nArguments:\n";
    my $max_name_length = &getMaxNameLength();
    if ($max_name_length > 30) { $max_name_length = 30; }
    foreach my $spec (@ARG_SPEC_ARRAY) {
        &writeArgumentName($$spec{'name'}, $max_name_length);
        &writeOptionDescription($spec, $max_name_length + 3);
    }
}
sub getMaxNameLength { # ()
    my $max = 0;
    foreach my $spec (@ARG_SPEC_ARRAY) {
        my $len = 1 + length $$spec{'name'};
        if ($len > $max) { $max = $len; }
    }
    return $max;
}
sub writeArgumentName { # (Name, MaxNameLength)
    my ($name, $maxnamelen) = @_;
    printResult " $name";
    my $totalLength = 1 + length $name;
    if ($totalLength <= $maxnamelen) {
        for (my $i=$totalLength; $i<$maxnamelen; ++$i) {
            printResult ' ';
        }
    } else {
        printResult "\n";
        for (my $i=0; $i<$maxnamelen; ++$i) {
            printResult ' ';
        }
    }
    printResult " : ";
}
sub writeOptionSyntax { # ()
    printResult "\nOptions:\n";
    my $max_id_length = &getMaxIdLength();
    if ($max_id_length > 30) { $max_id_length = 30; }
    my $cachedHeader;
    foreach my $spec (@OPTION_SPEC_ARRAY) {
        if (ref($spec) eq 'HASH') {
            my $flags = $$spec{'flags'};
            if ($SHOW_HIDDEN || ($flags & OPTION_SECRET) == 0) {
                if (defined $cachedHeader) {
                    printResult "\n";
                    if ($cachedHeader ne '') {
                        &writeOptionHeader($cachedHeader);
                    }
                    $cachedHeader = undef;
                }
                &writeOptionId($spec, $max_id_length);
                &writeOptionDescription($spec, $max_id_length + 3);
            }
        } else {
            $cachedHeader = $spec;
        }
    }
}
sub getMaxIdLength { # ()
    my $max = 0;
    foreach my $spec (@OPTION_SPEC_ARRAY) {
        if (!ref($spec)) { next; } # Ignore option headers
        my $size = 0;
        foreach my $id (@{ $$spec{'ids'} }) {
            my $len = length $id;
            if ($len == 1) {
                $size += 3;
            } else {
                $size += 3 + $len;
            }
        }
        if ($size > $max) { $max = $size; }
    }
    return $max;
}
sub writeOptionId { # (Spec, MaxNameLength)
    my ($spec, $maxidlen) = @_;
    my $totalLength = 0;
    foreach my $id (@{ $$spec{'ids'} }) {
        my $len = length $id;
        if ($len == 1) {
            printResult " -" . $id;
            $totalLength += 3;
        } else {
            printResult " --" . $id;
            $totalLength += 3 + $len;
        }
    }
    if ($totalLength <= $maxidlen) {
        for (my $i=$totalLength; $i<$maxidlen; ++$i) {
            printResult ' ';
        }
    } else {
        printResult "\n";
        for (my $i=0; $i<$maxidlen; ++$i) {
            printResult ' ';
        }
    }
    printResult " : ";
}
sub writeOptionDescription { # (Spec, MaxNameLength)
    my ($spec, $maxidlen) = @_;
    my $width = ConsoleOutput::getTerminalWidth() - $maxidlen;
    my $desc = $$spec{'description'};
    my $min = int ($width / 2);
    while (length $desc > $width) {
        if ($desc =~ /^(.{$min,$width}) (.*)$/s) {
            my ($first, $rest) = ($1, $2);
            printResult $first . "\n";
            for (my $i=0; $i<$maxidlen; ++$i) {
                printResult ' ';
            }
            $desc = $rest;
        } else {
            last;
        }
    }
    printResult $desc . "\n";
}
sub writeOptionHeader { # (Description)
    my ($desc) = @_;
    my $width = ConsoleOutput::getTerminalWidth();
    my $min = 2 * $width / 3;
    while (length $desc > $width) {
        if ($desc =~ /^(.{$min,$width}) (.*)$/s) {
            my ($first, $rest) = ($1, $2);
            printResult $first . "\n";
            $desc = $rest;
        } else {
            last;
        }
    }
    printResult $desc . "\n";
}
sub validHost { # (Hostname)
    my ($host) = @_;
    if ($host !~ /^[a-zA-Z][-_a-zA-Z0-9\.]*$/) {
        return 0;
    }
    if (system("host $host >/dev/null 2>/dev/null") != 0) {
        return 0;
    }
    return 1;
}