New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pattern groups #313
Open
frnhr
wants to merge
13
commits into
docopt:master
Choose a base branch
from
frnhr:pattern_groups
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Pattern groups #313
Conversation
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
Closed
I don't see why the following:
Couldn't be rewritten as:
Anyway, Docopt incorporates only POSIX standard, plus widely used conventions. So new syntax/semantics don't have any chance of being incorporated. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implementation of Pattern Groups
... a solution for medium-sized interfaces, when subparsers are an overkill, but interface is still getting too large for comfort.
Problem
Consider this not-so-much contrived example:
... or, the same interface specified like so:
Not very readable. Granted, It could have been somewhat shortened by using one-letter options, and "IN" instead of "SOURCE", but that is not really the direction we should be going.
Analysis
There are several problems with this particular example.
Long common subpattern
Common subpatterns can be put in front of other subpatterns, or after them. The former increases readability because other subpatterns are visually aligned, while at the same time decreasing it by moving important subpatterns to the right and possibly off the screen or wrapped to the next line.
vs :
Multiple axes of patterns
If there are multiple mutually exclusive subpatterns, pattern inevitably gets longer and complexity increases. Each group (pair or larger) of mutually exclusive subpatterns can be considered an axes in mathematical space of accepted usages.
Those spaces can, in principle, have multiple dimensions. Usage patterns by their nature are well suited for describing 1-dimensional spaces.
For clarity let us suppose possible subpatterns are
A1 | A2 | A3
andB1 | B2 | B3
. The space of accepted usages is:possibility 1 - list all combinations explicitly:
This might seem readable at first, but if sections are of different lengths (and they usually are), it quickly becomes Matrix.
possibility 2 - extract one element
Or similarly:
This is often the least problematic approach, but the patterns tend to get too long, again reducing readability.
possibility 3 - onebignastyonelinerofpatterns
no limits
There is no limit to either 3 points of any axes, or to 2 axes. Example could be contrived with subpatterns
A1-A5
,B1-B2
,C
andD1-D6
.Readability is decreased with each addition. Arguably, more then proportionally to the increase in actual interface complexity.
Solution - Pattern groups
Proposed solution is to extract much-repeating subpatterns and represent them with a group.
Keeping with the notation from the previous discussion, this interface:
... becomes:
Back to the example from the beginning, now implemented with pattern groups:
This type of specification is significantly more readable. And actually a bit shorter (in this example only about 30 characters shorter, but that is somewhat besides the point).
Mix and match
There is no obligation to put every repeatable subpattern in a group. Groups can be matched with other pattern elements in any way.
For example, important subpatterns can be kept in the usage pattern (even if they are repeated), while other subpattern can be extracted to groups. This example is equivalent to the one above, possibly even more readable:
Note how only part of the second repeating subpattern (database data) is now extracted into a group.
Implementation
Pattern groups (like
-source-
in the example above) are only "syntactic sugar". Before Docopt does any real parsing, the group elements (e.g.-source-
) are replaced by their respective pattern definitions (e.g.read_file <source_path> |(read_db <source_db> [--source_user=SOURCE_USER --source_pass=SOURCE_PASS] )
).Regex expression for group elements
Since elements like
-something-
are not useful for CLI specification, they are used here as a regex pattern for groups.Regex pattern will match
[letters, numbers, underscore, dash]
between[space_etc][dash]
and[dash][space_etc]
.Alternative group delimiters
Similar implementations are possible with other characters, as in
{my_group}
,~my_group~
,$my_group$
, etc. However, introducing new characters was deemed unnecessary, and possibly (though not likely) a source of collisions with other systems (e.g.{}
with Python'sstring.format
,$
and~
with Bash).Why Merge?
Not counting tests, comments, blank lines...
Oh, and total lines of code in docopt.py is still below 400! (398 by my count)
Interfaces have a tendency to grow. This keeps them readable, while adding very little complexity for both the application developer and Docopt.