option-help0.5.6 • Public • Published
some functions that help manage command line options
This is a library of utility functions that assist with making command line executables. It consists mainly of tools to help work with
minimist options and with outputting nicely formatted help pages.
This reference assumes the module is loaded in the variable
|args||Options output of minimist, which the function modifies.|
|configOptions||The configuration options provided to minimist to produces the given args. The function uses this to identify the boolean arguments and their aliases.|
Turns boolean arguments off when suffixed by
-. Minimist provides a
--no-<arg> option for setting the value of
<arg> to false. When an argument defaults to true (maybe because of an environment variable) and the user may need to frequently set it to false, the minimist way gets a bit cumbersome.
optionhelp.generateHelpGroup(group, delim, leftMargin, rightMargin, spaceEntries)
|group||An array of array pairs
|delim||The delimiter to place between the option template and the start of its description. Use space characters to space the two apart.|
|leftMargin||The margin at which to list the options templates.|
|rightMargin||The margin at which to wrap the option descriptions. Lines are wrapped at spaces.|
|spaceEntries||Whether to put a blank line between consecutive entries.|
Generates a string that shows the help information for a group of command line options. The option templates are all left-aligned, the option descriptions are all left aligned to the right of the longest option template, and the option descriptions wrap at word boundaries at the given right margin.
|flags||String of letters for flags that are true|
|flagLetter||Letter of flag to look for in flags|
Returns the boolean value of a flag according to a flag list, where flags are case-sensitive letters.
|args||Arguments output by minimist, which the function modifies by reducing arrays other than multiplesAllowed to just the last array element.|
|multiplesAllowed||Array of the names of options that collect all values instead of using only the last value supplied.|
Minimist collects multiple assignments of the same option into an array of all of the values assigned to that option. This feature is useful for allowing the command to support a default set of option values, such as via an environment variable. Applying the default option values before the actually provided options makes the last value of the array the intended value of the option. This function returns the last value of all array arguments except for those for which the caller specifies that multiple values are allowed.
|argv||Array of arguments input to minimist.|
|alternatives||Array of names of mutually exclusive options.|
Returns the name of the last option to appear in the argument list from a set of mutually exclusive alternatives, or null if none of the alternatives appears in the argument list.
optionhelp.wrapLine(line, maxWidth, leftMarginSize)
|line||Line to wrap, without trailing
|maxWidth||Column at which to wrap the line. This width includes the margin that leftMarginSize specifies.|
|leftMarginSize||Number of spaces that are to precede each wrapped line. (defaults to 0)|
Wraps the provided line at the given maximum width and return an array of the wrapped lines. Lines are wrapped the space boundary between words. Useful for generating help output.
optionhelp.wrapText(text, maxWidth, autoIndent)
|text||Text string of one or more lines to wrap.|
|maxWidth||Column at which to wrap the lines. This width includes the margin that leftMarginSize specifies.|
|autoIndent||Whether to automatically indent wrapped lines to the indentation at which they begin.|
Wraps the lines of the provided text at the given maximum width and returns the wrapped text as a string. Lines are wrapped on the space boundary between words. The function optionally automatically indents each wrapped line to the indentation at which the line begins. Useful for wrapping help output.
Returns: a string containing the text with all lines wrapped
This software is released under the MIT license:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright © 2016 Joseph T. Lapp