|
| 1 | +### The First Stage Command Line Parser |
| 2 | + |
| 3 | +**IAllocator** defines how the allocator -- the object that |
| 4 | +allocates elements from the command line into a collection of text values |
| 5 | +keyed by option keys -- works. |
| 6 | + |
| 7 | +Given the sometimes oddball nature of what constitutes |
| 8 | +a traditional command line in different environments you may want to |
| 9 | +replace it with a custom one. This article describes the way the default |
| 10 | +implementation works as a means of touching on issues you may need to |
| 11 | +consider in writing your own. |
| 12 | + |
| 13 | +Conceptually the **AllocateCommandLine()** method is pretty simple. It reads |
| 14 | +characters one by one from the text it was given and decodes them into |
| 15 | +option keys and values. Unfortunately that is a somewhat complex process |
| 16 | +because the context of what a character means depends on the characters that |
| 17 | +have come before it. |
| 18 | + |
| 19 | +Here's the code to the default **AllocateCommandLine()** implementation: |
| 20 | +``` |
| 21 | +public IAllocations AllocateCommandLine( string cmdLine ) |
| 22 | +{ |
| 23 | + var retVal = new Allocations(_keyComp); |
| 24 | +
|
| 25 | + var accumulator = new StringBuilder(); |
| 26 | + IAllocation? curResult = null; |
| 27 | + var charsProcessed = 0; |
| 28 | + var lastElementWasKey = false; |
| 29 | +
|
| 30 | + for( var idx = 0; idx < cmdLine.Length; idx++) |
| 31 | + { |
| 32 | + accumulator.Append( cmdLine[idx] ); |
| 33 | + charsProcessed++; |
| 34 | +
|
| 35 | + var element = accumulator.ToString(); |
| 36 | +
|
| 37 | + // analyze the sequence as it currently stands to see if it includes |
| 38 | + // a prefixed key and/or has been terminated |
| 39 | + var maxPrefix = _prefixer.GetMaxPrefixLength(element); |
| 40 | + var maxTerminator = _terminator.GetMaxTerminatorLength(element, maxPrefix > 0); |
| 41 | +
|
| 42 | + // keep adding characters unless we've encountered a termination |
| 43 | + // sequence or we've reached the end of the command line |
| 44 | + if( maxTerminator <= 0 && charsProcessed < cmdLine.Length ) |
| 45 | + continue; |
| 46 | +
|
| 47 | + // extract the true element value from the prefix and terminator |
| 48 | + element = element[maxPrefix..^maxTerminator]; |
| 49 | +
|
| 50 | + // key values are identified by the presence of known prefixes |
| 51 | + if ( maxPrefix > 0 ) |
| 52 | + { |
| 53 | + // because multiple key references are allowed (e.g., "-x abc -x def") check |
| 54 | + // to see if the key is already recorded. We only create and store a new |
| 55 | + // Allocation if the key isn't already stored |
| 56 | + if( !retVal.Contains( element ) ) |
| 57 | + retVal.Add( new Allocation( retVal ) { Key = element } ); |
| 58 | +
|
| 59 | + // store a reference to the current/active Allocation unless the |
| 60 | + // element is a request for help (because help options cannot have |
| 61 | + // parameters) |
| 62 | + if( !_masterText.Contains( element, TextUsageType.HelpOptionKey ) ) |
| 63 | + { |
| 64 | + curResult = retVal[ element ]; |
| 65 | + lastElementWasKey = true; |
| 66 | + } |
| 67 | + else lastElementWasKey = false; |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + if( curResult == null || !lastElementWasKey ) |
| 72 | + retVal.Unkeyed.Parameters.Add( element ); |
| 73 | + else curResult.Parameters.Add( element ); |
| 74 | +
|
| 75 | + lastElementWasKey = false; |
| 76 | + } |
| 77 | +
|
| 78 | + // clear the accumulator so we can start processing the next character sequence |
| 79 | + accumulator.Clear(); |
| 80 | + } |
| 81 | +
|
| 82 | + return retVal; |
| 83 | +} |
| 84 | +``` |
| 85 | +Let's walk through a few key areas. |
| 86 | +``` |
| 87 | + var maxPrefix = Prefixer.GetMaxPrefixLength(element); |
| 88 | + var maxTerminator = _terminator.GetMaxTerminatorLength(element, maxPrefix > 0); |
| 89 | +
|
| 90 | + // keep adding characters unless we've encountered a termination |
| 91 | + // sequence or we've reached the end of the command line |
| 92 | + if( maxTerminator <= 0 && charsProcessed < cmdLine.Length ) |
| 93 | + continue; |
| 94 | +``` |
| 95 | +**maxPrefix** and **maxTerminator** are the character positions of the |
| 96 | +"furthest" key prefix (e.g., "-", "--" or some such) and termination |
| 97 | +sequence (usually a single character). Those are looked for in |
| 98 | +the variable **element**, which simply contains whatever is in the **StringBuilder** |
| 99 | +instance **accumulator**. |
| 100 | + |
| 101 | +A **maxPrefix** of zero means no prefix has yet been found. A **maxTerminator** |
| 102 | +of zero means no termination sequence has yet been found..but in deciding |
| 103 | +whether or not to keep reading characters you have to honor the physical |
| 104 | +end of the command line, which isn't itself a character. |
| 105 | +Once you've found a termination the first thing we do is strip away any |
| 106 | +key prefixes and termination sequences (i.e., all we want from here on out |
| 107 | +is the "pure" text value) from **element**. |
| 108 | + |
| 109 | +``` |
| 110 | + // extract the true element value from the prefix and terminator |
| 111 | + element = element[maxPrefix..^maxTerminator]; |
| 112 | +``` |
| 113 | +We then check to see if the element had an option key prefix (e.g., a "--"). |
| 114 | +If it does we check to see if we need a new **Allocation** object to hold |
| 115 | +parameter values. Since options can appear multiple times we don't want to |
| 116 | +create a new **Allocation** each time we find a key. We only create a |
| 117 | +new **Allocation** if there's isn't already one with that key in the |
| 118 | +**Allocations** collection we're building. |
| 119 | + |
| 120 | +We also check to see if the **Allocation** we just created was for a |
| 121 | +help request. We only update the current **Allocation** -- which is |
| 122 | +receiving text values -- if it's a non-help option. |
| 123 | +``` |
| 124 | +
|
| 125 | + // key values are identified by the presence of known prefixes |
| 126 | + if ( maxPrefix > 0 ) |
| 127 | + { |
| 128 | + // because multiple key references are allowed (e.g., "-x abc -x def") check |
| 129 | + // to see if the key is already recorded. We only create and store a new |
| 130 | + // Allocation if the key isn't already stored |
| 131 | + if( !retVal.Contains( element ) ) |
| 132 | + retVal.Add( new Allocation( retVal ) { Key = element } ); |
| 133 | +
|
| 134 | + // store a reference to the current/active Allocation unless the |
| 135 | + // element is a request for help (because help options cannot have |
| 136 | + // parameters) |
| 137 | + if( !_masterText.Contains( element, TextUsageType.HelpOptionKey ) ) |
| 138 | + { |
| 139 | + curResult = retVal[ element ]; |
| 140 | + lastElementWasKey = true; |
| 141 | + } |
| 142 | + else lastElementWasKey = false; |
| 143 | + } |
| 144 | +``` |
| 145 | +If the element wasn't a key it's just a text value that needs to be |
| 146 | +assigned to either the current **Allocation** or the global **Allocation** |
| 147 | +representing "unkeyed" text values (which are plain old non-option command line |
| 148 | +parameters). |
| 149 | +``` |
| 150 | + else |
| 151 | + { |
| 152 | + if( curResult == null || !lastElementWasKey ) |
| 153 | + retVal.Unkeyed.Parameters.Add( element ); |
| 154 | + else curResult.Parameters.Add( element ); |
| 155 | +
|
| 156 | + lastElementWasKey = false; |
| 157 | + } |
| 158 | +
|
| 159 | + // clear the accumulator so we can start processing the next character sequence |
| 160 | + accumulator.Clear(); |
| 161 | +``` |
0 commit comments