-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds a way to define "multi-value" options, which can be passed as `--name[]='value ...'`. See docs for more detail. This PR also adds a good handful of tests for `arg-processor`, and cleans up some of the internals of that library (having been emboldened by the new tests).
- Loading branch information
Showing
55 changed files
with
2,210 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
The `arg-processor` argument processing system. | ||
=============================================== | ||
|
||
Bashy-lib includes a full-featured argument processing system, which accepts an | ||
argument/option syntax which hews closely to modern Posix(ish) de facto | ||
standards, while innovating just a tad in order to help with robustness and | ||
expressivity. | ||
|
||
## Recognized syntax | ||
|
||
As one might expect, the top-level syntax is that a set of valid arguments | ||
consists of zero or more named options followed by zero or more positional | ||
arguments. | ||
|
||
Short options consist of a single dash (`-`) followed by a single letter, and in | ||
this system such options are not allowed to accept values. | ||
|
||
Long options all start with two dashes (`--`), followed by a series of | ||
alphanumerics and more dashes, e.g. `--some-option`. In addition: | ||
|
||
* A single value can be passed to an option by following the option name | ||
with an equal sign (`=`) and the arbitrary value, e.g. `--some-option='my | ||
value'`. | ||
|
||
* Multiple values can be passed to an option by following the option name with | ||
a pair of square brackets and an equal sign (`[]=`) and then a series of | ||
space-separated words, with standard shell rules for quoting and escaping in | ||
order to pass special characters, e.g. `--some-option[]='this "and that"'`. | ||
|
||
This multi-value form will also work for options that don't allow values or | ||
allow only one value (though there are probably few reasons to favor this form | ||
in those cases). | ||
|
||
The helper function `vals` is a convenient way to safely pass multiple values | ||
without having to worry about quoting hygiene. (That is, the helper handles it | ||
for you.) For example, `--some-option[]="$(vals "${myArray[@]}")"`. | ||
|
||
Special cases: | ||
* A single dash (`-`) is interpreted as a non-option argument. | ||
* A negative number, that is a dash followed by one or more digits, is | ||
interpreted as a non-option argument. | ||
* An argument consisting of just two dashes (`--`) is taken to indicate the | ||
end of _option_ parsing, with all subsequent arguments taken to be positional | ||
even if they (seem to) have valid option syntax. | ||
* If an option is a toggle, then: | ||
* Prefixing its long name with `no-` turns it off. | ||
* Specifying a value of `0` or `1` sets it to on or off explicitly. | ||
|
||
Examples: | ||
|
||
```bash | ||
my-cmd -h # Short option. | ||
my-cmd --help # Long option, no value. | ||
my-cmd --size=27 # Long option, with value. | ||
my-cmd --colors[]='red green blue' # Long option, multi-value. | ||
my-cmd --no-florp # Long option, turning off a toggle. | ||
my-cmd --florp=1 # Long option, setting a toggle explicitly. | ||
my-cmd some-argument # One positional argument. | ||
my-cmd - # One positional argument. | ||
my-cmd -34 # One positional argument. | ||
my-cmd -- --foo # One positional argument, literally `--foo`. | ||
|
||
# Passing arbitrary strings safely to a multi-value option. | ||
my-cmd --strings[]="$(vals "${paths[@]}")" | ||
``` | ||
|
||
## Declaring options | ||
|
||
TODO! Coming soon! In the mean time, see the source of all the included commands | ||
and tests for a rich set of examples. |
Oops, something went wrong.