Skip to content

Commit b9c59ac

Browse files
Added additional help screen for patterns.
1 parent 1a10a10 commit b9c59ac

File tree

5 files changed

+94
-34
lines changed

5 files changed

+94
-34
lines changed

README.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<#{"seed":1}#>
1+
22

33
# ![TestDataGenerator](https://raw.githubusercontent.com/SecretDeveloper/TestDataGenerator/master/icon.png) TestDataGenerator
44
A library and command line tool that can be used to generate data for testing, templating and other uses. You provide it with a pattern containing symbols defining the output you want to produce and it will create random data to match that pattern.
@@ -45,50 +45,50 @@ The following symbols are shorthand tokens which you can use in your generation
4545
### Groups
4646
Groups can contain multiple Symbols or characters and allows us to treat them as a single unit. Their usage becomes apparent when using them with *repetitions* or *alternations*. Groups are surrounded in () brackets.
4747
#### Simple Groups
48-
- `(\d)` - A number between 0 and 9 e.g. '8'
49-
- `(ABC\d)` - 'ABC' followed by a number between 0 and 9 e.g. ABC7
50-
- `\L(\d)\L` - A upper-case letter, three digits between 0 and 9 and another upper-case letter e.g. H2O
48+
- `(\d)` - A number between 0 and 9 e.g. '2'
49+
- `(ABC\d)` - 'ABC' followed by a number between 0 and 9 e.g. ABC1
50+
- `\L(\d)\L` - A upper-case letter, three digits between 0 and 9 and another upper-case letter e.g. M7R
5151

5252
#### Alternations
5353
When a group contains alternations the patterns are divided by the | character, during processing one of the alternated patterns is selected at random.
5454
Groups can contain several individual symbols or groups of symbols and randomly alternate between them when generating the output value.
5555

5656
**Alternatives must be contained within a Group**
57-
- `(\L\L|\d\d)` - Either two upper-case letters OR two numbers e.g. 'RX'
58-
- `(\L\L|\d\d|[AEIOU]|[100-120])` - Either two upper-case letters OR two digits OR an upper-case vowel OR a number between 100 and 120 e.g. 'U'
57+
- `(\L\L|\d\d)` - Either two upper-case letters OR two numbers e.g. 'JY'
58+
- `(\L\L|\d\d|[AEIOU]|[100-120])` - Either two upper-case letters OR two digits OR an upper-case vowel OR a number between 100 and 120 e.g. 'QA'
5959
- `(\C|)` - Either a upper-case Consonant or nothing.
6060

6161
### Ranges
6262
Ranges can contain multiple characters or ranges of characters but no symbols (the items defined within the range will be what is used, no special symbols are allowed). The item to be produced is selected at random.
6363
#### Character ranges
64-
- `[abc]` - Either 'a','b' or 'c' e.g. 'b'
65-
- `[a-z]` - A single lower-case letter between a and z e.g. 'v'
66-
- `[A-Z]` - A single upper-case letter between A and Z e.g. 'D'
67-
- `[A-D]` - A single upper-case letter between A and D e.g. 'D'
68-
- `[A-Da-z]` - A single character between A and D or between a and z e.g. 'n'
64+
- `[abc]` - Either 'a','b' or 'c' e.g. 'a'
65+
- `[a-z]` - A single lower-case letter between a and z e.g. 'z'
66+
- `[A-Z]` - A single upper-case letter between A and Z e.g. 'R'
67+
- `[A-D]` - A single upper-case letter between A and D e.g. 'C'
68+
- `[A-Da-z]` - A single character between A and D or between a and z e.g. 's'
6969

7070
#### Numeric ranges
71-
- `[1-5]` - A number between 1 and 5 e.g. '3'
72-
- `[100-500]` - A number between 100 and 500 e.g. '161'
73-
- `[1.25-5]` - A decimal number between 1.25 and 5. The scope or number of decimal places is taken from the first number defined (1.25 in this case) and the produced value will have the same number of decimal places. e.g. '3.39'
71+
- `[1-5]` - A number between 1 and 5 e.g. '1'
72+
- `[100-500]` - A number between 100 and 500 e.g. '253'
73+
- `[1.25-5]` - A decimal number between 1.25 and 5. The scope or number of decimal places is taken from the first number defined (1.25 in this case) and the produced value will have the same number of decimal places. e.g. '1.89'
7474

7575
### Named Parameters
7676
A named pattern is surrounded with @ characters and links to a predefined pattern loaded from a file. The `default.tdg-patterns` file located in the same directory as the tdg executable file contains a list of named patterns which can be used in other patterns you write. For example to generate you could write something like `([1-9]\d\d-\d\d-\d\d\d\d)` or you can use the named parameter in the file `(@misc_ssn@)` to a similar value. You can add more patterns to the file as you wish. Named patterns can also include other named patterns if you so wish.
7777

78-
Take a look at the `@address_us_type1@` pattern in the file as an example of a compound pattern than uses other patterns to produce an output e.g. '5376 Pleasant Lane, Stafford County, Oregon, 68443'
78+
Take a look at the `@address_us_type1@` pattern in the file as an example of a compound pattern than uses other patterns to produce an output e.g. '8579 Shady Lane, Howard County, Montana, 52963'
7979

8080
### Repetition
8181
Repetition is a powerful feature allowing for complicated data production. A Symbol, Group or Range can be repeated a set or random number of times by using the following syntax.
8282

8383
#### Quantity Repetition
84-
- `\d{5}` - Will generate 5 number characters e.g. '74910'
85-
- `(\L\d\L){5}` - Will generate 5 upper-case letter, number, upper-case letter items e.g. 'Z8XA2AU8QU6QI2M'
86-
- `[ABC]{5}` - Will generate 5 items where each item will be either 'A','B' or 'C' e.g. 'ACCBA'
84+
- `\d{5}` - Will generate 5 number characters e.g. '46642'
85+
- `(\L\d\L){5}` - Will generate 5 upper-case letter, number, upper-case letter items e.g. 'J5EJ6KE9CZ4QB1F'
86+
- `[ABC]{5}` - Will generate 5 items where each item will be either 'A','B' or 'C' e.g. 'AABAC'
8787

8888
#### Random Quantity
89-
- `\d{5,10}` - Will generate between 5 and 10 number characters e.g. '39296896'
90-
- `(\L\d\L){5,10}` - Will generate between 5 and 10 upper-case letter, number, upper-case letter items e.g. 'A5UQ4QI1NB8YE4AZ5GA0B'
91-
- `[ABC]{5,10}` - Will generate between 5 and 10 items where each item will be either 'A','B' or 'C' e.g. 'BCBCCCAB'
89+
- `\d{5,10}` - Will generate between 5 and 10 number characters e.g. '7211494791'
90+
- `(\L\d\L){5,10}` - Will generate between 5 and 10 upper-case letter, number, upper-case letter items e.g. 'I0MJ3JA6VW7CA8FW4HD1J'
91+
- `[ABC]{5,10}` - Will generate between 5 and 10 items where each item will be either 'A','B' or 'C' e.g. 'BBAAAAB'
9292

9393

9494
## Template Syntax
@@ -98,7 +98,7 @@ You can create template documents that contain multiple pattern syntax placehold
9898

9999
### Placeholders
100100
You can place your symbols and patterns within placeholders which will be replaced with the generated values. These placeholders contain 1 or more symbols representing the desired output characters.
101-
Within a template all text not within a placeholder is treated as normal text with no special handling taking place. Patterns that need to be randomly generated should be placed inside '<< >>' tokens e.g. 'This is a template <<\L\L>>' produces 'This is a template DW'. You can also escape placeholders by placing a '\' before them which will prevent them from being processed. To place a '\' before a placeholder in the generated content you need to place 2 backslashes before the placeholder.
101+
Within a template all text not within a placeholder is treated as normal text with no special handling taking place. Patterns that need to be randomly generated should be placed inside '<< >>' tokens e.g. 'This is a template <<\L\L>>' produces 'This is a template TE'. You can also escape placeholders by placing a '\' before them which will prevent them from being processed. To place a '\' before a placeholder in the generated content you need to place 2 backslashes before the placeholder.
102102

103103
### Configuration
104104
You can supply configuration values to the generator either as an additional parameter within the api or you can include it within the template string itself by wrapping it within '<# #>' tokens. Configuration directives must appear as the first item with a template or else they will be ignored and removed.
@@ -134,23 +134,23 @@ Pattern files contain Named Patterns which can be used within Templates. TDG com
134134
### Commandline examples
135135
- Single repeating symbols using the following syntax
136136
- `tdg -t 'Letters <<\L{20}>> and Numbers <<\d{12}>>'`
137-
- Produces items like *'Letters EGTCWRTVAOIIGRDMEACQ and Numbers 963398177619'*.
137+
- Produces items like *'Letters DWZIZQWSYOREAADBHFCY and Numbers 489186903271'*.
138138
- Repeating patterns containing multiple letters or numbers of random length.
139-
- `tdg -t '<<(\L){5}>>'` - Will generate 5 random upper-case characters. e.g. *'YBTXR'*
140-
- `tdg -t '<<(\L\L\d){24}>>'` - Will generate 24 repeating letter-letter-number values e.g. *'YC8NE0MI2RV5LN9LN9OY3CA4YC3KJ4FX2UG9FC7OV0TD3HH1EN4CF9IV0KO0QC9VM9HR2BF4'*
139+
- `tdg -t '<<(\L){5}>>'` - Will generate 5 random upper-case characters. e.g. *'CRQGA'*
140+
- `tdg -t '<<(\L\L\d){24}>>'` - Will generate 24 repeating letter-letter-number values e.g. *'OG2QP1PR3JN1ZW0JV7IW8MH7UJ7UN0IK8BS9WH8QK4NI3WI3RV2ZI3EC9MV2LU1GA1HY1CM5'*
141141
- Variable length data can be generated also
142-
- `tdg -t '<<(\L){10,20}>>'` - Will generate a string containing between 10 and 20 characters of random value e.g. *'ZDHJWERVHFWZ'*
143-
- `tdg -t 'Letters <<\L{2,20}>> and Numbers <<\d{2,12}>>'` produces items like *'Letters SHLL and Numbers 85697196496'*
142+
- `tdg -t '<<(\L){10,20}>>'` - Will generate a string containing between 10 and 20 characters of random value e.g. *'RLLYYJXBKCRVJBNXQAJ'*
143+
- `tdg -t 'Letters <<\L{2,20}>> and Numbers <<\d{2,12}>>'` produces items like *'Letters QFQGHIFPJEP and Numbers 71697'*
144144
- Input can contain several placeholders.
145145
- `tdg -t 'Hi there <<\L\v{0,2}\l{0,2}\v \L\v{0,2}\l{0,2}\v{0,2}\l{0,2}\l>> how are you doing? Your SSN is <<[1-9]\d\d-\d\d-\d\d\d\d>>.' -c 100`
146-
- Produces 100 items like *'Hi there Va Xamw how are you doing? Your SSN is 480-83-2018.'*
146+
- Produces 100 items like *'Hi there Eu Emkabnc how are you doing? Your SSN is 114-81-6613.'*
147147
- Generate 100 SSN like values and output to console window.
148148
- `tdg -t '<<[1-9]\d\d-\d\d-\d\d\d\d>>' -c 100`
149-
- Produces 100 items like *'485-03-1427'*.
149+
- Produces 100 items like *'353-02-4514'*.
150150
- Generate 100 strings with random name like values and output to file.
151151
- `tdg -t 'Hi there <<\L\v\l\v \L\v\l\l\v\v\l\l\v>> how are you doing?' -c 100 -o C:\test1.txt`
152-
- Produces 100 items like *'Sala Nufniizqi'*.
153-
- `tdg -t '<<Letters \w{2,20} and Numbers \d{2,12}\n>>'` produces the following output: *'Letters xI3bgtn9Bb37lui and Numbers 800410
152+
- Produces 100 items like *'Aepi Euuwoavio'*.
153+
- `tdg -t '<<Letters \w{2,20} and Numbers \d{2,12}\n>>'` produces the following output: *'Letters nR9mhIGy_KE and Numbers 1134618008
154154
'*
155155

156156
## This README was generated using the generator. See the unit tests for other examples.
933 Bytes
Binary file not shown.

releases/tdg.3.4.2.0.nupkg

1.81 KB
Binary file not shown.

src/DataGenerator/gk.DataGenerator.CommandLine/CommandLineArgs.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Reflection;
3+
using System.Security.Policy;
4+
using System.Text;
35
using CommandLine;
46
using CommandLine.Text;
57

@@ -13,6 +15,9 @@ internal class CommandLineArgs
1315
[Option('p', "pattern", DefaultValue = "", HelpText = "The pattern to use when producing data.", MutuallyExclusiveSet = "fromCL")]
1416
public string Pattern { get; set; }
1517

18+
[Option('d', "detailed", DefaultValue = false, HelpText = "Show help text for pattern symbols")]
19+
public bool ShowPatternHelp { get; set; }
20+
1621
[Option('i', "inputfile", DefaultValue = "", HelpText = "The path of the input file.", Required = false, MutuallyExclusiveSet = "fromFile")]
1722
public string InputFilePath { get; set; }
1823

@@ -57,6 +62,7 @@ public string GetUsage()
5762
};
5863
help.AddPreOptionsLine("This is free software. You may redistribute copies of it under the terms of the MIT License.");
5964
help.AddPreOptionsLine(" <http://www.opensource.org/licenses/mit-license.php>");
65+
help.AddPreOptionsLine("Commandline Arguments:");
6066

6167
help.AddPostOptionsLine("Examples:");
6268
help.AddPostOptionsLine("\t tdg -t '<<LL>>'");
@@ -69,6 +75,45 @@ public string GetUsage()
6975
return help;
7076
}
7177

78+
public string GetPatternUsage()
79+
{
80+
var sb = new StringBuilder();
81+
82+
sb.AppendLine("Detailed Pattern Usage");
83+
sb.AppendLine();
84+
85+
sb.AppendLine("The following symbols can be used within a pattern to produce the desired output.");
86+
sb.AppendLine("\t\\. - A single random character of any type.");
87+
sb.AppendLine("\t\\W - A single random character from the following list ' .,;:'\"!&?£€$%^<>{}*+-=\\@#|~/'.");
88+
sb.AppendLine("\t\\w - A single random upper-case or lower-case letter.");
89+
sb.AppendLine("\t\\L - A single random upper-case Letter.");
90+
sb.AppendLine("\t\\l - A single random lower-case letter.");
91+
sb.AppendLine("\t\\V - A single random upper-case Vowel.");
92+
sb.AppendLine("\t\\v - A single random lower-case vowel.");
93+
sb.AppendLine("\t\\C - A single random upper-case Consonant.");
94+
sb.AppendLine("\t\\c - A single random lower-case consonant.");
95+
sb.AppendLine("\t\\D - A single random non number character.");
96+
sb.AppendLine("\t\\d - A single random number, 1-9.");
97+
sb.AppendLine("\t\\S - A single random non-whitespace character.");
98+
sb.AppendLine("\t\\s - A whitespace character (Tab, New Line, Space, Carriage Return or Form Feed)");
99+
sb.AppendLine("\t\\n - A newline character.");
100+
sb.AppendLine("\t\\t - A tab character.");
101+
102+
sb.AppendLine("Patterns usage");
103+
sb.AppendLine("\ttdg -p '(\\L)' - Will generate a random upper-case character.");
104+
sb.AppendLine("\ttdg -p '(\\L){5}' - Will generate 5 random upper-case characters.");
105+
sb.AppendLine("\ttdg -p '(\\L){10,20}' - Will generate between 10 and 20 random upper-case characters.");
106+
107+
sb.AppendLine("Patterns and normal text can be combined in templates");
108+
sb.AppendLine("Template usage");
109+
sb.AppendLine("\ttdg -t '<<Text containing a (\\L) pattern>>'");
110+
sb.AppendLine("\ttdg -t '<<Text containing a (\\L){5} pattern>>'");
111+
sb.AppendLine("\ttdg -t '<<Text containing a (\\L){10,20} pattern>>'");
112+
sb.AppendLine("View the Readme document for further examples");
113+
114+
return sb.ToString();
115+
}
116+
72117
}
73118
}
74119

src/DataGenerator/gk.DataGenerator.CommandLine/Program.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ private static void Main(string[] args)
2727
Console.WriteLine("Parse failed! Use --help flag for instructions on usage.");
2828
return;
2929
}
30+
31+
if (cla.ShowPatternHelp)
32+
{
33+
Console.Write(cla.GetPatternUsage());
34+
return;
35+
}
3036

3137
if (cla.Verbose)
3238
{
@@ -142,10 +148,19 @@ private static void OutputToFile(CommandLineArgs cla, string template)
142148
generateFrom = AlphaNumericGenerator.GenerateFromPattern;
143149
}
144150

145-
GenerationConfig config = new GenerationConfig();
151+
GenerationConfig config = null;
146152
if(cla.Seed.HasValue || !cla.NamedPatterns.IsNullOrEmpty()){
147-
if (cla.Seed.HasValue) config.Seed = cla.Seed;
148-
if (!cla.NamedPatterns.IsNullOrEmpty()) cla.NamedPatterns.Split(';').ToList().ForEach(config.PatternFiles.Add);
153+
154+
if (cla.Seed.HasValue)
155+
{
156+
if (config == null) config = new GenerationConfig();
157+
config.Seed = cla.Seed;
158+
}
159+
if (!cla.NamedPatterns.IsNullOrEmpty())
160+
{
161+
if (config == null) config = new GenerationConfig();
162+
cla.NamedPatterns.Split(';').ToList().ForEach(config.PatternFiles.Add);
163+
}
149164
}
150165

151166
using (var fs = new StreamWriter(cla.OutputFilePath))

0 commit comments

Comments
 (0)