|
| 1 | +=begin |
| 2 | + |
| 3 | +=head1 NAME |
| 4 | +
|
| 5 | +HLL::CommandLine - command line parsing tools |
| 6 | + |
| 7 | +=head1 SYNOPSIS |
| 8 | +
|
| 9 | + my $parser := HLL::CommandLine::Parser.new([ |
| 10 | + 'verbose', |
| 11 | + 'target=s', |
| 12 | + 'e=s' |
| 13 | + ]); |
| 14 | + |
| 15 | + # treat the first non-option argument as the program name, |
| 16 | + # and everything after that as arguments to the program |
| 17 | + $parser.stop-after-first-arg; |
| 18 | + |
| 19 | + # -e "program" also treats everything after it as arguments |
| 20 | + # to the program: |
| 21 | + $paser.add-stopper('-e'); |
| 22 | + |
| 23 | + my $results := $parser.parse(@*ARGS); |
| 24 | + my %options := $parser.options; |
| 25 | + my @args := $pasre.arguments; # remaining arguments from @*ARGS |
| 26 | + |
| 27 | +=head1 DESCRIPTION |
| 28 | +
|
| 29 | +HLL::CommandLine::Parser stores a specification of command line options and |
| 30 | +other behavior, and uses that to parse an array of command line directives. |
| 31 | + |
| 32 | +It classifies the directives as I<options> (usually of the form C<-f> or |
| 33 | +C<--foo>) and I<arguments> (ie. non-options). The result of a C<.parse(RPA)> |
| 34 | +call is an C<HLL::CommandLine::Result> object (or an exception thrown), |
| 35 | +which makes the options and arguments available via the methods C<options> |
| 36 | +and C<arguments>. |
| 37 | + |
| 38 | +=head1 HLL::CommandLine::Parser |
| 39 | +
|
| 40 | +=head2 new(Array) |
| 41 | +
|
| 42 | +The C<.new> method and constructor expects an array with option specifications. |
| 43 | +Such a specification is the name of an option, optionally followed by the |
| 44 | +C<=> equals sign and a single character describing the kind of value it expects. |
| 45 | +Missing value specification or C<b> stand for C<bool>, ie the option does not |
| 46 | +expect a value. C<s> stands for a string value. |
| 47 | +(Optional values are not yet supported). |
| 48 | + |
| 49 | +=head2 add-stopper(String) |
| 50 | +
|
| 51 | +Adds a stopper. A stopper is a special value that, when encountered in the |
| 52 | +command line arguments, terminates the processing, and classifies the rest |
| 53 | +of the strings as arguments, independently of their form. C<--> is a |
| 54 | +pre-defined stopper. If an option is used a stopper, that option itself is |
| 55 | +still processed. |
| 56 | + |
| 57 | +Examples: |
| 58 | + |
| 59 | + yourprogram -a --bar b -- c --foo |
| 60 | + # options: a = 1, bar = 1 |
| 61 | + # arguments: b, c, --foo |
| 62 | + |
| 63 | + # with stopper -e, and -e expecting a value: |
| 64 | + yourprogram -a -e foo --bar baz |
| 65 | + # options: -a = 1, -e = foo |
| 66 | + # arguments: --bar, baz |
| 67 | + |
| 68 | +=head2 parse(Array) |
| 69 | +
|
| 70 | +Parses the array as command line strings, and returns a |
| 71 | +C<HLL::CommandLine::Result> object (or thrown an exception on error). |
| 72 | + |
| 73 | +=head1 HLL::CommandLine::Result |
| 74 | +
|
| 75 | +An object of this type holds the options and arguments from a successful |
| 76 | +command line parse. |
| 77 | + |
| 78 | +=head2 options |
| 79 | +
|
| 80 | +Returns a hash of options |
| 81 | + |
| 82 | +=head2 arguments |
| 83 | +
|
| 84 | +Return an array of arguments. |
| 85 | + |
| 86 | +=end |
| 87 | + |
| 88 | +=cut |
| 89 | + |
| 90 | + |
1 | 91 | class HLL::CommandLine::Result {
|
2 | 92 | has @!arguments;
|
3 | 93 | has %!options;
|
|
0 commit comments