Skip to content

Commit

Permalink
Merge pull request #52 from Nick-Lucas/bug/issue-51-escaped-chars-str…
Browse files Browse the repository at this point in the history
…ipped

Bug/issue 51 escaped chars stripped
  • Loading branch information
Nick-Lucas authored Dec 23, 2017
2 parents 6f59c17 + ef82207 commit 92c6860
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/EntryPoint/EntryPoint.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Lightweight and Composable CLI Argument Parser for all modern .Net platforms</Description>
<AssemblyTitle>EntryPoint</AssemblyTitle>
<VersionPrefix>1.2.2</VersionPrefix>
<VersionPrefix>1.2.3</VersionPrefix>
<Authors>Nick Lucas</Authors>
<TargetFrameworks>netstandard1.6;net45</TargetFrameworks>
<AssemblyName>EntryPoint</AssemblyName>
Expand Down
22 changes: 5 additions & 17 deletions src/EntryPoint/Parsing/Tokeniser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ static IEnumerable<Token> TokeniseArgs(string[] args) {

static IEnumerable<Token> BasicTokenise(string args) {
bool isOption = false;
bool escaped = false;

StringBuilder token = new StringBuilder();
List<Token> tokens = new List<Token>();
Action StoreToken = () => {
void StoreToken() {
if (token.Length > 0) {
var t = token.ToString().Trim('"');
tokens.Add(new Token(t, isOption));
Expand All @@ -65,31 +64,20 @@ static IEnumerable<Token> BasicTokenise(string args) {
};

foreach (var c in args.ToCharArray()) {
if (!escaped && c == '\\') {
// If char is an unescaped escape, then set the escape state and continue
escaped = true;
continue;
}

if (!escaped && (c == '=')) {
// if char is an unescaped = then store the token and start again
if (c == '=') {
// if char is = then store the token and start again
// Whitespace splitting was already handled by .Net
StoreToken();

} else {
if (!escaped && token.Length == 0 && c == '-') {
if (token.Length == 0 && c == '-') {
// Mark the new token as an option
isOption = true;
}

// Otherwise append thiss char to the current token
// Otherwise append this char to the current token
token.Append(c);
}

if (escaped) {
// If this char was escape then switch off for the next char
escaped = false;
}
}
StoreToken();

Expand Down
22 changes: 4 additions & 18 deletions test/EntryPointTests/ParsingTests/TokeniserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,13 @@
namespace EntryPointTests.ParsingTests {
public class TokeniserTests {

// this text would be received if the user passed the argument in quotes (quotes would be stripped)
[Fact]
public void Tokenise_EscaleSingleDash() {
string[] args = new string[] { "--hello", @"\-world" };
public void Tokenise_EscapeQuotedArg() {
string[] args = new string[] { "--hello", @"w\orld" };
List<Token> expectedTokens = new List<Token>() {
new Token("--hello", true),
new Token("-world", false)
};

var tokens = Tokeniser.MakeTokens(args);
Assert.Equal(expectedTokens, tokens, new TokenEqualityComparer());
}

[Fact]
// TODO: revise this behaviour, it's correct and understood
// TODO: but maybe there is a precedent for having to escape
// TODO: both dashes to see a potential option as a parameter?
public void Tokenise_EscapeDoubleDash() {
string[] args = new string[] { "--hello", @"\--world" };
List<Token> expectedTokens = new List<Token>() {
new Token("--hello", true),
new Token("--world", false)
new Token(@"w\orld", false)
};

var tokens = Tokeniser.MakeTokens(args);
Expand Down

0 comments on commit 92c6860

Please sign in to comment.