Skip to content

Commit

Permalink
Making argument internal to option (dotnet#1148)
Browse files Browse the repository at this point in the history
* WIP making argument internal to option

* Fixing NRT warning

* Fixing end to end test app

* Updating tests
  • Loading branch information
Keboo committed Feb 16, 2021
1 parent 6493867 commit 465f9e5
Show file tree
Hide file tree
Showing 31 changed files with 416 additions and 1,350 deletions.
Expand Up @@ -20,10 +20,7 @@ public void SetupOneOptWithNestedCommand()
{
var rootCommand = new Command("root_command");
var nestedCommand = new Command("nested_command");
var option = new Option("-opt1")
{
Argument = new Argument<int>(() => 123)
};
var option = new Option<int>("-opt1", () => 123);
nestedCommand.AddOption(option);
rootCommand.AddCommand(nestedCommand);

Expand Down
Expand Up @@ -26,20 +26,8 @@ public void Setup()

var eatCommand = new Command("eat")
{
new Option("--fruit")
{
Argument = new Argument<string>()
{
Suggestions = {"apple", "banana", "cherry" }
}
},
new Option("--vegetable")
{
Argument = new Argument<string>()
{
Suggestions = {"asparagus", "broccoli", "carrot" }
}
}
new Option<string>("--fruit").AddSuggestions("apple", "banana", "cherry"),
new Option<string>("--vegetable").AddSuggestions("asparagus", "broccoli", "carrot")
};

_testParser = new CommandLineBuilder(eatCommand)
Expand Down
Expand Up @@ -21,13 +21,9 @@ public class Perf_Parser_Options_Bare
private IEnumerable<Option> GenerateTestOptions(int count, IArgumentArity arity)
=> Enumerable.Range(0, count)
.Select(i =>
new Option($"-option{i}")
new Option($"-option{i}", arity: arity)
{
Description = $"Description for -option {i} ....",
Argument = new Argument
{
Arity = arity
}
Description = $"Description for -option {i} ...."
}
);

Expand Down
Expand Up @@ -19,13 +19,9 @@ public class Perf_Parser_Options_With_Arguments

private IEnumerable<Option> GenerateTestOptions(int count, IArgumentArity arity)
=> Enumerable.Range(0, count)
.Select(i => new Option($"-option{i}")
.Select(i => new Option($"-option{i}", arity: arity)
{
Description = $"Description for -option {i} ....",
Argument = new Argument
{
Arity = arity
}
Description = $"Description for -option {i} ...."
}
);

Expand Down
Expand Up @@ -37,14 +37,8 @@ private IEnumerable<Option> GenerateOptionsArray(int count)
[GlobalSetup(Target = nameof(SuggestionsFromSymbol))]
public void Setup_FromSymbol()
{
_testSymbol = new Option("--hello")
{
Argument = new Argument
{
Arity = ArgumentArity.ExactlyOne,
Suggestions = { GenerateSuggestionsArray(TestSuggestionsCount) }
}
};
_testSymbol = new Option("--hello", arity: ArgumentArity.ExactlyOne)
.AddSuggestions(GenerateSuggestionsArray(TestSuggestionsCount));
}

[Benchmark]
Expand Down
Expand Up @@ -220,7 +220,7 @@ public void Options_are_not_built_for_infrastructure_types_exposed_by_method_par
var options = handlerMethod.BuildOptions();

options.Should()
.NotContain(o => o.Argument.ArgumentType == type);
.NotContain(o => o.ArgumentType == type);
}

[Fact]
Expand Down
20 changes: 6 additions & 14 deletions src/System.CommandLine.DragonFruit/CommandLine.cs
Expand Up @@ -290,24 +290,16 @@ public static IEnumerable<Option> BuildOptions(this MethodInfo method)

public static Option BuildOption(this ParameterDescriptor parameter)
{
var argument = new Argument
{
ArgumentType = parameter.ValueType
};

Func<object> getDefaultValue = null;
if (parameter.HasDefaultValue)
{
argument.SetDefaultValueFactory(parameter.GetDefaultValue);
getDefaultValue = parameter.GetDefaultValue;
}

var option = new Option(
return new Option(
parameter.BuildAlias(),
parameter.ValueName)
{
Argument = argument
};

return option;
parameter.ValueName,
parameter.ValueType,
getDefaultValue);
}

private static string GetDefaultXmlDocsFileLocation(Assembly assembly)
Expand Down
5 changes: 1 addition & 4 deletions src/System.CommandLine.Hosting.Tests/HostingTests.cs
Expand Up @@ -201,10 +201,7 @@ public static void UseHost_binds_parsed_arguments_to_options()
MyOptions options = null;

var rootCmd = new RootCommand();
rootCmd.AddOption(
new Option($"-{nameof(MyOptions.MyArgument)}")
{ Argument = new Argument<int>() }
);
rootCmd.AddOption(new Option<int>($"-{nameof(MyOptions.MyArgument)}"));
rootCmd.Handler = CommandHandler.Create((IHost host) =>
{
options = host.Services
Expand Down
20 changes: 4 additions & 16 deletions src/System.CommandLine.Suggest.Tests/EndToEndTestApp/Program.cs
Expand Up @@ -13,22 +13,10 @@ static async Task Main(string[] args)
{
var rootCommand = new RootCommand
{
new Option("--apple" )
{
Argument = new Argument<string>()
},
new Option("--banana")
{
Argument = new Argument<string>()
},
new Option("--cherry")
{
Argument = new Argument<string>()
},
new Option("--durian")
{
Argument = new Argument<string>()
}
new Option<string>("--apple" ),
new Option<string>("--banana"),
new Option<string>("--cherry"),
new Option<string>("--durian")
};

rootCommand.Handler = CommandHandler.Create(typeof(Program).GetMethod(nameof(Run)));
Expand Down
21 changes: 6 additions & 15 deletions src/System.CommandLine.Tests/ArgumentTests.cs
Expand Up @@ -367,14 +367,11 @@ public async Task Custom_argument_parser_is_only_called_once()
{
Handler = CommandHandler.Create<int>(Run)
};
command.AddOption(new Option("--value")
command.AddOption(new Option<int>("--value", result =>
{
Argument = new Argument<int>(result =>
{
callCount++;
return int.Parse(result.Tokens.Single().Value);
})
});
callCount++;
return int.Parse(result.Tokens.Single().Value);
}));

await command.InvokeAsync("--value 42");

Expand Down Expand Up @@ -439,14 +436,11 @@ public void When_custom_conversion_fails_then_an_option_does_not_accept_further_
var command = new Command("the-command")
{
new Argument<string>(),
new Option("-x")
{
Argument = new Argument<string>(argResult =>
new Option<string>("-x", argResult =>
{
argResult.ErrorMessage = "nope";
return default;
})
}
};

var result = command.Parse("the-command -x nope yep");
Expand All @@ -459,9 +453,7 @@ public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_va
{
var command = new Command("the-command")
{
new Option(new[] { "-o", "--one" })
{
Argument = new Argument<int>(argumentResult =>
new Option<int>(new[] { "-o", "--one" }, argumentResult =>
{
if (int.TryParse(argumentResult.Tokens.Select(t => t.Value).Single(), out var value))
{
Expand All @@ -472,7 +464,6 @@ public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_va
return default;
})
}
};

var result = command.Parse("the-command -o not-an-int");
Expand Down

0 comments on commit 465f9e5

Please sign in to comment.