commandlineparser / commandlineparser/commandline
Options with mixed required and optional incompatible with nullable reference types
- Linguagem predominante
- C#
- Estrelas
- 4.8k
- Forks
- 478
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Descrição
[Nullable reference types](https://docs.microsoft.com/dotnet/csharp/nullable-references) is a great new feature to help mitigate `NullReferenceExceptions` in production; however, when declaring options classes with both required and optional parameters, are incompatible.
Imagine an options class like so:
```csharp
public class GetOptions
{
[Option("path", Required = true)]
public string Path { get; set; }
[Option("out")]
public string? Out { get; set; }
}
```
This would satisfy CommandLine, but not the C# compiler with nullable types enabled because `Path` has to be set by the constructor. The compiler errs. We could add a `?` to the `string` declaration, but then we are effectively breaking the intent of declaring that `Path` will never be null downstream.
So we refactor the class like so:
```csharp
public class GetOptions
{
public GetOptions(string path)
{
Path = path ?? throw new ArgumentNullException(nameof(path));
}
[Option("path", Required = true)]
public string Path { get; }
[Option("out")]
public string? Out { get; set; }
}
```
However, because the options class type is still mutable, the constructor is not used and instead a default constructor is required.
I propose a model that allows both. And because this could be considered a breaking change (though perhaps not in practice), this could be opt-in: perhaps a marker interface for options classes or some other configuration. This would override the `IsMutable` check in `InstanceBuilder` and allow for non-default construction and property setters.
We also want to use this in a project that targets net45, and are willing to contribute changes that not only enable this behavior but also add back support for net45. Or perhaps you wouldn't mind a revision on 2.3.0 which we use now.
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
Direção de pesquisa
Comece lendo a verificação IsMutable em InstanceBuilder e compare-a com os dois exemplos de classes de opções na issue. Defina um modelo de construção opt-in que ofereça suporte a propriedades obrigatórias e opcionais; em seguida, verifique se o comportamento existente permanece inalterado e avalie o suporte solicitado a net45. Considera-se concluído quando o comportamento proposto estiver implementado com cobertura adequada para ambos os caminhos de construção.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- csharp
- Domínio
- cli
- Tipo de issue
- Funcionalidade
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 25/100