Skip to content

Commit 30046a1

Browse files
authored
support comma separated values for --ignore-columns (dotnet#300)
1 parent 2c44aab commit 30046a1

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/mlnet.Test/CommandLineTests.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public void IgnoreColumnsArgumentTest()
261261
var trainDataset = Path.GetTempFileName();
262262
var testDataset = Path.GetTempFileName();
263263
var labelName = "Label";
264+
var ignoreColumns = "a,b,c";
264265

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

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

292+
parsingSuccessful = false;
293+
294+
args = new[] { "new", "--ml-task", "binary-classification", "--dataset", trainDataset, "--label-column-name", labelName, "--ignore-columns", "a b c" };
295+
parser.InvokeAsync(args).Wait();
296+
Assert.IsFalse(parsingSuccessful);
297+
291298
File.Delete(trainDataset);
292299
File.Delete(testDataset);
293300
}

src/mlnet/Commands/CommandDefinitions.cs

+34-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using System.CommandLine;
78
using System.CommandLine.Builder;
89
using System.CommandLine.Invocation;
910
using System.IO;
11+
using System.Linq;
1012

1113
namespace Microsoft.ML.CLI.Commands
1214
{
@@ -97,20 +99,47 @@ Option Name() =>
9799

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

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

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

112+
// This is a temporary hack to work around having comma separated values for argument. This feature needs to be enabled in the parser itself.
110113
Option IgnoreColumns() =>
111-
new Option(new List<string>() { "--ignore-columns" }, "Specify the columns that needs to be ignored in the given dataset.",
112-
new Argument<List<string>>(defaultValue: new List<string>()));
113-
114+
new Option(new List<string>() { "--ignore-columns" }, "Specify the columns that needs to be ignored in the given dataset.",
115+
new Argument<List<string>>(symbolResult =>
116+
{
117+
try
118+
{
119+
List<string> valuesList = new List<string>();
120+
foreach (var argument in symbolResult.Arguments)
121+
{
122+
if (!string.IsNullOrWhiteSpace(argument))
123+
{
124+
var values = argument.Split(",", StringSplitOptions.RemoveEmptyEntries);
125+
valuesList.AddRange(values);
126+
}
127+
}
128+
if (valuesList.Count > 0)
129+
return ArgumentResult.Success(valuesList);
130+
131+
}
132+
catch (Exception)
133+
{
134+
return ArgumentResult.Failure($"Unknown exception occured while parsing argument for --ignore-columns :{string.Join(' ', symbolResult.Arguments.ToArray())}");
135+
}
136+
137+
//This shouldn't be hit.
138+
return ArgumentResult.Failure($"Unknown error while parsing argument for --ignore-columns");
139+
})
140+
{
141+
Arity = ArgumentArity.OneOrMore
142+
});
114143
}
115144

116145
private static string[] GetMlTaskSuggestions()

0 commit comments

Comments
 (0)