commandlineparser / commandlineparser/commandline

Allow runtime attribute values via static methods

Ouverte
#191 3 commentaires 6 réactions 0 personnes assignées Voir sur GitHub
feature request
Langage dominant
C#
Étoiles
4.8k
Forks
478
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

Attribute values are limited to compile-time constant [attribute parameter types](https://github.com/dotnet/csharplang/blob/master/spec/attributes.md#attribute-parameter-types). This can be limiting when values need to be evaluated at runtime. For example, I might want to take my help text or license from some file, or I might want a non-constant default value, or I might want [dynamic options](https://github.com/commandlineparser/commandline/issues/9), etc.

One way around this would be a string pointer to a property/method which would supply the actual value:

```cs
class MyOptions
{
[Option('f', "foo", HelpTextProperty = nameof(FooHelpText), Default = nameof(FooDefault))]
public int Foo {get; set;}

private string FooHelpText {get { return File.ReadAllText("foo.txt"); } }
private int FooDefault {get { return Math.Pow(2,5); } }
}
```

Of course type validation would be performed (Default should have an assignable type to the property type, HelpTest should be a string, etc) for all such pointers (regardless of whether they are actually invoked, to fail fast in case of type inconsistency)

This is just one example, I believe this could be useful for many other attribute properties. To explicitly name the attributes currently defined in the library:
- `AssemblyLicenseAttribute` (don't really understand the multiple line thing BTW, why not just pass the full string including newlines)
- `AssemblyUsageAttribute` (same comment)
- `OptionAttribute`
- `ValueAttribute`
- `UsageAttribute`
- `VerbAttribute`

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

Commencez par examiner les définitions répertoriées de AssemblyLicenseAttribute, AssemblyUsageAttribute, OptionAttribute, ValueAttribute, UsageAttribute et VerbAttribute. Suivez la manière dont les valeurs de leurs attributs sont consommées, puis déterminez la portée de la résolution à l’exécution des méthodes ou propriétés et de la validation des types pour ces attributs. Le travail sera considéré comme terminé lorsqu’il inclura une conception arrêtée et une couverture des valeurs d’exécution prises en charge.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
csharp
Domaine
cli
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.