Skip to content

Commit

Permalink
support comma separated values for --ignore-columns (dotnet#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
srsaggam authored and Dmitry-A committed Aug 22, 2019
1 parent 361caa2 commit 17197e1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/mlnet.Test/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public void IgnoreColumnsArgumentTest()
var trainDataset = Path.GetTempFileName();
var testDataset = Path.GetTempFileName();
var labelName = "Label";
var ignoreColumns = "a,b,c";

// Create handler outside so that commandline and the handler is decoupled and testable.
var handler = CommandHandler.Create<NewCommandSettings>(
Expand All @@ -284,10 +285,16 @@ public void IgnoreColumnsArgumentTest()
.Build();

// valid cache test
string[] args = new[] { "new", "--ml-task", "binary-classification", "--dataset", trainDataset, "--label-column-name", labelName, "--ignore-columns", "a", "b", "c" };
string[] args = new[] { "new", "--ml-task", "binary-classification", "--dataset", trainDataset, "--label-column-name", labelName, "--ignore-columns", ignoreColumns };
parser.InvokeAsync(args).Wait();
Assert.IsTrue(parsingSuccessful);

parsingSuccessful = false;

args = new[] { "new", "--ml-task", "binary-classification", "--dataset", trainDataset, "--label-column-name", labelName, "--ignore-columns", "a b c" };
parser.InvokeAsync(args).Wait();
Assert.IsFalse(parsingSuccessful);

File.Delete(trainDataset);
File.Delete(testDataset);
}
Expand Down
39 changes: 34 additions & 5 deletions src/mlnet/Commands/CommandDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.IO;
using System.Linq;

namespace Microsoft.ML.CLI.Commands
{
Expand Down Expand Up @@ -97,20 +99,47 @@ Option Name() =>

Option OutputPath() =>
new Option(new List<string>() { "--output-path" }, "Location folder to place the generated output. The default is the current directory.",
new Argument<DirectoryInfo>(defaultValue:new DirectoryInfo(".")));
new Argument<DirectoryInfo>(defaultValue: new DirectoryInfo(".")));

Option HasHeader() =>
new Option(new List<string>() {"--has-header" }, "Specify true/false depending if the dataset file(s) have a header row.",
new Option(new List<string>() { "--has-header" }, "Specify true/false depending if the dataset file(s) have a header row.",
new Argument<bool>(defaultValue: true));

Option Cache() =>
new Option(new List<string>() { "--cache" }, "Specify on/off/auto if you want cache to be turned on, off or auto determined.",
new Argument<string>(defaultValue: "auto").FromAmong(GetCacheSuggestions()));

// This is a temporary hack to work around having comma separated values for argument. This feature needs to be enabled in the parser itself.
Option IgnoreColumns() =>
new Option(new List<string>() { "--ignore-columns" }, "Specify the columns that needs to be ignored in the given dataset.",
new Argument<List<string>>(defaultValue: new List<string>()));

new Option(new List<string>() { "--ignore-columns" }, "Specify the columns that needs to be ignored in the given dataset.",
new Argument<List<string>>(symbolResult =>
{
try
{
List<string> valuesList = new List<string>();
foreach (var argument in symbolResult.Arguments)
{
if (!string.IsNullOrWhiteSpace(argument))
{
var values = argument.Split(",", StringSplitOptions.RemoveEmptyEntries);
valuesList.AddRange(values);
}
}
if (valuesList.Count > 0)
return ArgumentResult.Success(valuesList);

}
catch (Exception)
{
return ArgumentResult.Failure($"Unknown exception occured while parsing argument for --ignore-columns :{string.Join(' ', symbolResult.Arguments.ToArray())}");
}

//This shouldn't be hit.
return ArgumentResult.Failure($"Unknown error while parsing argument for --ignore-columns");
})
{
Arity = ArgumentArity.OneOrMore
});
}

private static string[] GetMlTaskSuggestions()
Expand Down

0 comments on commit 17197e1

Please sign in to comment.