Skip to content
Permalink
Browse files

Add better help text

  • Loading branch information...
Inventitech committed Feb 6, 2019
1 parent 68035fc commit 514d398661bf2504ad5c59ccf54c18450b20c05b
Showing with 97 additions and 69 deletions.
  1. +95 −69 Program.cs
  2. +2 −0 strans.csproj
@@ -4,100 +4,126 @@
using Microsoft.ProgramSynthesis.Transformation.Text;
using Microsoft.ProgramSynthesis.Wrangling.Constraints;
using CommandLine;
using CommandLine.Text;

class Options
namespace CommandLine.Text
{
[Option('b', "before", SetName = "SingleExample", Required = true, HelpText = "An example of an input line before transformation")]
public String before { get; set; }
[Option('a', "after", SetName = "SingleExample", Required = true, HelpText = "An example of an output line after transformation")]
public String after { get; set; }
// HelpText="Performs string manipulation tasks by learning from the provided example(s), instead of having to program them out explicitly.
class Options
{
[Option('b', "before", SetName = "SingleExample", Required = true, HelpText = "An example of an input line before transformation")]
public String before { get; set; }
[Option('a', "after", SetName = "SingleExample", Required = true, HelpText = "An example of an output line after transformation")]
public String after { get; set; }

[Option('f', "example-file", SetName = "MultiExample", Required = true, HelpText = "A file containing one or multiple transformation examples. The before and after transfer string are separated by => on the same line. One line per example.")]
public String exampleFile { get; set; }

[Option('f', "example-file", SetName = "MultiExample", Required = true, HelpText = "A file containing one or multiple transformation examples. The before and after transfer string are separated by => on the same line. One line per example.")]
public String multiFile { get; set; }
[Usage(ApplicationAlias = "strans")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Standard usage where strans infers a string transformation rule from one example, extracting the file extension from the input on STDIN (for example, from ls)", new Options { before = "file.bin", after = "bin" });
yield return new Example("Usage with multiple examples to infer string transformation rules stored in file examples. Syntax in example is one transformation example per line as before => after", new Options { exampleFile = "examples" });
}
}


}
}

class MyProgram
namespace Microsoft.ProgramSynthesis.Transformation.Text
{
static int Main(string[] args)
{
return Parser.Default.ParseArguments<Options>(args)
.MapResult(
options => run(options),
_ => 1);
}

static int run(Options options)
class MyProgram
{
Session session = new Session();

HashSet<Example> examples;
if(options.multiFile != null) {
examples = buildExamples(options.multiFile);
}
else {
examples = buildExamples(options);
static int Main(string[] args)
{
return Parser.Default.ParseArguments<Options>(args)
.MapResult(
options => run(options),
_ => 1);
}

if(examples.Count < 1) {
Console.Error.WriteLine(@"Error: You need to provide at least one valid transformation example of form 'before => after'.");
return -1;
}
static int run(Options options)
{
Session session = new Session();

session.Constraints.Add(examples);
Program program = session.Learn();
HashSet<Example> examples;
if (options.exampleFile != null)
{
examples = buildExamples(options.exampleFile);
}
else
{
examples = buildExamples(options);
}

if(program == null) {
Console.Error.WriteLine(@"Error: Given your transformation examples, no program could be learned.");
return -1;
}
if (examples.Count < 1)
{
Console.Error.WriteLine(@"Error: You need to provide at least one valid transformation example of form 'before => after'.");
return -1;
}

processInputPipe(program);
session.Constraints.Add(examples);
Program program = session.Learn();

return 0;
}
if (program == null)
{
Console.Error.WriteLine(@"Error: Given your transformation examples, no program could be learned.");
return -1;
}

static HashSet<Example> buildExamples(Options options)
{
HashSet<Example> examples = new HashSet<Example>();
var before = options.before.Trim();
var after = options.after.Trim();
processInputPipe(program);

examples.Add(new Example(new InputRow(before), after));
return 0;
}

return examples;
}
static HashSet<Example> buildExamples(Options options)
{
HashSet<Example> examples = new HashSet<Example>();
var before = options.before.Trim();
var after = options.after.Trim();

static HashSet<Example> buildExamples(string file)
{
HashSet<Example> examples = new HashSet<Example>();
string[] lines = System.IO.File.ReadAllLines(file);
examples.Add(new Example(new InputRow(before), after));

return examples;
}

foreach (string line in lines)
static HashSet<Example> buildExamples(string file)
{
if(line.Trim().Equals(System.String.Empty)) {
continue;
}

var example = line.Split(@"=>");
if (example.Length != 2)
{
Console.Error.WriteLine(@"Wrongly formatted line in examples: " + line);
}
else
HashSet<Example> examples = new HashSet<Example>();
string[] lines = System.IO.File.ReadAllLines(file);

foreach (string line in lines)
{
examples.Add(new Example(new InputRow(example[0].Trim()), example[1].Trim()));
if (line.Trim().Equals(System.String.Empty))
{
continue;
}

var example = line.Split(@"=>");
if (example.Length != 2)
{
Console.Error.WriteLine(@"Wrongly formatted line in examples: " + line);
}
else
{
examples.Add(new Example(new InputRow(example[0].Trim()), example[1].Trim()));
}
}
return examples;
}
return examples;
}


static void processInputPipe(Program program)
{
string line;
while ((line = Console.ReadLine()) != null)
static void processInputPipe(Program program)
{
Console.WriteLine(program.Run(new InputRow(line)));
string line;
while ((line = Console.ReadLine()) != null)
{
Console.WriteLine(program.Run(new InputRow(line)));
}
}
}
}
}
@@ -3,6 +3,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<Version>0.0.1</Version>
<Copyright>Copyirght (C) 2019 Moritz Beller (Inventitech)</Copyright>
</PropertyGroup>

<ItemGroup>

0 comments on commit 514d398

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.