Skip to content

Commit 260937c

Browse files
authored
Null exception for ignorecolumns in CLI (dotnet#294)
* Null exception for ignorecolumns in CLI * Check if ignore-columns array has values (as the default is now a empty array)
1 parent dc16273 commit 260937c

File tree

2 files changed

+45
-48
lines changed

2 files changed

+45
-48
lines changed

src/mlnet/Commands/CommandDefinitions.cs

+44-47
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
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;
65
using System.Collections.Generic;
76
using System.CommandLine;
87
using System.CommandLine.Builder;
98
using System.CommandLine.Invocation;
109
using System.IO;
11-
using System.Linq;
12-
using Microsoft.ML.Auto;
1310

1411
namespace Microsoft.ML.CLI.Commands
1512
{
@@ -19,40 +16,40 @@ internal static System.CommandLine.Command New(ICommandHandler handler)
1916
{
2017
var newCommand = new System.CommandLine.Command("new", "Create a new .NET project using ML.NET to train and run a model", handler: handler)
2118
{
22-
Dataset(),
23-
ValidationDataset(),
24-
TestDataset(),
25-
MlTask(),
26-
LabelName(),
27-
MaxExplorationTime(),
28-
LabelColumnIndex(),
29-
Verbosity(),
30-
Name(),
31-
OutputPath(),
32-
HasHeader(),
33-
Cache(),
34-
IgnoreColumns()
19+
Dataset(),
20+
ValidationDataset(),
21+
TestDataset(),
22+
MlTask(),
23+
LabelName(),
24+
MaxExplorationTime(),
25+
LabelColumnIndex(),
26+
Verbosity(),
27+
Name(),
28+
OutputPath(),
29+
HasHeader(),
30+
Cache(),
31+
IgnoreColumns(),
3532
};
3633

3734
newCommand.Argument.AddValidator((sym) =>
3835
{
39-
if (sym.Children["--dataset"] == null)
36+
if (!sym.Children.Contains("--dataset"))
4037
{
4138
return "Option required : --dataset";
4239
}
43-
if (sym.Children["--ml-task"] == null)
40+
if (!sym.Children.Contains("--ml-task"))
4441
{
4542
return "Option required : --ml-task";
4643
}
47-
if (sym.Children["--label-column-name"] == null && sym.Children["--label-column-index"] == null)
44+
if (!sym.Children.Contains("--label-column-name") && !sym.Children.Contains("--label-column-index"))
4845
{
4946
return "Option required : --label-column-name or --label-column-index";
5047
}
51-
if (sym.Children["--label-column-name"] != null && sym.Children["--label-column-index"] != null)
48+
if (sym.Children.Contains("--label-column-name") && sym.Children.Contains("--label-column-index"))
5249
{
5350
return "The following options are mutually exclusive please provide only one : --label-column-name, --label-column-index";
5451
}
55-
if (sym.Children["--label-column-index"] != null && sym.Children["--ignore-columns"] != null)
52+
if (sym.Children.Contains("--label-column-index") && sym.Children["--ignore-columns"].Arguments.Count > 0)
5653
{
5754
return "Currently we don't support specifying --ignore-columns in conjunction with --label-column-index";
5855
}
@@ -63,56 +60,56 @@ internal static System.CommandLine.Command New(ICommandHandler handler)
6360
return newCommand;
6461

6562
Option Dataset() =>
66-
new Option("--dataset", "File path to either a single dataset or a training dataset for train/test split approaches.",
67-
new Argument<FileInfo>().ExistingOnly());
63+
new Option("--dataset", "File path to either a single dataset or a training dataset for train/test split approaches.",
64+
new Argument<FileInfo>().ExistingOnly());
6865

6966
Option ValidationDataset() =>
70-
new Option("--validation-dataset", "File path for the validation dataset in train/validation/test split approaches.",
71-
new Argument<FileInfo>(defaultValue: default(FileInfo)).ExistingOnly());
67+
new Option("--validation-dataset", "File path for the validation dataset in train/validation/test split approaches.",
68+
new Argument<FileInfo>(defaultValue: default(FileInfo)).ExistingOnly());
7269

7370
Option TestDataset() =>
74-
new Option("--test-dataset", "File path for the test dataset in train/test approaches.",
75-
new Argument<FileInfo>(defaultValue: default(FileInfo)).ExistingOnly());
71+
new Option("--test-dataset", "File path for the test dataset in train/test approaches.",
72+
new Argument<FileInfo>(defaultValue: default(FileInfo)).ExistingOnly());
7673

7774
Option MlTask() =>
78-
new Option("--ml-task", "Type of ML task to perform. Current supported tasks: regression and binary-classification",
79-
new Argument<string>().FromAmong(GetMlTaskSuggestions()));
75+
new Option("--ml-task", "Type of ML task to perform. Current supported tasks: regression and binary-classification",
76+
new Argument<string>().FromAmong(GetMlTaskSuggestions()));
8077

8178
Option LabelName() =>
82-
new Option("--label-column-name", "Name of the label (target) column to predict.",
83-
new Argument<string>());
79+
new Option("--label-column-name", "Name of the label (target) column to predict.",
80+
new Argument<string>());
8481

8582
Option LabelColumnIndex() =>
86-
new Option("--label-column-index", "Index of the label (target) column to predict.",
87-
new Argument<uint>());
83+
new Option("--label-column-index", "Index of the label (target) column to predict.",
84+
new Argument<uint>());
8885

8986
Option MaxExplorationTime() =>
90-
new Option("--max-exploration-time", "Maximum time in seconds for exploring models with best configuration.",
91-
new Argument<uint>(defaultValue: 10));
87+
new Option("--max-exploration-time", "Maximum time in seconds for exploring models with best configuration.",
88+
new Argument<uint>(defaultValue: 10));
9289

9390
Option Verbosity() =>
94-
new Option(new List<string>() { "--verbosity" }, "Output verbosity choices: q[uiet], m[inimal] (by default) and diag[nostic]",
95-
new Argument<string>(defaultValue: "m").FromAmong(GetVerbositySuggestions()));
91+
new Option(new List<string>() { "--verbosity" }, "Output verbosity choices: q[uiet], m[inimal] (by default) and diag[nostic]",
92+
new Argument<string>(defaultValue: "m").FromAmong(GetVerbositySuggestions()));
9693

9794
Option Name() =>
98-
new Option(new List<string>() { "--name" }, "Name for the output project or solution to create. ",
99-
new Argument<string>());
95+
new Option(new List<string>() { "--name" }, "Name for the output project or solution to create. ",
96+
new Argument<string>());
10097

10198
Option OutputPath() =>
102-
new Option(new List<string>() { "--output-path" }, "Location folder to place the generated output. The default is the current directory.",
103-
new Argument<DirectoryInfo>(defaultValue: new DirectoryInfo(".")));
99+
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(".")));
104101

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

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

113110
Option IgnoreColumns() =>
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>>());
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>()));
116113

117114
}
118115

src/mlnet/Commands/New/NewCommandHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.ML.CLI.Commands.New
99
{
1010
internal class NewCommand : ICommand
1111
{
12-
private NewCommandSettings settings;
12+
private readonly NewCommandSettings settings;
1313

1414
internal NewCommand(NewCommandSettings settings)
1515
{

0 commit comments

Comments
 (0)